From 96471853c03654a9f5b3eb691044e460a2ed3090 Mon Sep 17 00:00:00 2001 From: Arif Hoque Date: Thu, 22 Jan 2026 15:03:15 +0600 Subject: [PATCH 1/3] diff() collection method added --- src/Phaseolies/Support/Collection.php | 20 ++++++++++++++++++++ tests/CollectionTest.php | 21 +++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/src/Phaseolies/Support/Collection.php b/src/Phaseolies/Support/Collection.php index d59e10a..c4262d5 100644 --- a/src/Phaseolies/Support/Collection.php +++ b/src/Phaseolies/Support/Collection.php @@ -761,4 +761,24 @@ public function partition(callable $callback): array new static($this->model, $failed) ]; } + + /** + * Get items not present in the given items. + * + * @param mixed $items + * @return static + */ + public function diff($items): self + { + $compare = $items instanceof self ? $items->all() : (array) $items; + + $result = []; + foreach ($this->data as $item) { + if (!in_array($item, $compare, true)) { + $result[] = $item; + } + } + + return new static($this->model, $result); + } } diff --git a/tests/CollectionTest.php b/tests/CollectionTest.php index 4c1cdab..1492843 100644 --- a/tests/CollectionTest.php +++ b/tests/CollectionTest.php @@ -1315,4 +1315,25 @@ public function testPartitionWithObjects() $this->assertCount(2, $active); $this->assertCount(1, $inactive); } + + public function testDiffReturnsItemsNotInOtherCollection() + { + $modelClass = get_class($this->makeTestModel(0, "")); + $collection1 = new Collection($modelClass, [1, 2, 3, 4, 5]); + $collection2 = new Collection($modelClass, [3, 4, 5, 6, 7]); + + $diff = $collection1->diff($collection2); + + $this->assertEquals([1, 2], $diff->all()); + } + + public function testDiffWithArray() + { + $modelClass = get_class($this->makeTestModel(0, "")); + $collection = new Collection($modelClass, [1, 2, 3, 4, 5]); + + $diff = $collection->diff([3, 4, 5]); + + $this->assertEquals([1, 2], $diff->all()); + } } From e329b499ea44727fc7c2c46a089e35b7639c31c2 Mon Sep 17 00:00:00 2001 From: Arif Hoque Date: Thu, 22 Jan 2026 15:05:23 +0600 Subject: [PATCH 2/3] intersect() collection method added --- src/Phaseolies/Support/Collection.php | 20 ++++++++++++++++++++ tests/CollectionTest.php | 21 +++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/src/Phaseolies/Support/Collection.php b/src/Phaseolies/Support/Collection.php index c4262d5..5d2e9f2 100644 --- a/src/Phaseolies/Support/Collection.php +++ b/src/Phaseolies/Support/Collection.php @@ -781,4 +781,24 @@ public function diff($items): self return new static($this->model, $result); } + + /** + * Get items present in both this collection and given items + * + * @param mixed $items + * @return static + */ + public function intersect($items): self + { + $compare = $items instanceof self ? $items->all() : (array) $items; + + $result = []; + foreach ($this->data as $item) { + if (in_array($item, $compare, true)) { + $result[] = $item; + } + } + + return new static($this->model, $result); + } } diff --git a/tests/CollectionTest.php b/tests/CollectionTest.php index 1492843..504e375 100644 --- a/tests/CollectionTest.php +++ b/tests/CollectionTest.php @@ -1336,4 +1336,25 @@ public function testDiffWithArray() $this->assertEquals([1, 2], $diff->all()); } + + public function testIntersectReturnsCommonItems() + { + $modelClass = get_class($this->makeTestModel(0, "")); + $collection1 = new Collection($modelClass, [1, 2, 3, 4, 5]); + $collection2 = new Collection($modelClass, [3, 4, 5, 6, 7]); + + $intersect = $collection1->intersect($collection2); + + $this->assertEquals([3, 4, 5], $intersect->all()); + } + + public function testIntersectWithArray() + { + $modelClass = get_class($this->makeTestModel(0, "")); + $collection = new Collection($modelClass, [1, 2, 3, 4, 5]); + + $intersect = $collection->intersect([2, 3, 6]); + + $this->assertEquals([2, 3], $intersect->all()); + } } From 8a656d580e6a7f9c86062b511b89a71cac24f30d Mon Sep 17 00:00:00 2001 From: Arif Hoque Date: Thu, 22 Jan 2026 15:06:27 +0600 Subject: [PATCH 3/3] tap() collection method added --- src/Phaseolies/Support/Collection.php | 13 +++++++++++++ tests/CollectionTest.php | 15 +++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/src/Phaseolies/Support/Collection.php b/src/Phaseolies/Support/Collection.php index 5d2e9f2..9134b09 100644 --- a/src/Phaseolies/Support/Collection.php +++ b/src/Phaseolies/Support/Collection.php @@ -801,4 +801,17 @@ public function intersect($items): self return new static($this->model, $result); } + + /** + * Execute a callback over the collection without modifying it. + * + * @param callable $callback + * @return $this + */ + public function tap(callable $callback): self + { + $callback($this); + + return $this; + } } diff --git a/tests/CollectionTest.php b/tests/CollectionTest.php index 504e375..b46519c 100644 --- a/tests/CollectionTest.php +++ b/tests/CollectionTest.php @@ -1357,4 +1357,19 @@ public function testIntersectWithArray() $this->assertEquals([2, 3], $intersect->all()); } + + public function testTapExecutesCallbackWithoutModifying() + { + $modelClass = get_class($this->makeTestModel(0, "")); + $collection = new Collection($modelClass, [1, 2, 3]); + + $tapped = null; + $result = $collection->tap(function ($col) use (&$tapped) { + $tapped = $col->count(); + }); + + $this->assertEquals(3, $tapped); + $this->assertSame($collection, $result); + $this->assertEquals([1, 2, 3], $collection->all()); + } }