diff --git a/src/Arrayed.php b/src/Arrayed.php index 6a2001d..2a7e8eb 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->walkRecursive( + function ($value) { + return $value instanceof ArrayedInterface ? $value->getWorkableItem() : $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..9b74f64 100644 --- a/src/Traits/ArrayPrefix.php +++ b/src/Traits/ArrayPrefix.php @@ -64,6 +64,28 @@ 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, function (&$value, $key, $arg) use ($callable) { + $value = $callable($value, $key, $arg); + }, $arg); + + return $this->setResult($workableItem); + } + + + public function walkRecursive(callable $callable, $arg = null) + { + $workableItem = $this->getWorkableItem(); + array_walk_recursive($workableItem, function (&$value, $key, $arg) use ($callable) { + $value = $callable($value, $key, $arg); + }, $arg); + + return $this->setResult($workableItem); + } + + /** * Like php array_key_exists, this instead search if (one or more) keys exists in the array * @@ -82,7 +104,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/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 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()