Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.4] Create a new "every" method on the collection #16777

Merged
merged 4 commits into from
Dec 14, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 45 additions & 14 deletions src/Illuminate/Support/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class Collection implements ArrayAccess, Arrayable, Countable, IteratorAggregate
* @var array
*/
protected static $proxies = [
'each', 'filter', 'first', 'map', 'partition',
'each', 'every', 'filter', 'first', 'map', 'partition',
'reject', 'sortBy', 'sortByDesc', 'sum',
];

Expand Down Expand Up @@ -245,27 +245,34 @@ public function each(callable $callback)
}

/**
* Create a new collection consisting of every n-th element.
* Determine if all items in the collection pass the given test.
*
* @param int $step
* @param int $offset
* @return static
* @param string|callable $key
* @param mixed $operator
* @param mixed $value
* @return bool
*/
public function every($step, $offset = 0)
public function every($key, $operator = null, $value = null)
{
$new = [];

$position = 0;
if (func_num_args() == 1) {
$callback = $this->valueRetriever($key);

foreach ($this->items as $item) {
if ($position % $step === $offset) {
$new[] = $item;
foreach ($this->items as $k => $v) {
if (! $callback($v, $k)) {
return false;
}
}

$position++;
return true;
}

return new static($new);
if (func_num_args() == 2) {
$value = $operator;

$operator = '=';
}

return $this->every($this->operatorForWhere($key, $operator, $value));
}

/**
Expand Down Expand Up @@ -717,6 +724,30 @@ public function min($callback = null)
});
}

/**
* Create a new collection consisting of every n-th element.
*
* @param int $step
* @param int $offset
* @return static
*/
public function nth($step, $offset = 0)
{
$new = [];

$position = 0;

foreach ($this->items as $item) {
if ($position % $step === $offset) {
$new[] = $item;
}

$position++;
}

return new static($new);
}

/**
* Get the items with the specified keys.
*
Expand Down
53 changes: 41 additions & 12 deletions tests/Support/SupportCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -728,19 +728,31 @@ public function testChunkWhenGivenLessThanZero()

public function testEvery()
{
$data = new Collection([
6 => 'a',
4 => 'b',
7 => 'c',
1 => 'd',
5 => 'e',
3 => 'f',
]);
$c = new Collection([]);
$this->assertTrue($c->every('key', 'value'));
$this->assertTrue($c->every(function () {
return false;
}));

$this->assertEquals(['a', 'e'], $data->every(4)->all());
$this->assertEquals(['b', 'f'], $data->every(4, 1)->all());
$this->assertEquals(['c'], $data->every(4, 2)->all());
$this->assertEquals(['d'], $data->every(4, 3)->all());
$c = new Collection([['age' => 18], ['age' => 20], ['age' => 20]]);
$this->assertFalse($c->every('age', 18));
$this->assertTrue($c->every('age', '>=', 18));
$this->assertTrue($c->every(function ($item) {
return $item['age'] >= 18;
}));
$this->assertFalse($c->every(function ($item) {
return $item['age'] >= 20;
}));

$c = new Collection([null, null]);
$this->assertTrue($c->every(function ($item) {
return $item === null;
}));

$c = new Collection([['active' => true], ['active' => true]]);
$this->assertTrue($c->every('active'));
$this->assertTrue($c->every->active);
$this->assertFalse($c->push(['active' => false])->every->active);
}

public function testExcept()
Expand Down Expand Up @@ -1042,6 +1054,23 @@ public function testMapWithKeysCallbackKey()
);
}

public function testNth()
{
$data = new Collection([
6 => 'a',
4 => 'b',
7 => 'c',
1 => 'd',
5 => 'e',
3 => 'f',
]);

$this->assertEquals(['a', 'e'], $data->nth(4)->all());
$this->assertEquals(['b', 'f'], $data->nth(4, 1)->all());
$this->assertEquals(['c'], $data->nth(4, 2)->all());
$this->assertEquals(['d'], $data->nth(4, 3)->all());
}

public function testTransform()
{
$data = new Collection(['first' => 'taylor', 'last' => 'otwell']);
Expand Down