From 9ee29491b5c262c408898f3a0bd9f276f9ec4eb2 Mon Sep 17 00:00:00 2001 From: Oluwatobi Omisakin Date: Tue, 30 Apr 2024 02:02:12 +0300 Subject: [PATCH 1/3] feat: Fix merge() issue --- src/Arrayed.php | 13 +++++++++++++ src/Interfaces/ArrayedInterface.php | 2 ++ src/Traits/ArrayPrefix.php | 19 ++++++++++++++++++- tests/ArrayedTest.php | 9 +++++++++ 4 files changed, 42 insertions(+), 1 deletion(-) diff --git a/src/Arrayed.php b/src/Arrayed.php index 6a2001d..f4d2153 100644 --- a/src/Arrayed.php +++ b/src/Arrayed.php @@ -32,6 +32,10 @@ private function argumentsToArray(...$values): array return $values[0]; } + if (func_num_args() === 1 && $values[0] instanceof ArrayedInterface) { + return $values[0]->toArray(); + } + return $values; } @@ -218,6 +222,15 @@ public function __toString(): string return json_encode($this->getWorkableItem(true)); } + public function toArray(): array + { + return $this->walk( + function (&$value, $key) { + $value = $value instanceof ArrayedInterface ? $value->raw() : $value; + } + )->result(); + } + /** * @inheritDoc */ diff --git a/src/Interfaces/ArrayedInterface.php b/src/Interfaces/ArrayedInterface.php index cef5739..0930948 100644 --- a/src/Interfaces/ArrayedInterface.php +++ b/src/Interfaces/ArrayedInterface.php @@ -99,4 +99,6 @@ public function diffKey(array $array2, array ...$_): ArrayedInterface; * @return bool true if the needle(s) is found else false */ public function keysExists(array $needles, bool $all = true): bool; + + public function toArray(): array; } diff --git a/src/Traits/ArrayPrefix.php b/src/Traits/ArrayPrefix.php index 0d9cb4f..3fb637e 100644 --- a/src/Traits/ArrayPrefix.php +++ b/src/Traits/ArrayPrefix.php @@ -64,6 +64,24 @@ public function diffKey(array $array2, array ...$_): ArrayedInterface return $this->setResult(array_diff_key($this->getWorkableItem(), $array2, ...$_)); } + public function walk(callable $callable, $arg = null) + { + $workableItem = $this->getWorkableItem(); + array_walk($workableItem, $callable, $arg); + + return $this->setResult($workableItem); + } + + + public function walkRecursive(callable $callable, $arg = null) + { + $workableItem = $this->getWorkableItem(); + array_walk_recursive($workableItem, $callable, $arg); + + return $this->setResult($workableItem); + } + + /** * Like php array_key_exists, this instead search if (one or more) keys exists in the array * @@ -82,7 +100,6 @@ public function keysExists(array $needles, bool $all = true): bool } /** - * @throws ArrayedException * @return ArrayedInterface|mixed */ public function head(bool $preserveKeys = false) diff --git a/tests/ArrayedTest.php b/tests/ArrayedTest.php index 0b39132..6dd2bf5 100644 --- a/tests/ArrayedTest.php +++ b/tests/ArrayedTest.php @@ -130,6 +130,15 @@ public function testMergeMethod() arrayed(['a' => 1, 'b' => 2]) ->merge(['z' => 26], ['c' => 2])() ); + + $names = arrayed(['cdd']); + $names = arrayed($names) + ->merge(['abc']); + + $this->assertEquals( + array_merge(['cdd'], ['abc']), + $names->toArray(), + ); } public function testAccessibleArray() From 2202bd7be62186af8c33a3f7888761197cef5046 Mon Sep 17 00:00:00 2001 From: Oluwatobi Omisakin Date: Tue, 30 Apr 2024 02:11:55 +0300 Subject: [PATCH 2/3] feat: Test walk and walk recursive --- src/Arrayed.php | 4 ++-- src/Traits/ArrayPrefix.php | 8 ++++++-- tests/ArrayPrefixTraitTest.php | 27 +++++++++++++++++++++++++++ 3 files changed, 35 insertions(+), 4 deletions(-) diff --git a/src/Arrayed.php b/src/Arrayed.php index f4d2153..66292f3 100644 --- a/src/Arrayed.php +++ b/src/Arrayed.php @@ -225,8 +225,8 @@ public function __toString(): string public function toArray(): array { return $this->walk( - function (&$value, $key) { - $value = $value instanceof ArrayedInterface ? $value->raw() : $value; + function ($value) { + return $value instanceof ArrayedInterface ? $value->getWorkableItem() : $value; } )->result(); } diff --git a/src/Traits/ArrayPrefix.php b/src/Traits/ArrayPrefix.php index 3fb637e..9b74f64 100644 --- a/src/Traits/ArrayPrefix.php +++ b/src/Traits/ArrayPrefix.php @@ -67,7 +67,9 @@ public function diffKey(array $array2, array ...$_): ArrayedInterface public function walk(callable $callable, $arg = null) { $workableItem = $this->getWorkableItem(); - array_walk($workableItem, $callable, $arg); + array_walk($workableItem, function (&$value, $key, $arg) use ($callable) { + $value = $callable($value, $key, $arg); + }, $arg); return $this->setResult($workableItem); } @@ -76,7 +78,9 @@ public function walk(callable $callable, $arg = null) public function walkRecursive(callable $callable, $arg = null) { $workableItem = $this->getWorkableItem(); - array_walk_recursive($workableItem, $callable, $arg); + array_walk_recursive($workableItem, function (&$value, $key, $arg) use ($callable) { + $value = $callable($value, $key, $arg); + }, $arg); return $this->setResult($workableItem); } diff --git a/tests/ArrayPrefixTraitTest.php b/tests/ArrayPrefixTraitTest.php index bb2f701..37f49ea 100644 --- a/tests/ArrayPrefixTraitTest.php +++ b/tests/ArrayPrefixTraitTest.php @@ -151,6 +151,33 @@ public function testDiffKey() ); } + public function testWalk(): void + { + $data = ['a' => 'b', 'c' => 'd']; + + $this->assertEquals( + ['a' => 'b-1', 'c' => 'd-1'], + arrayed($data)->walk(function ($value, $key) { + return $value . '-1'; + }) + ->result() + ); + } + + public function testWalkRecursive(): void + { + $data = ['a' => 'b', 'c' => ['d' => 'e']]; + + $this->assertEquals( + ['a' => 'b-1', 'c' => ['d' => 'e-1']], + arrayed($data) + ->walkRecursive(function ($value, $key) { + return $value . '-1'; + }) + ->result() + ); + } + public function testUnImplementedArrayPrefixFunction() { // array_combine From f216102f4863cd86cb22118d2325cf000d487d36 Mon Sep 17 00:00:00 2001 From: Oluwatobi Omisakin Date: Tue, 30 Apr 2024 02:12:19 +0300 Subject: [PATCH 3/3] feat: Test walk and walk recursive --- src/Arrayed.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Arrayed.php b/src/Arrayed.php index 66292f3..2a7e8eb 100644 --- a/src/Arrayed.php +++ b/src/Arrayed.php @@ -224,7 +224,7 @@ public function __toString(): string public function toArray(): array { - return $this->walk( + return $this->walkRecursive( function ($value) { return $value instanceof ArrayedInterface ? $value->getWorkableItem() : $value; }