From 4b9d3e78e6de2d7439eeecb3f1115e8048e64f90 Mon Sep 17 00:00:00 2001 From: Tim MacDonald Date: Fri, 12 Apr 2019 09:19:34 +1000 Subject: [PATCH] handle duplicates method in eloquent collections --- .../Database/Eloquent/Collection.php | 13 ++++++++++ src/Illuminate/Support/Collection.php | 26 ++++++++++++++----- .../DatabaseEloquentCollectionTest.php | 22 ++++++++++++++++ 3 files changed, 55 insertions(+), 6 deletions(-) diff --git a/src/Illuminate/Database/Eloquent/Collection.php b/src/Illuminate/Database/Eloquent/Collection.php index ca8cf852dc00..3720a140436d 100755 --- a/src/Illuminate/Database/Eloquent/Collection.php +++ b/src/Illuminate/Database/Eloquent/Collection.php @@ -305,6 +305,19 @@ public function diff($items) return $diff; } + /** + * Get the comparison function to detect duplicates. + * + * @param bool $strict + * @return \Closure + */ + protected function duplicateComparator($strict) + { + return function ($a, $b) { + return $a->is($b); + }; + } + /** * Intersect the collection with the given items. * diff --git a/src/Illuminate/Support/Collection.php b/src/Illuminate/Support/Collection.php index 0262fe1acce3..ca3fba0a9894 100644 --- a/src/Illuminate/Support/Collection.php +++ b/src/Illuminate/Support/Collection.php @@ -420,12 +420,7 @@ public function duplicates($callback = null, $strict = false) $uniqueItems = $items->unique(null, $strict); - $compare = $strict ? function ($a, $b) { - return $a === $b; - } - : function ($a, $b) { - return $a == $b; - }; + $compare = $this->duplicateComparator($strict); $duplicates = new static; @@ -451,6 +446,25 @@ public function duplicatesStrict($callback = null) return $this->duplicates($callback, true); } + /** + * Get the comparison function to detect duplicates. + * + * @param bool $strict + * @return \Closure + */ + protected function duplicateComparator($strict) + { + if ($strict) { + return function ($a, $b) { + return $a === $b; + }; + } + + return function ($a, $b) { + return $a == $b; + }; + } + /** * Execute a callback over each item. * diff --git a/tests/Database/DatabaseEloquentCollectionTest.php b/tests/Database/DatabaseEloquentCollectionTest.php index eb6fbd13882e..adf76f6d0f57 100755 --- a/tests/Database/DatabaseEloquentCollectionTest.php +++ b/tests/Database/DatabaseEloquentCollectionTest.php @@ -245,6 +245,28 @@ public function testCollectionDiffsWithGivenCollection() $this->assertEquals(new Collection([$one]), $c1->diff($c2)); } + public function testCollectionReturnsDuplicateBasedOnlyOnKeys() + { + $one = new TestEloquentCollectionModel(); + $two = new TestEloquentCollectionModel(); + $three = new TestEloquentCollectionModel(); + $four = new TestEloquentCollectionModel(); + $one->id = 1; + $one->someAttribute = '1'; + $two->id = 1; + $two->someAttribute = '2'; + $three->id = 1; + $three->someAttribute = '3'; + $four->id = 2; + $four->someAttribute = '4'; + + $duplicates = Collection::make([$one, $two, $three, $four])->duplicates()->all(); + $this->assertSame([1 => $two, 2 => $three], $duplicates); + + $duplicates = Collection::make([$one, $two, $three, $four])->duplicatesStrict()->all(); + $this->assertSame([1 => $two, 2 => $three], $duplicates); + } + public function testCollectionIntersectsWithGivenCollection() { $one = m::mock(Model::class);