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.27 changes (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
besologic authored Dec 15, 2016
1 parent bd8b80d commit 726d0cf
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 31 deletions.
35 changes: 32 additions & 3 deletions src/Illuminate/Support/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public function median($key = null)
/**
* Get the mode of a given key.
*
* @param null $key
* @param mixed $key
* @return array
*/
public function mode($key = null)
Expand Down Expand Up @@ -545,6 +545,16 @@ public function isEmpty()
return empty($this->items);
}

/**
* Determine if the collection is not empty.
*
* @return bool
*/
public function isNotEmpty()
{
return ! $this->isEmpty();
}

/**
* Determine if the given value is callable, but not a string.
*
Expand Down Expand Up @@ -639,7 +649,7 @@ public function max($callback = null)
{
$callback = $this->valueRetriever($callback);

return $this->reduce(function ($result, $item) use ($callback) {
return $this->filter()->reduce(function ($result, $item) use ($callback) {
$value = $callback($item);

return is_null($result) || $value > $result ? $value : $result;
Expand Down Expand Up @@ -689,7 +699,7 @@ public function min($callback = null)
{
$callback = $this->valueRetriever($callback);

return $this->reduce(function ($result, $item) use ($callback) {
return $this->filter()->reduce(function ($result, $item) use ($callback) {
$value = $callback($item);

return is_null($result) || $value < $result ? $value : $result;
Expand Down Expand Up @@ -725,6 +735,25 @@ public function forPage($page, $perPage)
return $this->slice(($page - 1) * $perPage, $perPage);
}

/**
* Partition the collection into two arrays using the given callback or key.
*
* @param callable|string $callback
* @return static
*/
public function partition($callback)
{
$partitions = [new static, new static];

$callback = $this->valueRetriever($callback);

foreach ($this->items as $key => $item) {
$partitions[(int) ! $callback($item)][$key] = $item;
}

return new static($partitions);
}

/**
* Pass the collection to the given callback and return the result.
*
Expand Down
113 changes: 85 additions & 28 deletions tests/Support/SupportCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,34 @@ public function testFirstReturnsFirstItemInCollection()
$this->assertEquals('foo', $c->first());
}

public function testFirstWithCallback()
{
$data = new Collection(['foo', 'bar', 'baz']);
$result = $data->first(function ($value) {
return $value === 'bar';
});
$this->assertEquals('bar', $result);
}

public function testFirstWithCallbackAndDefault()
{
$data = new Collection(['foo', 'bar']);
$result = $data->first(function ($value) {
return $value === 'baz';
}, 'default');
$this->assertEquals('default', $result);
}

public function testFirstWithDefaultAndWithoutCallback()
{
$data = new Collection;
$result = $data->first(null, 'default');
$this->assertEquals('default', $result);
}

public function testLastReturnsLastItemInCollection()
{
$c = new Collection(['foo', 'bar']);

$this->assertEquals('bar', $c->last());
}

Expand Down Expand Up @@ -72,6 +96,14 @@ public function testEmptyCollectionIsEmpty()
$this->assertTrue($c->isEmpty());
}

public function testEmptyCollectionIsNotEmpty()
{
$c = new Collection(['foo', 'bar']);

$this->assertFalse($c->isEmpty());
$this->assertTrue($c->isNotEmpty());
}

public function testCollectionIsConstructed()
{
$collection = new Collection('foo');
Expand Down Expand Up @@ -957,31 +989,6 @@ public function testTransform()
$this->assertEquals(['first' => 'first-rolyat', 'last' => 'last-llewto'], $data->all());
}

public function testFirstWithCallback()
{
$data = new Collection(['foo', 'bar', 'baz']);
$result = $data->first(function ($value) {
return $value === 'bar';
});
$this->assertEquals('bar', $result);
}

public function testFirstWithCallbackAndDefault()
{
$data = new Collection(['foo', 'bar']);
$result = $data->first(function ($value) {
return $value === 'baz';
}, 'default');
$this->assertEquals('default', $result);
}

public function testFirstWithDefaultAndWithoutCallback()
{
$data = new Collection;
$result = $data->first(null, 'default');
$this->assertEquals('default', $result);
}

public function testGroupByAttribute()
{
$data = new Collection([['rating' => 1, 'url' => '1'], ['rating' => 1, 'url' => '1'], ['rating' => 2, 'url' => '2']]);
Expand Down Expand Up @@ -1367,6 +1374,9 @@ public function testGettingMinItemsFromCollection()
$c = new Collection([1, 2, 3, 4, 5]);
$this->assertEquals(1, $c->min());

$c = new Collection([1, null, 3, 4, 5]);
$this->assertEquals(1, $c->min());

$c = new Collection();
$this->assertNull($c->min());
}
Expand Down Expand Up @@ -1581,13 +1591,13 @@ public function testSliceNegativeOffsetAndNegativeLength()
$this->assertEquals([3, 4, 5, 6], $collection->slice(-6, -2)->values()->toArray());
}

public function testCollectonFromTraversable()
public function testCollectionFromTraversable()
{
$collection = new Collection(new \ArrayObject([1, 2, 3]));
$this->assertEquals([1, 2, 3], $collection->toArray());
}

public function testCollectonFromTraversableWithKeys()
public function testCollectionFromTraversableWithKeys()
{
$collection = new Collection(new \ArrayObject(['foo' => 1, 'bar' => 2, 'baz' => 3]));
$this->assertEquals(['foo' => 1, 'bar' => 2, 'baz' => 3], $collection->toArray());
Expand Down Expand Up @@ -1640,6 +1650,53 @@ public function testSplitEmptyCollection()
})->toArray()
);
}

public function testPartitionWithClosure()
{
$collection = new Collection(range(1, 10));

list($firstPartition, $secondPartition) = $collection->partition(function ($i) {
return $i <= 5;
});

$this->assertEquals([1, 2, 3, 4, 5], $firstPartition->values()->toArray());
$this->assertEquals([6, 7, 8, 9, 10], $secondPartition->values()->toArray());
}

public function testPartitionByKey()
{
$courses = new Collection([
['free' => true, 'title' => 'Basic'], ['free' => false, 'title' => 'Premium'],
]);

list($free, $premium) = $courses->partition('free');

$this->assertSame([['free' => true, 'title' => 'Basic']], $free->values()->toArray());

$this->assertSame([['free' => false, 'title' => 'Premium']], $premium->values()->toArray());
}

public function testPartitionPreservesKeys()
{
$courses = new Collection([
'a' => ['free' => true], 'b' => ['free' => false], 'c' => ['free' => true],
]);

list($free, $premium) = $courses->partition('free');

$this->assertSame(['a' => ['free' => true], 'c' => ['free' => true]], $free->toArray());

$this->assertSame(['b' => ['free' => false]], $premium->toArray());
}

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

$this->assertCount(2, $collection->partition(function () {
return true;
}));
}
}

class TestAccessorEloquentTestStub
Expand Down

0 comments on commit 726d0cf

Please sign in to comment.