diff --git a/src/Illuminate/Support/Collection.php b/src/Illuminate/Support/Collection.php index 390d068..5aa6156 100644 --- a/src/Illuminate/Support/Collection.php +++ b/src/Illuminate/Support/Collection.php @@ -374,8 +374,8 @@ public function keyBy($keyBy) $results = []; - foreach ($this->items as $item) { - $results[$keyBy($item)] = $item; + foreach ($this->items as $key => $item) { + $results[$keyBy($item, $key)] = $item; } return new static($results); @@ -817,7 +817,6 @@ public function sort(callable $callback = null) $items = $this->items; $callback ? uasort($items, $callback) : uasort($items, function ($a, $b) { - if ($a == $b) { return 0; } diff --git a/tests/Support/SupportCollectionTest.php b/tests/Support/SupportCollectionTest.php index d347b7b..870a817 100644 --- a/tests/Support/SupportCollectionTest.php +++ b/tests/Support/SupportCollectionTest.php @@ -23,14 +23,18 @@ public function testLastReturnsLastItemInCollection() public function testLastWithCallback() { $data = new Collection([2, 4, 3, 2]); - $result = $data->last(function ($key, $value) { return $value > 2; }); + $result = $data->last(function ($key, $value) { + return $value > 2; + }); $this->assertEquals(3, $result); } public function testLastWithCallbackAndDefault() { $data = new Collection(['foo', 'bar']); - $result = $data->last(function ($key, $value) { return $value === 'baz'; }, 'default'); + $result = $data->last(function ($key, $value) { + return $value === 'baz'; + }, 'default'); $this->assertEquals('default', $result); } @@ -466,7 +470,9 @@ public function testUniqueWithCallback() 1 => ['id' => 1, 'first' => 'Taylor', 'last' => 'Otwell'], 3 => ['id' => 3, 'first' => 'Abigail', 'last' => 'Otwell'], 5 => ['id' => 5, 'first' => 'Taylor', 'last' => 'Swift'], - ], $c->unique(function ($item) { return $item['first'].$item['last']; })->all()); + ], $c->unique(function ($item) { + return $item['first'].$item['last']; + })->all()); } public function testCollapse() @@ -509,12 +515,16 @@ public function testSortWithCallback() public function testSortBy() { $data = new Collection(['taylor', 'dayle']); - $data = $data->sortBy(function ($x) { return $x; }); + $data = $data->sortBy(function ($x) { + return $x; + }); $this->assertEquals(['dayle', 'taylor'], array_values($data->all())); $data = new Collection(['dayle', 'taylor']); - $data = $data->sortByDesc(function ($x) { return $x; }); + $data = $data->sortByDesc(function ($x) { + return $x; + }); $this->assertEquals(['taylor', 'dayle'], array_values($data->all())); } @@ -655,8 +665,8 @@ public function testMacroable() // Foo() macro : unique values starting with A Collection::macro('foo', function () { return $this->filter(function ($item) { - return strpos($item, 'a') === 0; - }) + return strpos($item, 'a') === 0; + }) ->unique() ->values(); }); @@ -770,7 +780,9 @@ public function testGetPluckValueWithAccessors() public function testMap() { $data = new Collection(['first' => 'taylor', 'last' => 'otwell']); - $data = $data->map(function ($item, $key) { return $key.'-'.strrev($item); }); + $data = $data->map(function ($item, $key) { + return $key.'-'.strrev($item); + }); $this->assertEquals(['first' => 'first-rolyat', 'last' => 'last-llewto'], $data->all()); } @@ -780,28 +792,36 @@ public function testFlatMap() ['name' => 'taylor', 'hobbies' => ['programming', 'basketball']], ['name' => 'adam', 'hobbies' => ['music', 'powerlifting']], ]); - $data = $data->flatMap(function ($person) { return $person['hobbies']; }); + $data = $data->flatMap(function ($person) { + return $person['hobbies']; + }); $this->assertEquals(['programming', 'basketball', 'music', 'powerlifting'], $data->all()); } public function testTransform() { $data = new Collection(['first' => 'taylor', 'last' => 'otwell']); - $data->transform(function ($item, $key) { return $key.'-'.strrev($item); }); + $data->transform(function ($item, $key) { + return $key.'-'.strrev($item); + }); $this->assertEquals(['first' => 'first-rolyat', 'last' => 'last-llewto'], $data->all()); } public function testFirstWithCallback() { $data = new Collection(['foo', 'bar', 'baz']); - $result = $data->first(function ($key, $value) { return $value === 'bar'; }); + $result = $data->first(function ($key, $value) { + return $value === 'bar'; + }); $this->assertEquals('bar', $result); } public function testFirstWithCallbackAndDefault() { $data = new Collection(['foo', 'bar']); - $result = $data->first(function ($key, $value) { return $value === 'baz'; }, 'default'); + $result = $data->first(function ($key, $value) { + return $value === 'baz'; + }, 'default'); $this->assertEquals('default', $result); } @@ -929,7 +949,9 @@ public function testKeyByAttribute() $result = $data->keyBy('rating'); $this->assertEquals([1 => ['rating' => 1, 'name' => '1'], 2 => ['rating' => 2, 'name' => '2'], 3 => ['rating' => 3, 'name' => '3']], $result->all()); - $result = $data->keyBy(function ($item) { return $item['rating'] * 2; }); + $result = $data->keyBy(function ($item) { + return $item['rating'] * 2; + }); $this->assertEquals([2 => ['rating' => 1, 'name' => '1'], 4 => ['rating' => 2, 'name' => '2'], 6 => ['rating' => 3, 'name' => '3']], $result->all()); } @@ -939,12 +961,12 @@ public function testKeyByClosure() ['firstname' => 'Taylor', 'lastname' => 'Otwell', 'locale' => 'US'], ['firstname' => 'Lucas', 'lastname' => 'Michot', 'locale' => 'FR'], ]); - $result = $data->keyBy(function ($item) { - return strtolower($item['firstname'].$item['lastname']); + $result = $data->keyBy(function ($item, $key) { + return strtolower($key.'-'.$item['firstname'].$item['lastname']); }); $this->assertEquals([ - 'taylorotwell' => ['firstname' => 'Taylor', 'lastname' => 'Otwell', 'locale' => 'US'], - 'lucasmichot' => ['firstname' => 'Lucas', 'lastname' => 'Michot', 'locale' => 'FR'], + '0-taylorotwell' => ['firstname' => 'Taylor', 'lastname' => 'Otwell', 'locale' => 'US'], + '1-lucasmichot' => ['firstname' => 'Lucas', 'lastname' => 'Michot', 'locale' => 'FR'], ], $result->all()); } @@ -954,8 +976,12 @@ public function testContains() $this->assertTrue($c->contains(1)); $this->assertFalse($c->contains(2)); - $this->assertTrue($c->contains(function ($value) { return $value < 5; })); - $this->assertFalse($c->contains(function ($value) { return $value > 5; })); + $this->assertTrue($c->contains(function ($value) { + return $value < 5; + })); + $this->assertFalse($c->contains(function ($value) { + return $value > 5; + })); $c = new Collection([['v' => 1], ['v' => 3], ['v' => 5]]); @@ -975,7 +1001,9 @@ public function testGettingSumFromCollection() $this->assertEquals(100, $c->sum('foo')); $c = new Collection([(object) ['foo' => 50], (object) ['foo' => 50]]); - $this->assertEquals(100, $c->sum(function ($i) { return $i->foo; })); + $this->assertEquals(100, $c->sum(function ($i) { + return $i->foo; + })); } public function testCanSumValuesWithoutACallback() @@ -1027,7 +1055,9 @@ public function testRejectRemovesElementsPassingTruthTest() $this->assertEquals(['foo'], $c->reject('bar')->values()->all()); $c = new Collection(['foo', 'bar']); - $this->assertEquals(['foo'], $c->reject(function ($v) { return $v == 'bar'; })->values()->all()); + $this->assertEquals(['foo'], $c->reject(function ($v) { + return $v == 'bar'; + })->values()->all()); $c = new Collection(['foo', null]); $this->assertEquals(['foo'], $c->reject(null)->values()->all()); @@ -1036,7 +1066,9 @@ public function testRejectRemovesElementsPassingTruthTest() $this->assertEquals(['foo', 'bar'], $c->reject('baz')->values()->all()); $c = new Collection(['foo', 'bar']); - $this->assertEquals(['foo', 'bar'], $c->reject(function ($v) { return $v == 'baz'; })->values()->all()); + $this->assertEquals(['foo', 'bar'], $c->reject(function ($v) { + return $v == 'baz'; + })->values()->all()); $c = new Collection(['id' => 1, 'primary' => 'foo', 'secondary' => 'bar']); $this->assertEquals(['primary' => 'foo', 'secondary' => 'bar'], $c->reject(function ($item, $key) { @@ -1050,8 +1082,12 @@ public function testSearchReturnsIndexOfFirstFoundItem() $this->assertEquals(1, $c->search(2)); $this->assertEquals('foo', $c->search('bar')); - $this->assertEquals(4, $c->search(function ($value) { return $value > 4; })); - $this->assertEquals('foo', $c->search(function ($value) { return ! is_numeric($value); })); + $this->assertEquals(4, $c->search(function ($value) { + return $value > 4; + })); + $this->assertEquals('foo', $c->search(function ($value) { + return ! is_numeric($value); + })); } public function testSearchReturnsFalseWhenItemIsNotFound() @@ -1060,8 +1096,12 @@ public function testSearchReturnsFalseWhenItemIsNotFound() $this->assertFalse($c->search(6)); $this->assertFalse($c->search('foo')); - $this->assertFalse($c->search(function ($value) { return $value < 1 && is_numeric($value); })); - $this->assertFalse($c->search(function ($value) { return $value == 'nope'; })); + $this->assertFalse($c->search(function ($value) { + return $value < 1 && is_numeric($value); + })); + $this->assertFalse($c->search(function ($value) { + return $value == 'nope'; + })); } public function testKeys() @@ -1220,7 +1260,9 @@ public function testCombineWithCollection() public function testReduce() { $data = new Collection([1, 2, 3]); - $this->assertEquals(6, $data->reduce(function ($carry, $element) { return $carry += $element; })); + $this->assertEquals(6, $data->reduce(function ($carry, $element) { + return $carry += $element; + })); } /**