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.7 changes (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
besologic authored Sep 8, 2016
1 parent 66022ef commit 17c6954
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 2 deletions.
8 changes: 7 additions & 1 deletion src/Illuminate/Support/Arr.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,13 @@ public static function exists($array, $key)
public static function first($array, callable $callback = null, $default = null)
{
if (is_null($callback)) {
return empty($array) ? value($default) : reset($array);
if (empty($array)) {
return value($default);
}

foreach ($array as $item) {
return $item;
}
}

foreach ($array as $key => $value) {
Expand Down
19 changes: 18 additions & 1 deletion src/Illuminate/Support/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public function median($key = null)
$values = with(isset($key) ? $this->pluck($key) : $this)
->sort()->values();

$middle = (int) floor($count / 2);
$middle = (int) ($count / 2);

if ($count % 2) {
return $values->get($middle);
Expand Down Expand Up @@ -920,6 +920,23 @@ public function slice($offset, $length = null)
return new static(array_slice($this->items, $offset, $length, true));
}

/**
* Split a collection into a certain number of groups.
*
* @param int $numberOfGroups
* @return static
*/
public function split($numberOfGroups)
{
if ($this->isEmpty()) {
return new static;
}

$groupSize = ceil($this->count() / $numberOfGroups);

return $this->chunk($groupSize);
}

/**
* Chunk the underlying collection array.
*
Expand Down
48 changes: 48 additions & 0 deletions tests/Support/SupportCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1554,6 +1554,54 @@ public function testCollectonFromTraversableWithKeys()
$collection = new Collection(new \ArrayObject(['foo' => 1, 'bar' => 2, 'baz' => 3]));
$this->assertEquals(['foo' => 1, 'bar' => 2, 'baz' => 3], $collection->toArray());
}

public function testSplitCollectionWithADivisableCount()
{
$collection = new Collection(['a', 'b', 'c', 'd']);

$this->assertEquals(
[['a', 'b'], ['c', 'd']],
$collection->split(2)->map(function (Collection $chunk) {
return $chunk->values()->toArray();
})->toArray()
);
}

public function testSplitCollectionWithAnUndivisableCount()
{
$collection = new Collection(['a', 'b', 'c']);

$this->assertEquals(
[['a', 'b'], ['c']],
$collection->split(2)->map(function (Collection $chunk) {
return $chunk->values()->toArray();
})->toArray()
);
}

public function testSplitCollectionWithCountLessThenDivisor()
{
$collection = new Collection(['a']);

$this->assertEquals(
[['a']],
$collection->split(2)->map(function (Collection $chunk) {
return $chunk->values()->toArray();
})->toArray()
);
}

public function testSplitEmptyCollection()
{
$collection = new Collection();

$this->assertEquals(
[],
$collection->split(2)->map(function (Collection $chunk) {
return $chunk->values()->toArray();
})->toArray()
);
}
}

class TestAccessorEloquentTestStub
Expand Down

0 comments on commit 17c6954

Please sign in to comment.