Skip to content

Commit

Permalink
Add ArrayCollection::chunk() function
Browse files Browse the repository at this point in the history
  • Loading branch information
mahagr committed Sep 15, 2017
1 parent 37b5818 commit 588c509
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,15 @@ public function shuffle()
return $this->collection->shuffle();
}

/**
* {@inheritDoc}
*/
public function chunk($size)
{
$this->initialize();
return $this->collection->chunk($size);
}

/**
* {@inheritDoc}
*/
Expand Down
11 changes: 11 additions & 0 deletions system/src/Grav/Framework/Collection/ArrayCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,17 @@ public function shuffle()
return $this->createFrom(array_replace(array_flip($keys), $this->toArray()));
}

/**
* Split collection into chunks.
*
* @param int $size Size of each chunk.
* @return array
*/
public function chunk($size)
{
return array_chunk($this->toArray(), $size, true);
}

/**
* Implementes JsonSerializable interface.
*
Expand Down
8 changes: 8 additions & 0 deletions system/src/Grav/Framework/Collection/CollectionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,12 @@ public function reverse();
* @return static
*/
public function shuffle();

/**
* Split collection into chunks.
*
* @param int $size Size of each chunk.
* @return array
*/
public function chunk($size);
}

0 comments on commit 588c509

Please sign in to comment.