Skip to content

Commit

Permalink
[5.4] Create a new "every" method on the collection (#16777)
Browse files Browse the repository at this point in the history
* Rename the "every" method to "nth"

* Create a new "every" method on the collection

* Add "every" to higher order proxies

* Allow passing a single key argument to "every"
  • Loading branch information
JosephSilber authored and taylorotwell committed Dec 14, 2016
1 parent 5a3d722 commit 77ee214
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 26 deletions.
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 @@ -250,27 +250,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 @@ -722,6 +729,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

0 comments on commit 77ee214

Please sign in to comment.