Skip to content
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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "Collections library for php language",
"minimum-stability": "dev",
"license": "MIT",
"version": "1.1.5",
"version": "1.1.6",
"authors": [
{
"name": "Maxim Sokolovsky",
Expand Down
25 changes: 8 additions & 17 deletions src/WS/Utils/Collections/Functions/Reorganizers.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,23 +90,14 @@ public static function random(int $count = 1): Closure
public static function chunk(int $size): Closure
{
return static function (Collection $collection) use ($size): Collection {
$chunkCollection = self::collectionConstructor();
$currentChunk = self::collectionConstructor();
$pointer = $size;
$collection
->stream()
->each(static function ($el) use ($size, $chunkCollection, & $currentChunk, & $pointer) {
$pointer--;
$currentChunk->add($el);

if ($pointer === 0) {
$chunkCollection->add($currentChunk);
$currentChunk = self::collectionConstructor();
$pointer = $size;
}
})
;
return $chunkCollection;
$array = $collection->toArray();
$chunkedArray = array_chunk($array, $size);
$result = self::collectionConstructor();
foreach ($chunkedArray as $items) {
$result->add(self::collectionConstructor($items));
};

return $result;
};
}

Expand Down
17 changes: 17 additions & 0 deletions tests/WS/Utils/Collections/ReorganizersFunctionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,23 @@ public function chunking(): void
$this->assertThat($chunkedCollection->stream()->findLast(), CollectionIsEqual::to([5, 6]));
}

/**
* @test
* @return void
*/
public function chunkingWhitTail(): void
{
$collection = self::toCollection(1, 2, 3, 4, 5);
$chunkedCollection = $collection
->stream()
->reorganize(Reorganizers::chunk(2))
->getCollection()
;
$this->assertEquals(3, $chunkedCollection->size());
$this->assertThat($chunkedCollection->stream()->findFirst(), CollectionIsEqual::to([1, 2]));
$this->assertThat($chunkedCollection->stream()->findLast(), CollectionIsEqual::to([5]));
}

/**
* @test
*/
Expand Down