From 0822589dbe0c02dd182cad4db8b0a622d165ea4c Mon Sep 17 00:00:00 2001 From: Oluwatobi Samue Omisakin Date: Wed, 13 May 2020 14:23:51 +0300 Subject: [PATCH 01/10] Add docblock-declared combine, shift, and slice methods --- src/Traits/ArrayPrefix.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/Traits/ArrayPrefix.php b/src/Traits/ArrayPrefix.php index 5602934..6b61f67 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 ArrayedInterface combine(array $values) + * @method mixed shift() + * @method ArrayedInterface slice(int $offset, int $length = null, bool $preserve_keys = false) + */ trait ArrayPrefix { public function changeKeyCase(int $case = null): ArrayedInterface From 18c225ada9f9907a3fca19fa002d3a60bd94fa65 Mon Sep 17 00:00:00 2001 From: Oluwatobi Samue Omisakin Date: Wed, 13 May 2020 14:35:01 +0300 Subject: [PATCH 02/10] Add service provider with publishable config file --- composer.json | 9 ++++++++ src/Providers/ArrayedServiceProvider.php | 29 ++++++++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 src/Providers/ArrayedServiceProvider.php 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/Providers/ArrayedServiceProvider.php b/src/Providers/ArrayedServiceProvider.php new file mode 100644 index 0000000..97ecc65 --- /dev/null +++ b/src/Providers/ArrayedServiceProvider.php @@ -0,0 +1,29 @@ +publishes([ + __DIR__.'/../../config/migrations-snapshot.php' => config_path('arrayed'), + ], 'arrayed'); + + $this->app->bind(ArrayedInterface::class, Arrayed::class); + } + + public function register() + { + $this->mergeConfigFrom(__DIR__.'/../../config/arrayed.php', 'arrayed'); + } +} From 327f36909ebf04227a6b2c0adfda746a00c2db10 Mon Sep 17 00:00:00 2001 From: Oluwatobi Samue Omisakin Date: Wed, 13 May 2020 18:37:29 +0300 Subject: [PATCH 03/10] Fix with phpfixer --- src/Providers/ArrayedServiceProvider.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Providers/ArrayedServiceProvider.php b/src/Providers/ArrayedServiceProvider.php index 97ecc65..321c5ba 100644 --- a/src/Providers/ArrayedServiceProvider.php +++ b/src/Providers/ArrayedServiceProvider.php @@ -16,7 +16,7 @@ class ArrayedServiceProvider extends ServiceProvider public function boot() { $this->publishes([ - __DIR__.'/../../config/migrations-snapshot.php' => config_path('arrayed'), + __DIR__ . '/../../config/migrations-snapshot.php' => config_path('arrayed'), ], 'arrayed'); $this->app->bind(ArrayedInterface::class, Arrayed::class); @@ -24,6 +24,6 @@ public function boot() public function register() { - $this->mergeConfigFrom(__DIR__.'/../../config/arrayed.php', 'arrayed'); + $this->mergeConfigFrom(__DIR__ . '/../../config/arrayed.php', 'arrayed'); } } From adeedb9ff7ce0fa55edecf4fc8d4090c010a6b54 Mon Sep 17 00:00:00 2001 From: Oluwatobi Samue Omisakin Date: Wed, 13 May 2020 18:46:06 +0300 Subject: [PATCH 04/10] Fix config path --- src/Providers/ArrayedServiceProvider.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Providers/ArrayedServiceProvider.php b/src/Providers/ArrayedServiceProvider.php index 321c5ba..fed315e 100644 --- a/src/Providers/ArrayedServiceProvider.php +++ b/src/Providers/ArrayedServiceProvider.php @@ -16,7 +16,7 @@ class ArrayedServiceProvider extends ServiceProvider public function boot() { $this->publishes([ - __DIR__ . '/../../config/migrations-snapshot.php' => config_path('arrayed'), + __DIR__ . '/../../config/arrayed.php' => config_path('arrayed'), ], 'arrayed'); $this->app->bind(ArrayedInterface::class, Arrayed::class); From 00fbe6a26401ca3eee71f62fa43597ee73820071 Mon Sep 17 00:00:00 2001 From: Oluwatobi Samue Omisakin Date: Wed, 13 May 2020 18:55:20 +0300 Subject: [PATCH 05/10] Fix config path and file type --- src/Providers/ArrayedServiceProvider.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Providers/ArrayedServiceProvider.php b/src/Providers/ArrayedServiceProvider.php index fed315e..ec14033 100644 --- a/src/Providers/ArrayedServiceProvider.php +++ b/src/Providers/ArrayedServiceProvider.php @@ -16,7 +16,7 @@ class ArrayedServiceProvider extends ServiceProvider public function boot() { $this->publishes([ - __DIR__ . '/../../config/arrayed.php' => config_path('arrayed'), + __DIR__ . '/../../config/arrayed.php' => config_path('arrayed.php'), ], 'arrayed'); $this->app->bind(ArrayedInterface::class, Arrayed::class); From 4e057790199a096dd62cf8bccabdec936efe53c9 Mon Sep 17 00:00:00 2001 From: Oluwatobi Samue Omisakin Date: Wed, 13 May 2020 19:07:44 +0300 Subject: [PATCH 06/10] Get array as array inside collect --- src/Arrayed.php | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) 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'); From 816f69f99701be97ea81af2f244661fd65604e91 Mon Sep 17 00:00:00 2001 From: Oluwatobi Samue Omisakin Date: Mon, 18 May 2020 19:14:22 +0300 Subject: [PATCH 07/10] Declare doc block of non-implemented some array_* method --- src/Interfaces/ArrayPrefixInterface.php | 16 ++++++++++++++++ src/Interfaces/ArrayedInterface.php | 4 +--- src/Traits/ArrayPrefix.php | 3 --- 3 files changed, 17 insertions(+), 6 deletions(-) create mode 100644 src/Interfaces/ArrayPrefixInterface.php diff --git a/src/Interfaces/ArrayPrefixInterface.php b/src/Interfaces/ArrayPrefixInterface.php new file mode 100644 index 0000000..64a14b7 --- /dev/null +++ b/src/Interfaces/ArrayPrefixInterface.php @@ -0,0 +1,16 @@ + Date: Mon, 18 May 2020 20:40:46 +0300 Subject: [PATCH 08/10] Add diffKey, diffUassoc Implementation; combine, shift, and slice doc block methods --- src/Interfaces/ArrayPrefixInterface.php | 16 ---------- src/Interfaces/ArrayedInterface.php | 6 +++- src/Traits/ArrayPrefix.php | 13 ++++++++ tests/ArrayPrefixTraitTest.php | 40 ++++++++++++++++++++++++- 4 files changed, 57 insertions(+), 18 deletions(-) delete mode 100644 src/Interfaces/ArrayPrefixInterface.php diff --git a/src/Interfaces/ArrayPrefixInterface.php b/src/Interfaces/ArrayPrefixInterface.php deleted file mode 100644 index 64a14b7..0000000 --- a/src/Interfaces/ArrayPrefixInterface.php +++ /dev/null @@ -1,16 +0,0 @@ -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 From d51ce6e0497a26b6c657f5f64cf114263f5e0045 Mon Sep 17 00:00:00 2001 From: Oluwatobi Samue Omisakin Date: Mon, 18 May 2020 20:45:45 +0300 Subject: [PATCH 09/10] [skip travis] document new methods in README.md --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index 9a0d37c..27a178e 100644 --- a/README.md +++ b/README.md @@ -261,6 +261,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; ``` From 82b8c107e29e3f7f33daf42c5570cc93c25b5605 Mon Sep 17 00:00:00 2001 From: Oluwatobi Samue Omisakin Date: Mon, 18 May 2020 21:27:57 +0300 Subject: [PATCH 10/10] [skip travis] Add instruction for Laravel --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index 27a178e..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