From 2d87017770e146662996453553f9825d22c69d3b Mon Sep 17 00:00:00 2001 From: iamdadmin Date: Tue, 31 Mar 2026 12:37:16 +0100 Subject: [PATCH 1/4] feat(support): with and without methods for manipulatesarray --- packages/support/src/Arr/ManipulatesArray.php | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/packages/support/src/Arr/ManipulatesArray.php b/packages/support/src/Arr/ManipulatesArray.php index d300c5bd7..83fbf7241 100644 --- a/packages/support/src/Arr/ManipulatesArray.php +++ b/packages/support/src/Arr/ManipulatesArray.php @@ -324,6 +324,32 @@ public function intersectKeys(array|self ...$arrays): self return $this->createOrModify(namespace\intersect_keys($this->value, ...$arrays)); } + /** + * Returns a new instance of the array, with only key=>value pairs from the first array where the key matches the values + * from the second array; a wrapper around intersectKeys with an array_flip for convenience. + * + * @param array|static ...$arrays + * + * @return static + */ + public function with(array|self ...$arrays): self + { + return $this->createOrModify(namespace\intersect_keys($this->value, array_flip(...$arrays))); + } + + /** + * Returns a new instance of the array, without the key=>value pairs from the first array where the key matches the values + * from the second array; a wrapper around diffKeys with an array_flip for convenience. + * + * @param array|static ...$arrays + * + * @return static + */ + public function without(array|self ...$arrays): self + { + return $this->createOrModify(namespace\diff_keys($this->value, array_flip(...$arrays))); + } + /** * Merges the array with the given arrays. * From 35303db437aec8a446cde3d954de5e76a9a807e0 Mon Sep 17 00:00:00 2001 From: iamdadmin Date: Tue, 31 Mar 2026 12:53:16 +0100 Subject: [PATCH 2/4] test(support): tests for with and without manipulatesarray methods --- .../tests/Arr/ManipulatesArrayTest.php | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/packages/support/tests/Arr/ManipulatesArrayTest.php b/packages/support/tests/Arr/ManipulatesArrayTest.php index f1150610e..66098a0aa 100644 --- a/packages/support/tests/Arr/ManipulatesArrayTest.php +++ b/packages/support/tests/Arr/ManipulatesArrayTest.php @@ -574,6 +574,42 @@ public function test_intersect_keys(): void $this->assertSame($expected, $current); } + public function test_with(): void + { + $collection = arr([ + 'first_name' => 'John', + 'last_name' => 'Doe', + 'age' => 42, + ]); + $current = $collection + ->with(['first_name', 'last_name']) + ->toArray(); + $expected = [ + 'first_name' => 'John', + 'last_name' => 'Doe', + ]; + + $this->assertSame($expected, $current); + } + + public function test_without(): void + { + $collection = arr([ + 'first_name' => 'John', + 'last_name' => 'Doe', + 'age' => 42, + ]); + $current = $collection + ->without(['age']) + ->toArray(); + $expected = [ + 'first_name' => 'John', + 'last_name' => 'Doe', + ]; + + $this->assertSame($expected, $current); + } + public function test_unique_with_basic_item(): void { $collection = arr([ From ba637c2098dd35e21cdcc160b3b57d388b0cee12 Mon Sep 17 00:00:00 2001 From: iamdadmin Date: Tue, 31 Mar 2026 18:14:58 +0100 Subject: [PATCH 3/4] refactor(support): adjust method names to onlyKeys and exceptKeys --- packages/support/src/Arr/ManipulatesArray.php | 4 ++-- packages/support/tests/Arr/ManipulatesArrayTest.php | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/support/src/Arr/ManipulatesArray.php b/packages/support/src/Arr/ManipulatesArray.php index 83fbf7241..a8f9852f9 100644 --- a/packages/support/src/Arr/ManipulatesArray.php +++ b/packages/support/src/Arr/ManipulatesArray.php @@ -332,7 +332,7 @@ public function intersectKeys(array|self ...$arrays): self * * @return static */ - public function with(array|self ...$arrays): self + public function onlyKeys(array|self ...$arrays): self { return $this->createOrModify(namespace\intersect_keys($this->value, array_flip(...$arrays))); } @@ -345,7 +345,7 @@ public function with(array|self ...$arrays): self * * @return static */ - public function without(array|self ...$arrays): self + public function exceptKeys(array|self ...$arrays): self { return $this->createOrModify(namespace\diff_keys($this->value, array_flip(...$arrays))); } diff --git a/packages/support/tests/Arr/ManipulatesArrayTest.php b/packages/support/tests/Arr/ManipulatesArrayTest.php index 66098a0aa..12ebbb0b6 100644 --- a/packages/support/tests/Arr/ManipulatesArrayTest.php +++ b/packages/support/tests/Arr/ManipulatesArrayTest.php @@ -574,7 +574,7 @@ public function test_intersect_keys(): void $this->assertSame($expected, $current); } - public function test_with(): void + public function test_only_keys(): void { $collection = arr([ 'first_name' => 'John', @@ -582,7 +582,7 @@ public function test_with(): void 'age' => 42, ]); $current = $collection - ->with(['first_name', 'last_name']) + ->onlyKeys(['first_name', 'last_name']) ->toArray(); $expected = [ 'first_name' => 'John', @@ -592,7 +592,7 @@ public function test_with(): void $this->assertSame($expected, $current); } - public function test_without(): void + public function test_except_keys(): void { $collection = arr([ 'first_name' => 'John', @@ -600,7 +600,7 @@ public function test_without(): void 'age' => 42, ]); $current = $collection - ->without(['age']) + ->exceptKeys(['age']) ->toArray(); $expected = [ 'first_name' => 'John', From 0312b8da725fa31a952386fc3f24bd29cb809f2a Mon Sep 17 00:00:00 2001 From: iamdadmin Date: Wed, 1 Apr 2026 10:35:24 +0100 Subject: [PATCH 4/4] refactor(support): change methods and tests to withKeys and withoutKeys --- packages/support/src/Arr/ManipulatesArray.php | 4 ++-- packages/support/tests/Arr/ManipulatesArrayTest.php | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/support/src/Arr/ManipulatesArray.php b/packages/support/src/Arr/ManipulatesArray.php index a8f9852f9..eb487e91e 100644 --- a/packages/support/src/Arr/ManipulatesArray.php +++ b/packages/support/src/Arr/ManipulatesArray.php @@ -332,7 +332,7 @@ public function intersectKeys(array|self ...$arrays): self * * @return static */ - public function onlyKeys(array|self ...$arrays): self + public function withKeys(array|self ...$arrays): self { return $this->createOrModify(namespace\intersect_keys($this->value, array_flip(...$arrays))); } @@ -345,7 +345,7 @@ public function onlyKeys(array|self ...$arrays): self * * @return static */ - public function exceptKeys(array|self ...$arrays): self + public function withoutKeys(array|self ...$arrays): self { return $this->createOrModify(namespace\diff_keys($this->value, array_flip(...$arrays))); } diff --git a/packages/support/tests/Arr/ManipulatesArrayTest.php b/packages/support/tests/Arr/ManipulatesArrayTest.php index 12ebbb0b6..eade280b6 100644 --- a/packages/support/tests/Arr/ManipulatesArrayTest.php +++ b/packages/support/tests/Arr/ManipulatesArrayTest.php @@ -574,7 +574,7 @@ public function test_intersect_keys(): void $this->assertSame($expected, $current); } - public function test_only_keys(): void + public function test_with_keys(): void { $collection = arr([ 'first_name' => 'John', @@ -582,7 +582,7 @@ public function test_only_keys(): void 'age' => 42, ]); $current = $collection - ->onlyKeys(['first_name', 'last_name']) + ->withKeys(['first_name', 'last_name']) ->toArray(); $expected = [ 'first_name' => 'John', @@ -592,7 +592,7 @@ public function test_only_keys(): void $this->assertSame($expected, $current); } - public function test_except_keys(): void + public function test_without_keys(): void { $collection = arr([ 'first_name' => 'John', @@ -600,7 +600,7 @@ public function test_except_keys(): void 'age' => 42, ]); $current = $collection - ->exceptKeys(['age']) + ->withoutKeys(['age']) ->toArray(); $expected = [ 'first_name' => 'John',