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

Backport chunk to v1.11 #38

Merged
merged 3 commits into from
Nov 18, 2024
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
25 changes: 25 additions & 0 deletions docs/collection-trait.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,17 @@ A fluent version of `append`. To do stuff like:
$collection->add($item1)->add($item2);
```

## chunk

```php
/** @return self[] */
public function chunk(int $size): array
```

This method [mirrors the behavior of `array_chunk`](https://www.php.net/manual/function.array-chunk.php) and returns a zero indexed numeric array of the current collection based on the chunk size provided.

Internally this method chunks the collection (by getting a copy with [getArrayCopy](https://www.php.net/manual/en/arrayiterator.getarraycopy.php) method) with the PHP [array_chunk](https://www.php.net/manual/function.array-chunk.php) function, returning a zero indexed array of collections of the same type as the initial one.

## unique

```php
Expand Down Expand Up @@ -70,6 +81,20 @@ Callable signature:
function(mixed $element, string|float|int|bool|null $elementKey): void;
```

## eachChunk

```php
public function eachChunk(int $size, callable $function): self
```

Internally, this method calls the [chunk](#chunk) method and then executes the passed anonymous function with each chunk.

Callable signature:

```php
function(CollectionInterface $collection): void;
```

## map

```php
Expand Down
24 changes: 24 additions & 0 deletions src/CollectionTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,21 @@ public function add($value): self
return $this;
}

/** @return self[] */
public function chunk(int $size): array
{
if ($size < 1) {
return [$this];
}

return array_map(
static function(array $chunk) {
return self::fromIterable($chunk);
},
array_chunk($this->getArrayCopy(), $size)
);
}

public function unique(): self
{
return static::fromIterable(array_unique($this->toArray(), SORT_REGULAR));
Expand Down Expand Up @@ -75,6 +90,15 @@ public function each(callable $function, bool $rewind = true): self
return $this;
}

public function eachChunk(int $size, callable $function): self
{
foreach ($this->chunk($size) as $chunk) {
$function($chunk);
}

return $this;
}

public function map(callable $function, bool $rewind = true): array
{
$map = [];
Expand Down
102 changes: 102 additions & 0 deletions tests/CollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,108 @@ public function fromIterableDataProvider(): array
];
}

/**
* @dataProvider chunkDataProvider
*/
public function testChunk(CollectionStub $collection, int $chunkSize, array $expectedChunks): void
{
$this->assertEquals($expectedChunks, $collection->chunk($chunkSize));
}

public static function chunkDataProvider(): array
{
return [
'chunk_size_0' => [
CollectionStub::fromIterable([1, 2, 3, 4, 5]),
0,
[
CollectionStub::fromIterable([1, 2, 3, 4, 5]),
],
],
'chunk_size_2' => [
CollectionStub::fromIterable([1, 2, 3, 4, 5]),
2,
[
CollectionStub::fromIterable([1, 2]),
CollectionStub::fromIterable([3, 4]),
CollectionStub::fromIterable([5]),
],
],
'chunk_size_1' => [
CollectionStub::fromIterable([1, 2, 3, 4, 5]),
1,
[
CollectionStub::fromIterable([1]),
CollectionStub::fromIterable([2]),
CollectionStub::fromIterable([3]),
CollectionStub::fromIterable([4]),
CollectionStub::fromIterable([5]),
],
],
'chunk_size_5' => [
CollectionStub::fromIterable([1, 2, 3, 4, 5]),
5,
[
CollectionStub::fromIterable([1, 2, 3, 4, 5]),
],
],
];
}

/**
* @dataProvider chunkEachDataProvider
*/
public function testChunkEach(CollectionStub $collection, int $chunkSize, array $expectedChunks): void
{
$i = 0;
$collection->eachChunk(
$chunkSize,
function(CollectionStub $collection) use (&$i, $expectedChunks): void {
$this->assertEquals($collection, $expectedChunks[$i++]);
}
);
}

public static function chunkEachDataProvider(): array
{
return [
'chunk_each_size_0' => [
CollectionStub::fromIterable([1, 2, 3, 4, 5]),
0,
[
CollectionStub::fromIterable([1, 2, 3, 4, 5]),
],
],
'chunk_each_size_2' => [
CollectionStub::fromIterable([1, 2, 3, 4, 5]),
2,
[
CollectionStub::fromIterable([1, 2]),
CollectionStub::fromIterable([3, 4]),
CollectionStub::fromIterable([5]),
],
],
'chunk_each_size_1' => [
CollectionStub::fromIterable([1, 2, 3, 4, 5]),
1,
[
CollectionStub::fromIterable([1]),
CollectionStub::fromIterable([2]),
CollectionStub::fromIterable([3]),
CollectionStub::fromIterable([4]),
CollectionStub::fromIterable([5]),
],
],
'chunk_each_size_5' => [
CollectionStub::fromIterable([1, 2, 3, 4, 5]),
5,
[
CollectionStub::fromIterable([1, 2, 3, 4, 5]),
],
],
];
}

public function testEmpty(): void
{
$collection = new CollectionStub();
Expand Down