Skip to content

Commit 21abc4c

Browse files
committed
initial commit
0 parents  commit 21abc4c

17 files changed

+416
-0
lines changed

.editorconfig

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
root = true
2+
3+
[*]
4+
charset = utf-8
5+
end_of_line = lf
6+
insert_final_newline = true
7+
trim_trailing_whitespace = true
8+
indent_style = tab
9+
indent_size = tab
10+
tab_width = 4
11+
12+
[*.yml]
13+
indent_style = space
14+
indent_size = 4
15+
16+
[composer.json]
17+
indent_style = space
18+
indent_size = 4

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
## ide
2+
/.idea/
3+
4+
## composer
5+
/composer.lock
6+
/vendor/

.travis.yml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
language: php
2+
php:
3+
- 7.4
4+
5+
before_install:
6+
- phpenv config-rm xdebug.ini || return 0 # Turn off XDebug
7+
8+
install:
9+
- travis_retry composer install --no-progress --prefer-dist # Install dependencies
10+
11+
script:
12+
- make tests
13+
14+
after_failure:
15+
- for i in $(find tests -name \*.actual); do echo "--- $i"; cat $i; echo; echo; done # Print *.actual content
16+
17+
jobs:
18+
include:
19+
- stage: Code style
20+
php: 7.4
21+
script:
22+
- make cs
23+
24+
- stage: PHPStan
25+
php: 7.4
26+
script:
27+
- make phpstan
28+
29+
sudo: false
30+
31+
cache:
32+
directories:
33+
- $HOME/.composer/cache

LICENSE

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
MIT License
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is
8+
furnished to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in all
11+
copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19+
SOFTWARE.

Makefile

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
.PHONY: phpstan tests cs csfix lint
2+
3+
check: tests cs phpstan lint
4+
5+
vendor: composer.json composer.lock
6+
composer install
7+
8+
tests: vendor
9+
vendor/bin/tester -s -p php --colors 1 -C tests/cases
10+
11+
lint: vendor
12+
vendor/bin/linter src tests
13+
14+
cs: vendor
15+
vendor/bin/codesniffer src tests
16+
17+
csfix: vendor
18+
vendor/bin/codefixer src tests
19+
20+
phpstan: vendor
21+
vendor/bin/phpstan analyse -l max -c phpstan.neon src

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
## php utility
2+
3+
[![Build Status](https://travis-ci.org/utilitte/php.svg?branch=master)](https://travis-ci.org/utilitte/php)
4+
[![Licence](https://img.shields.io/packagist/l/utilitte/php.svg?style=flat-square)](https://packagist.org/packages/utilitte/php)
5+
[![Latest stable](https://img.shields.io/packagist/v/utilitte/php.svg?style=flat-square)](https://packagist.org/packages/utilitte/php)

composer.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "utilitte/php",
3+
"description": "",
4+
"license": "proprietary",
5+
"type": "library",
6+
"require": {
7+
"php": ">=7.4"
8+
},
9+
"require-dev": {
10+
"phpstan/phpstan": "^0.12.33",
11+
"phpstan/extension-installer": "^1.0",
12+
"phpstan/phpstan-deprecation-rules": "^0.12.2",
13+
"utilitte/qa": "~1.0.1",
14+
"nette/tester": "^2.3.0"
15+
},
16+
"autoload": {
17+
"psr-4": {
18+
"Utilitte\\Php\\": "src/"
19+
}
20+
}
21+
}

phpstan.neon

Whitespace-only changes.

src/Arrays.php

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Utilitte\Php;
4+
5+
use InvalidArgumentException;
6+
use LogicException;
7+
use Traversable;
8+
use Utilitte\Php\ValueObject\ArraySynchronized;
9+
10+
class Arrays
11+
{
12+
13+
/**
14+
* @param mixed[] $defaults
15+
* @param mixed[] $array
16+
* @return mixed[]
17+
*/
18+
public static function defaults(array $defaults, iterable $array): array
19+
{
20+
foreach ($array as $key => $value) {
21+
if (!array_key_exists($key, $defaults)) {
22+
throw new LogicException(
23+
sprintf('Key %s is not allowed in array', $key)
24+
);
25+
}
26+
27+
$defaults[$key] = $value;
28+
}
29+
30+
return $defaults;
31+
}
32+
33+
/**
34+
* @param mixed[] $array
35+
* @return mixed
36+
*/
37+
public static function firstValue(array $array)
38+
{
39+
$key = array_key_first($array);
40+
41+
if ($key === null) {
42+
throw new InvalidArgumentException('Given array is empty');
43+
}
44+
45+
return $array[$key];
46+
}
47+
48+
/**
49+
* @param mixed[] $previous
50+
* @param mixed[] $current
51+
*/
52+
public static function synchronize(
53+
iterable $previous,
54+
iterable $current,
55+
?callable $comparator = null
56+
): ArraySynchronized
57+
{
58+
$comparator ??= [self::class, 'strictComparator'];
59+
60+
$added = self::iterableToArray($current);
61+
$removed = self::iterableToArray($previous);
62+
$result = $added;
63+
64+
foreach ($removed as $key => $value) {
65+
foreach ($added as $key1 => $value1) {
66+
if ($comparator($value, $value1)) {
67+
unset($removed[$key]);
68+
unset($added[$key1]);
69+
}
70+
}
71+
}
72+
73+
return new ArraySynchronized($added, $removed, $result);
74+
}
75+
76+
/**
77+
* @param mixed[] $iterable
78+
* @return mixed[]
79+
*/
80+
public static function iterableToArray(iterable $iterable): array
81+
{
82+
return $iterable instanceof Traversable ? iterator_to_array($iterable) : (array) $iterable;
83+
}
84+
85+
/**
86+
* @param mixed[] $values
87+
* @param string|int $key
88+
* @param string|int $value
89+
* @return mixed[]
90+
*/
91+
public static function twoDimensionArrayToAssociativeArray(array $values, $key, $value): array
92+
{
93+
$return = [];
94+
95+
foreach ($values as $itemKey => $item) {
96+
if (!array_key_exists($key, $item)) {
97+
throw new InvalidArgumentException(
98+
sprintf('Key "key" %s not exists in array[%s]', (string) $key, (string) $itemKey)
99+
);
100+
}
101+
102+
if (!array_key_exists($value, $item)) {
103+
throw new InvalidArgumentException(
104+
sprintf('Key "value" %s not exists in array[%s]', (string) $value, (string) $itemKey)
105+
);
106+
}
107+
108+
$return[$item[$key]] = $item[$value];
109+
}
110+
111+
return $return;
112+
}
113+
114+
/**
115+
* @param mixed $first
116+
* @param mixed $second
117+
* @phpcsSuppress SlevomatCodingStandard.Classes.UnusedPrivateElements.UnusedMethod
118+
*/
119+
private static function strictComparator($first, $second): bool
120+
{
121+
return $first === $second;
122+
}
123+
124+
}

src/Objects.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Utilitte\Php;
4+
5+
class Objects
6+
{
7+
8+
/**
9+
* @param object|string $object
10+
*/
11+
public static function instanceOf($object, string $className): bool
12+
{
13+
if (is_object($object)) {
14+
$object = get_class($object);
15+
}
16+
17+
return $className === $object || is_subclass_of($object, $className);
18+
}
19+
20+
}

0 commit comments

Comments
 (0)