Skip to content
This repository has been archived by the owner on Mar 14, 2024. It is now read-only.

Commit

Permalink
Laravel 5.3.29 changes (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
besologic authored Jan 13, 2017
1 parent bb49526 commit 7d5bce2
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 15 deletions.
23 changes: 11 additions & 12 deletions src/Illuminate/Support/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -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)
Expand All @@ -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;
}

Expand Down
16 changes: 13 additions & 3 deletions tests/Support/SupportCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand All @@ -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()
Expand Down

0 comments on commit 7d5bce2

Please sign in to comment.