From 7d5bce2658143340a9b1e0dd1cb5d67c28654dd3 Mon Sep 17 00:00:00 2001 From: Benson Lee Date: Fri, 13 Jan 2017 13:31:20 -0800 Subject: [PATCH] Laravel 5.3.29 changes (#30) --- src/Illuminate/Support/Collection.php | 23 +++++++++++------------ tests/Support/SupportCollectionTest.php | 16 +++++++++++++--- 2 files changed, 24 insertions(+), 15 deletions(-) diff --git a/src/Illuminate/Support/Collection.php b/src/Illuminate/Support/Collection.php index 587f62b..0c29db1 100644 --- a/src/Illuminate/Support/Collection.php +++ b/src/Illuminate/Support/Collection.php @@ -85,7 +85,7 @@ public function average($callback = null) * Get the median of a given key. * * @param null $key - * @return mixed|null + * @return mixed */ public function median($key = null) { @@ -859,7 +859,7 @@ public function random($amount = 1) * Reduce the collection to a single value. * * @param callable $callback - * @param mixed $initial + * @param mixed $initial * @return mixed */ public function reduce(callable $callback, $initial = null) @@ -900,7 +900,7 @@ public function reverse() * Search the collection for a given value and return the corresponding key if successful. * * @param mixed $value - * @param bool $strict + * @param bool $strict * @return mixed */ public function search($value, $strict = false) @@ -931,7 +931,7 @@ public function shift() /** * Shuffle the items in the collection. * - * @param int $seed + * @param int $seed * @return static */ public function shuffle($seed = null) @@ -954,8 +954,8 @@ public function shuffle($seed = null) /** * Slice the underlying collection array. * - * @param int $offset - * @param int $length + * @param int $offset + * @param int $length * @return static */ public function slice($offset, $length = null) @@ -983,7 +983,7 @@ public function split($numberOfGroups) /** * Chunk the underlying collection array. * - * @param int $size + * @param int $size * @return static */ public function chunk($size) @@ -1022,7 +1022,7 @@ public function sort(callable $callback = null) * Sort the collection using the given callback. * * @param callable|string $callback - * @param int $options + * @param int $options * @param bool $descending * @return static */ @@ -1133,7 +1133,6 @@ public function transform(callable $callback) * * @param string|callable|null $key * @param bool $strict - * * @return static */ public function unique($key = null, $strict = false) @@ -1142,12 +1141,12 @@ public function unique($key = null, $strict = false) return new static(array_unique($this->items, SORT_REGULAR)); } - $key = $this->valueRetriever($key); + $callback = $this->valueRetriever($key); $exists = []; - return $this->reject(function ($item) use ($key, $strict, &$exists) { - if (in_array($id = $key($item), $exists, $strict)) { + return $this->reject(function ($item, $key) use ($callback, $strict, &$exists) { + if (in_array($id = $callback($item, $key), $exists, $strict)) { return true; } diff --git a/tests/Support/SupportCollectionTest.php b/tests/Support/SupportCollectionTest.php index ca84d9b..39be602 100644 --- a/tests/Support/SupportCollectionTest.php +++ b/tests/Support/SupportCollectionTest.php @@ -556,9 +556,12 @@ public function testUnique() public function testUniqueWithCallback() { $c = new Collection([ - 1 => ['id' => 1, 'first' => 'Taylor', 'last' => 'Otwell'], 2 => ['id' => 2, 'first' => 'Taylor', 'last' => 'Otwell'], - 3 => ['id' => 3, 'first' => 'Abigail', 'last' => 'Otwell'], 4 => ['id' => 4, 'first' => 'Abigail', 'last' => 'Otwell'], - 5 => ['id' => 5, 'first' => 'Taylor', 'last' => 'Swift'], 6 => ['id' => 6, 'first' => 'Taylor', 'last' => 'Swift'], + 1 => ['id' => 1, 'first' => 'Taylor', 'last' => 'Otwell'], + 2 => ['id' => 2, 'first' => 'Taylor', 'last' => 'Otwell'], + 3 => ['id' => 3, 'first' => 'Abigail', 'last' => 'Otwell'], + 4 => ['id' => 4, 'first' => 'Abigail', 'last' => 'Otwell'], + 5 => ['id' => 5, 'first' => 'Taylor', 'last' => 'Swift'], + 6 => ['id' => 6, 'first' => 'Taylor', 'last' => 'Swift'], ]); $this->assertEquals([ @@ -573,6 +576,13 @@ public function testUniqueWithCallback() ], $c->unique(function ($item) { return $item['first'].$item['last']; })->all()); + + $this->assertEquals([ + 1 => ['id' => 1, 'first' => 'Taylor', 'last' => 'Otwell'], + 2 => ['id' => 2, 'first' => 'Taylor', 'last' => 'Otwell'], + ], $c->unique(function ($item, $key) { + return $key % 2; + })->all()); } public function testUniqueStrict()