diff --git a/README.md b/README.md index 9a0d37c..d091867 100644 --- a/README.md +++ b/README.md @@ -29,6 +29,9 @@ Minimum Requirement - PHP 7.2 + - Composer +- For using `collect()` method, requires `illuminate\support` >= 5.5 +> Additionally on Laravel App, if `arrayed.php`'s config file doesn't get added automatically then run `php artisan vendor:publish --tag=arrayed` after installation. + ## Quick Usage ```php @@ -261,6 +264,9 @@ Arrayed::diff(array $array2, array ...$_): ArrayedInterface; Arrayed::reverse(bool $preserve_keys = false): ArrayedInterface; +Arrayed::diffUassoc(callable $key_compare_func, array $array2, array ...$_): ArrayedInterface; + +Arrayed::diffKey(array $array2, array ...$_): ArrayedInterface; ``` diff --git a/composer.json b/composer.json index 52ec921..d2e24da 100644 --- a/composer.json +++ b/composer.json @@ -42,5 +42,14 @@ "psr-4": { "Transprime\\Arrayed\\Tests\\": "tests/" } + }, + "minimum-stability": "dev", + "prefer-stable": true, + "extra": { + "laravel": { + "providers": [ + "Transprime\\Arrayed\\Providers\\ArrayedServiceProvider" + ] + } } } diff --git a/src/Arrayed.php b/src/Arrayed.php index 06d7d09..f2f26eb 100644 --- a/src/Arrayed.php +++ b/src/Arrayed.php @@ -18,14 +18,19 @@ class Arrayed implements ArrayedInterface private $result; public function __construct(...$values) + { + $this->raw = $this->argumentsToArray(...$values); + + $this->setResult(new Undefined()); + } + + private function argumentsToArray(...$values) { if (func_num_args() === 1 && is_array($values[0])) { - $this->raw = $values[0]; - } else { - $this->raw = $values; + return $values[0]; } - $this->setResult(new Undefined()); + return $values; } public static function on(...$values): ArrayedInterface @@ -237,7 +242,7 @@ public function collect(...$with) $collectionClass = $this->getConfig('collection_class'); if ($collectionClass && class_exists($collectionClass)) { - return new $collectionClass($this->copy()->merge($with)->result()); + return new $collectionClass($this->copy()->merge($this->argumentsToArray(...$with))->result()); } throw new ArrayedException('Collection class is not set or does not exist'); diff --git a/src/Interfaces/ArrayedInterface.php b/src/Interfaces/ArrayedInterface.php index 7dc9a6a..cef5739 100644 --- a/src/Interfaces/ArrayedInterface.php +++ b/src/Interfaces/ArrayedInterface.php @@ -7,7 +7,6 @@ use ArrayAccess; use JsonSerializable; use IteratorAggregate; -use Transprime\Arrayed\Exceptions\ArrayedException; interface ArrayedInterface extends ArrayAccess, Countable, IteratorAggregate, JsonSerializable { @@ -88,6 +87,9 @@ public function diff(array $array2, array ...$_): ArrayedInterface; public function reverse(bool $preserve_keys = false): ArrayedInterface; + public function diffUassoc(callable $key_compare_func, array $array2, array ...$_): ArrayedInterface; + + public function diffKey(array $array2, array ...$_): ArrayedInterface; /** * Like php array_key_exists, this instead search if (one or more) keys exists in the array diff --git a/src/Providers/ArrayedServiceProvider.php b/src/Providers/ArrayedServiceProvider.php new file mode 100644 index 0000000..ec14033 --- /dev/null +++ b/src/Providers/ArrayedServiceProvider.php @@ -0,0 +1,29 @@ +publishes([ + __DIR__ . '/../../config/arrayed.php' => config_path('arrayed.php'), + ], 'arrayed'); + + $this->app->bind(ArrayedInterface::class, Arrayed::class); + } + + public function register() + { + $this->mergeConfigFrom(__DIR__ . '/../../config/arrayed.php', 'arrayed'); + } +} diff --git a/src/Traits/ArrayPrefix.php b/src/Traits/ArrayPrefix.php index 5602934..b3c6967 100644 --- a/src/Traits/ArrayPrefix.php +++ b/src/Traits/ArrayPrefix.php @@ -5,6 +5,14 @@ use Transprime\Arrayed\Exceptions\ArrayedException; use Transprime\Arrayed\Interfaces\ArrayedInterface; +/** + * Trait ArrayPrefix + * @package Transprime\Arrayed\Traits + * + * @method self combine(array $values) + * @method mixed shift() + * @method self slice(int $offset, int $length = null, bool $preserve_keys = false) + */ trait ArrayPrefix { public function changeKeyCase(int $case = null): ArrayedInterface @@ -42,6 +50,16 @@ public function reverse(bool $preserve_keys = false): ArrayedInterface return $this->setResult(array_reverse($this->getWorkableItem(), $preserve_keys)); } + public function diffUassoc(callable $key_compare_func, array $array2, array ...$_): ArrayedInterface + { + return $this->setResult(array_diff_uassoc($this->getWorkableItem(), $array2, ...$_, ...[$key_compare_func])); + } + + public function diffKey(array $array2, array ...$_): ArrayedInterface + { + return $this->setResult(array_diff_key($this->getWorkableItem(), $array2, ...$_)); + } + /** * Like php array_key_exists, this instead search if (one or more) keys exists in the array * diff --git a/tests/ArrayPrefixTraitTest.php b/tests/ArrayPrefixTraitTest.php index 6530401..7cc4001 100644 --- a/tests/ArrayPrefixTraitTest.php +++ b/tests/ArrayPrefixTraitTest.php @@ -17,7 +17,7 @@ public function testChangeKeyCase() public function testChunk() { $this->assertEquals( - [[1,2], [3,4]], + [[1, 2], [3, 4]], arrayed(1, 2, 3, 4)->chunk(2)->result() ); } @@ -77,6 +77,44 @@ public function testReverse() ); } + public function testDiffUassoc() + { + $first = ['a' => 'b', 'c' => 'd']; + $second = ['2' => 'b', 'c' => 'd']; + $third = ['2' => 'b', 'k' => 'd']; + $callback = function ($first, $second) { + return $first === $second; + }; + + $this->assertEquals( + array_diff_uassoc($first, $second, $callback), + arrayed($first)->diffUassoc($callback, $second)->result() + ); + + $this->assertEquals( + array_diff_uassoc($first, $second, $third, $callback), + arrayed($first)->diffUassoc($callback, $second, $third)->result() + ); + } + + public function testDiffKey() + { + $first = ['a' => 'b', 'c' => 'd']; + $second = ['2' => 'b', 'c' => 'd']; + $third = ['2' => 'b', 'k' => 'd', 'm' => 'a']; + $fourth = ['m' => 'b', 'h' => 'd', 's' => 'a']; + + $this->assertEquals( + array_diff_key($first, $second), + arrayed($first)->diffKey($second)->result() + ); + + $this->assertEquals( + array_diff_key($first, $second, $third, $fourth), + arrayed($first)->diffKey($second, $third, $fourth)->result() + ); + } + public function testUnImplementedArrayPrefixFunction() { // array_combine