From 4c1ad03a6110a13c492c39c805f6e0acee4d009e Mon Sep 17 00:00:00 2001 From: Oluwatobi Samue Omisakin Date: Thu, 7 May 2020 20:20:16 +0300 Subject: [PATCH 1/8] Add tap method that uses a global tap or inbuilt tap --- src/Arrayed.php | 10 +++++++++- src/Interfaces/ArrayedInterface.php | 3 +++ tests/ArrayedTest.php | 11 +++++++++++ 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/Arrayed.php b/src/Arrayed.php index 4f16caf..fc01a40 100644 --- a/src/Arrayed.php +++ b/src/Arrayed.php @@ -2,9 +2,10 @@ namespace Transprime\Arrayed; +use Closure; use ArrayIterator; -use Transprime\Arrayed\Traits\ArrayPrefix; use Transprime\Arrayed\Types\Undefined; +use Transprime\Arrayed\Traits\ArrayPrefix; use Transprime\Arrayed\Interfaces\ArrayedInterface; class Arrayed implements ArrayedInterface @@ -217,4 +218,11 @@ public function copy(): ArrayedInterface { return new self($this->result()); } + + public function tap(Closure $closure): ArrayedInterface + { + function_exists('tap') ? tap($closure, $this->copy()) : $closure($this->copy()); + + return $this; + } } diff --git a/src/Interfaces/ArrayedInterface.php b/src/Interfaces/ArrayedInterface.php index 4d28db7..dbcdee3 100644 --- a/src/Interfaces/ArrayedInterface.php +++ b/src/Interfaces/ArrayedInterface.php @@ -2,6 +2,7 @@ namespace Transprime\Arrayed\Interfaces; +use Closure; use Countable; use ArrayAccess; use JsonSerializable; @@ -64,6 +65,8 @@ public function __toString(): string; public function copy(): ArrayedInterface; + public function tap(Closure $closure): ArrayedInterface; + public function changeKeyCase(int $case = null): ArrayedInterface; diff --git a/tests/ArrayedTest.php b/tests/ArrayedTest.php index b4b6514..3f9dbe7 100644 --- a/tests/ArrayedTest.php +++ b/tests/ArrayedTest.php @@ -338,4 +338,15 @@ public function testCopy() $this->assertEquals($arrayed, $arrayed->copy()); $this->assertNotSame($arrayed, $arrayed->copy()); } + + public function testTap() + { + $res = []; + $arrayed = arrayed(1, 2, 3) + ->tap(function ($arrd) use (&$res) { + $res = $arrd->reverse(); + }); + + $this->assertEquals($arrayed[0], $res[2]); + } } From 345bd5fe5c47e72264f0a4cb15a77401a86035a0 Mon Sep 17 00:00:00 2001 From: Oluwatobi Samue Omisakin Date: Tue, 12 May 2020 19:29:22 +0300 Subject: [PATCH 2/8] Include collection by configuration --- Helpers/helpers.php | 32 +++++++++++++++++++++++++++++ composer.json | 3 ++- config/arrayed.php | 6 ++++++ src/Arrayed.php | 24 +++++++++++++++++++++- src/Interfaces/ArrayedInterface.php | 9 ++++++-- tests/ArrayedTest.php | 15 ++++++++++++++ 6 files changed, 85 insertions(+), 4 deletions(-) create mode 100644 config/arrayed.php diff --git a/Helpers/helpers.php b/Helpers/helpers.php index 71e7688..be98c81 100644 --- a/Helpers/helpers.php +++ b/Helpers/helpers.php @@ -14,4 +14,36 @@ function arrayed(...$value) { return new Arrayed(...$value); } +} + +if (! function_exists('configured')) { + /** + * Get config value + * + *
Up to firs level without Laravel config + * + * @param $value + * @param null $default + * @return mixed + */ + function configured($value, $default = null) + { + if (function_exists('config')) { + return config($value, $default); + } + + $keys = explode('.', $value); + + $data = require (__DIR__.'/../config/'.$keys[0].'.php'); + + foreach ($keys as $index => $key) { + if ($index < 1) { + continue; + } + + return $data[$key] ?? $default; + } + + return $data; + } } \ No newline at end of file diff --git a/composer.json b/composer.json index b6f814b..52ec921 100644 --- a/composer.json +++ b/composer.json @@ -27,7 +27,8 @@ }, "require-dev": { "phpunit/phpunit": ">=8.0", - "squizlabs/php_codesniffer": "3.*" + "squizlabs/php_codesniffer": "3.*", + "illuminate/support": ">=5.5" }, "autoload": { "psr-4": { diff --git a/config/arrayed.php b/config/arrayed.php new file mode 100644 index 0000000..d66938a --- /dev/null +++ b/config/arrayed.php @@ -0,0 +1,6 @@ + '\\Illuminate\\Support\\Collection', +]; \ No newline at end of file diff --git a/src/Arrayed.php b/src/Arrayed.php index fc01a40..6aeae9c 100644 --- a/src/Arrayed.php +++ b/src/Arrayed.php @@ -4,6 +4,7 @@ use Closure; use ArrayIterator; +use Transprime\Arrayed\Exceptions\ArrayedException; use Transprime\Arrayed\Types\Undefined; use Transprime\Arrayed\Traits\ArrayPrefix; use Transprime\Arrayed\Interfaces\ArrayedInterface; @@ -216,7 +217,7 @@ public function jsonSerialize() public function copy(): ArrayedInterface { - return new self($this->result()); + return new static($this->result()); } public function tap(Closure $closure): ArrayedInterface @@ -225,4 +226,25 @@ function_exists('tap') ? tap($closure, $this->copy()) : $closure($this->copy()); return $this; } + + /** + * @param $with + * @return mixed + * @throws ArrayedException + */ + public function collect(...$with) + { + $collectionClass = $this->getConfig('collection_class'); + + if ($collectionClass && class_exists($collectionClass)) { + return new $collectionClass($this->copy()->merge($with)->result()); + } + + throw new ArrayedException('Collection class is not set or does not exist'); + } + + private function getConfig($item) + { + return configured("arrayed.$item", null); + } } diff --git a/src/Interfaces/ArrayedInterface.php b/src/Interfaces/ArrayedInterface.php index dbcdee3..7dc9a6a 100644 --- a/src/Interfaces/ArrayedInterface.php +++ b/src/Interfaces/ArrayedInterface.php @@ -7,6 +7,7 @@ use ArrayAccess; use JsonSerializable; use IteratorAggregate; +use Transprime\Arrayed\Exceptions\ArrayedException; interface ArrayedInterface extends ArrayAccess, Countable, IteratorAggregate, JsonSerializable { @@ -61,12 +62,16 @@ public function raw(): array; */ public function initial(): array; - public function __toString(): string; - public function copy(): ArrayedInterface; public function tap(Closure $closure): ArrayedInterface; + /** + * @param $with + * @return \Illuminate\Support\Collection|mixed + */ + public function collect(...$with); + public function changeKeyCase(int $case = null): ArrayedInterface; diff --git a/tests/ArrayedTest.php b/tests/ArrayedTest.php index 3f9dbe7..0d20f7f 100644 --- a/tests/ArrayedTest.php +++ b/tests/ArrayedTest.php @@ -349,4 +349,19 @@ public function testTap() $this->assertEquals($arrayed[0], $res[2]); } + + public function testCollect() + { + $this->assertIsObject( + arrayed(1, 2, 3) + ->collect() + ); + + $this->assertEquals( + 6, + arrayed(1, 2, 3) + ->collect(2, 3, 4) + ->count() + ); + } } From 76d35f7fbb43bbe130c42d854038a30c7707a636 Mon Sep 17 00:00:00 2001 From: Oluwatobi Samue Omisakin Date: Tue, 12 May 2020 19:30:17 +0300 Subject: [PATCH 3/8] Update to fix file format --- tests/ArrayedTest.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/tests/ArrayedTest.php b/tests/ArrayedTest.php index 0d20f7f..2840523 100644 --- a/tests/ArrayedTest.php +++ b/tests/ArrayedTest.php @@ -352,16 +352,16 @@ public function testTap() public function testCollect() { - $this->assertIsObject( - arrayed(1, 2, 3) + $this->assertIsObject( + arrayed(1, 2, 3) ->collect() - ); + ); - $this->assertEquals( - 6, - arrayed(1, 2, 3) + $this->assertEquals( + 6, + arrayed(1, 2, 3) ->collect(2, 3, 4) - ->count() - ); + ->count() + ); } } From 79ff052787ebd9b10fd6169b227d2ca03f530c4c Mon Sep 17 00:00:00 2001 From: Oluwatobi Samue Omisakin Date: Tue, 12 May 2020 20:03:49 +0300 Subject: [PATCH 4/8] Update to fix test, on tap method --- src/Arrayed.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Arrayed.php b/src/Arrayed.php index 6aeae9c..06d7d09 100644 --- a/src/Arrayed.php +++ b/src/Arrayed.php @@ -222,7 +222,7 @@ public function copy(): ArrayedInterface public function tap(Closure $closure): ArrayedInterface { - function_exists('tap') ? tap($closure, $this->copy()) : $closure($this->copy()); + function_exists('tap') ? tap($this->copy(), $closure) : $closure($this->copy()); return $this; } From 4f2c2c312dff38e724985783ffd52b37539f11a7 Mon Sep 17 00:00:00 2001 From: Oluwatobi Samue Omisakin Date: Tue, 12 May 2020 20:22:00 +0300 Subject: [PATCH 5/8] [skip travis] Update documentation with collect and other APIs --- README.md | 47 +++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 45 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index dcc6594..bd5d9e9 100644 --- a/README.md +++ b/README.md @@ -85,6 +85,16 @@ arrayed(['a' => 1, 'b' => 2]); Laravel Collections +New: `collect()` method :tada: + +```php +arrayed(1,2)->collect(); // instance of Illuminate/Support/Collection +arrayed(1,2)->collect(3, 4); //merged with first one to give [1, 2, 3, 4] +``` +> Swap out the default Collection class by editing `config/arrayed.php`'s collection_class value + +Others: + ```php collect(arrayed(1, 2, 3, 4)); @@ -101,6 +111,17 @@ Laravel Response accepts `Arrayed`: response()->json(arrayed(1, 2, 3)->flip()); ``` +#### Special methods + +New :tada: `tap()` method allows other actions on the last resulting `Arrayed` instance without mutating the last `Arrayed` result: + +```php +arrayed(1, 2, 3) + ->tap(function ($arrd) { + logger('Array has '.$arrd->count()); + }); +``` + ## Others If any operation normally returns an array, the return value will give `Arrayed` instance so that other methods can be chained on them otherwise a non-array value is returned as can be seen that `sum()` returns an integer in the example below: @@ -169,7 +190,7 @@ arrayed(['a', 'b']) These are the API's available ```php -static Arrayed::on(...$values): ArrayedInterface; +static Arrayed::on(...$values): ArrayedInterface; //new instance of Arrayed Arrayed::map($callback): ArrayedInterface; @@ -217,7 +238,29 @@ Arrayed::raw(): array; Arrayed::initial(): array; // Deprecated, use raw() instead -Arrayed::__toString(): string; // returns string rep of the array +Arrayed::tap(Closure $closure): ArrayedInterface; + +Arrayed::copy(): ArrayedInterface; + +Arrayed::collect(...$with): array; + +// Other Array_* methods + +Arrayed::changeKeyCase(int $case = null): ArrayedInterface; + +Arrayed::chunk(int $size, bool $preserve_keys = false): ArrayedInterface; + +Arrayed::column($column, $index_key = null): ArrayedInterface; + +Arrayed::countValues(): ArrayedInterface; + +Arrayed::diffAssoc(array $array2, array ...$_): ArrayedInterface; + +Arrayed::diff(array $array2, array ...$_): ArrayedInterface; + +Arrayed::reverse(bool $preserve_keys = false): ArrayedInterface; + + ``` ## Additional Information From b10093d645e6f7318d13cfe36fb3291b7f456088 Mon Sep 17 00:00:00 2001 From: Oluwatobi Samue Omisakin Date: Tue, 12 May 2020 20:39:50 +0300 Subject: [PATCH 6/8] [skip travis] Update to fix first example on doc --- README.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index bd5d9e9..1a73ea6 100644 --- a/README.md +++ b/README.md @@ -33,11 +33,11 @@ Minimum Requirement ```php arrayed(1, 2, 'ninja') - ->filter(fn($val) => is_int($val)) - ->map(fn($val) => $val + 1) - ->flip() - ->values() - ->sum()(); //use () or ->result() at the end; + ->filter(fn($val) => is_int($val)) // [1,2] + ->map(fn($val) => $val + 1) // [2, 3] + ->flip() // [0, 1] + ->values() // [0, 1] + ->sum(); // 1 ``` Instead of: @@ -134,7 +134,7 @@ arrayed(['a' => 1, 'b' => 2]) ->sum(); // returns an integer, we cannot chain ``` -You can still work on the result (if its an array'ed value) by passing a closure/callable function to `result()` method: +You can work on a result (if its an array'ed value) by passing a closure/callable function to `result()` method: ```php arrayed(['a' => 'name', 'b' => 'age']) From 7b35775cacf2e5aa48db3ab0c802fa994b25a1c8 Mon Sep 17 00:00:00 2001 From: Oluwatobi Samue Omisakin Date: Tue, 12 May 2020 20:42:44 +0300 Subject: [PATCH 7/8] Give piped calls a heading --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 1a73ea6..ccedd18 100644 --- a/README.md +++ b/README.md @@ -153,6 +153,7 @@ Get the original array data with `raw()` method arrayed([1, 2])->raw(); //[1,2] ``` +#### Piped calls As at now not all `array_*` functions have been implemented. `pipe()` method helps to call custom function on the array result. From a407040a000c6a12c0078dbecb7c68dc33b04f01 Mon Sep 17 00:00:00 2001 From: Oluwatobi Samue Omisakin Date: Tue, 12 May 2020 20:43:18 +0300 Subject: [PATCH 8/8] [skip travis] Add column pointer to available apis --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index ccedd18..82a08bf 100644 --- a/README.md +++ b/README.md @@ -188,7 +188,7 @@ arrayed(['a', 'b']) ## APIs -These are the API's available +These are the API's available: ```php static Arrayed::on(...$values): ArrayedInterface; //new instance of Arrayed