Skip to content

Commit

Permalink
Add Pack operation.
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Aug 26, 2020
1 parent 2d7ce6e commit 412734c
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 1 deletion.
24 changes: 23 additions & 1 deletion docs/pages/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,6 @@ Signature: ``Collection::with($data = [], ...$parameters);``
// With a resource/stream
$collection = Collection::with(fopen( __DIR__ . '/vendor/autoload.php', 'r'));
Methods (operations)
--------------------

Expand Down Expand Up @@ -832,6 +831,29 @@ Signature: ``Collection::only(...$keys);``
$collection = Collection::with(range(10, 100))
->only(3, 10, 'a', 9);
pack
~~~~

Wrap each items into an array containing 2 items: the key and the value.

Interface: `Packable`_

Signature: ``Collection::pack();``

.. code-block:: php
$input = ['a' => 'b', 'c' => 'd', 'e' => 'f'];
$c = Collection::fromIterable($input)
->pack();
// [
// ['a', 'b'],
// ['c', 'd'],
// ['e', 'f'],
// ]
pad
~~~

Expand Down
24 changes: 24 additions & 0 deletions spec/loophp/collection/CollectionSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -1312,6 +1312,30 @@ public function it_can_nullsy(): void
->shouldReturn(false);
}

public function it_can_pack(): void
{
$input = array_combine(range('a', 'c'), range('a', 'c'));

$this::fromIterable($input)
->pack()
->shouldIterateAs(
[
0 => [
0 => 'a',
1 => 'a',
],
1 => [
0 => 'b',
1 => 'b',
],
2 => [
0 => 'c',
1 => 'c',
],
]
);
}

public function it_can_pad(): void
{
$input = array_combine(range('A', 'E'), range('A', 'E'));
Expand Down
6 changes: 6 additions & 0 deletions src/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
use loophp\collection\Operation\Normalize;
use loophp\collection\Operation\Nth;
use loophp\collection\Operation\Only;
use loophp\collection\Operation\Pack;
use loophp\collection\Operation\Pad;
use loophp\collection\Operation\Pair;
use loophp\collection\Operation\Permutate;
Expand Down Expand Up @@ -512,6 +513,11 @@ public function only(...$keys): CollectionInterface
return $this->run(new Only(...$keys));
}

public function pack(): CollectionInterface
{
return $this->run(new Pack());
}

public function pad(int $size, $value): CollectionInterface
{
return $this->run(new Pad($size, $value));
Expand Down
20 changes: 20 additions & 0 deletions src/Contract/Operation/Packable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace loophp\collection\Contract\Operation;

use loophp\collection\Contract\Collection;

/**
* @psalm-template TKey
* @psalm-template TKey of array-key
* @psalm-template T
*/
interface Packable
{
/**
* @psalm-return \loophp\collection\Contract\Collection<int, array{0: TKey, 1: T}>
*/
public function pack(): Collection;
}
33 changes: 33 additions & 0 deletions src/Operation/Pack.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace loophp\collection\Operation;

use Closure;
use Generator;
use Iterator;
use loophp\collection\Contract\Operation;

/**
* @psalm-template TKey
* @psalm-template TKey of array-key
* @psalm-template T
*/
final class Pack extends AbstractOperation implements Operation
{
public function __invoke(): Closure
{
return
/**
* @psalm-param Iterator<TKey, T> $iterator
*
* @psalm-return Generator<int, array{0:TKey, 1:T}>
*/
static function (Iterator $iterator): Generator {
foreach ($iterator as $key => $value) {
yield [$key, $value];
}
};
}
}

0 comments on commit 412734c

Please sign in to comment.