Skip to content

Commit

Permalink
Add Wrap and Unwrap operations.
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Jul 6, 2020
1 parent 9b77e0e commit 0bd0e42
Show file tree
Hide file tree
Showing 8 changed files with 149 additions and 0 deletions.
34 changes: 34 additions & 0 deletions docs/pages/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -912,6 +912,22 @@ Signature: ``Collection::until(callable ...$callbacks);``
return 1 === $number;
});
unwrap
~~~~~~

Unwrap every collection element.

Interface: `Unwrapable`_

Signature: ``Collection::unwrap();``

.. code-block:: php
$data = [['a' => 'A'], ['b' => 'B'], ['c' => 'C']];
$collection = Collection::with($data)
->unwrap();
walk
~~~~

Expand Down Expand Up @@ -949,6 +965,22 @@ Signature: ``Collection::window(int ...$length);``
->window(2, 3)
->all();
wrap
~~~~

Wrap every element into an array.

Interface: `Wrapable`_

Signature: ``Collection::wrap();``

.. code-block:: php
$data = ['a' => 'A', 'b' => 'B', 'c' => 'C'];
$collection = Collection::with($data)
->wrap();
zip
~~~

Expand Down Expand Up @@ -1099,6 +1131,8 @@ Interface: `Truthyable`_
.. _Transposeable: https://github.com/loophp/collection/blob/master/src/Contract/Operation/Transposeable.php
.. _Truthyable: https://github.com/loophp/collection/blob/master/src/Contract/Transformation/Truthyable.php
.. _Untilable: https://github.com/loophp/collection/blob/master/src/Contract/Operation/Untilable.php
.. _Unwrapable: https://github.com/loophp/collection/blob/master/src/Contract/Operation/Unwrapable.php
.. _Walkable: https://github.com/loophp/collection/blob/master/src/Contract/Operation/Walkable.php
.. _Windowable: https://github.com/loophp/collection/blob/master/src/Contract/Operation/Windowable.php
.. _Wrapable: https://github.com/loophp/collection/blob/master/src/Contract/Operation/Wrapable.php
.. _Zipable: https://github.com/loophp/collection/blob/master/src/Contract/Operation/Zipable.php
31 changes: 31 additions & 0 deletions spec/loophp/collection/CollectionSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -1599,6 +1599,26 @@ public function it_can_until(): void
->shouldIterateAs([5, 16, 8, 4, 2, 1]);
}

public function it_can_unwrap()
{
$this::with([['a' => 'A'], ['b' => 'B'], ['c' => 'C']])
->unwrap()
->shouldIterateAs([
'a' => 'A',
'b' => 'B',
'c' => 'C',
]);

$this::fromIterable(['foo' => ['a' => 'A'], 'bar' => ['b' => 'B'], 'foobar' => ['c' => 'C', 'd' => 'D']])
->unwrap()
->shouldIterateAs([
'a' => 'A',
'b' => 'B',
'c' => 'C',
'd' => 'D',
]);
}

public function it_can_use_range(): void
{
$this
Expand Down Expand Up @@ -1824,6 +1844,17 @@ public function it_can_window(): void
]);
}

public function it_can_wrap()
{
$this::with(['a' => 'A', 'b' => 'B', 'c' => 'C'])
->wrap()
->shouldIterateAs([
['a' => 'A'],
['b' => 'B'],
['c' => 'C'],
]);
}

public function it_can_zip(): void
{
$this
Expand Down
12 changes: 12 additions & 0 deletions src/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,10 @@
use loophp\collection\Operation\Times;
use loophp\collection\Operation\Transpose;
use loophp\collection\Operation\Until;
use loophp\collection\Operation\Unwrap;
use loophp\collection\Operation\Walk;
use loophp\collection\Operation\Window;
use loophp\collection\Operation\Wrap;
use loophp\collection\Operation\Zip;
use loophp\collection\Transformation\All;
use loophp\collection\Transformation\Contains;
Expand Down Expand Up @@ -444,6 +446,11 @@ public function until(callable ...$callbacks): BaseInterface
return $this->run(new Until(...$callbacks));
}

public function unwrap(): BaseInterface
{
return $this->run(new Unwrap());
}

public function walk(callable ...$callbacks): BaseInterface
{
return $this->run(new Walk(...$callbacks));
Expand All @@ -462,6 +469,11 @@ public static function with($data = [], ...$parameters): CollectionInterface
return new self($data, ...$parameters);
}

public function wrap(): BaseInterface
{
return $this->run(new Wrap());
}

/**
* {@inheritdoc}
*/
Expand Down
4 changes: 4 additions & 0 deletions src/Contract/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,10 @@
use loophp\collection\Contract\Operation\Timesable;
use loophp\collection\Contract\Operation\Transposeable;
use loophp\collection\Contract\Operation\Untilable;
use loophp\collection\Contract\Operation\Unwrapable;
use loophp\collection\Contract\Operation\Walkable;
use loophp\collection\Contract\Operation\Windowable;
use loophp\collection\Contract\Operation\Wrapable;
use loophp\collection\Contract\Operation\Zipable;
use loophp\collection\Contract\Transformation\Allable;
use loophp\collection\Contract\Transformation\Containsable;
Expand Down Expand Up @@ -126,8 +128,10 @@ interface Collection extends
Transposeable,
Truthyable,
Untilable,
Unwrapable,
Walkable,
Windowable,
Wrapable,
Zipable
{
/**
Expand Down
12 changes: 12 additions & 0 deletions src/Contract/Operation/Unwrapable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace loophp\collection\Contract\Operation;

use loophp\collection\Contract\Base;

interface Unwrapable
{
public function unwrap(): Base;
}
12 changes: 12 additions & 0 deletions src/Contract/Operation/Wrapable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace loophp\collection\Contract\Operation;

use loophp\collection\Contract\Base;

interface Wrapable
{
public function wrap(): Base;
}
23 changes: 23 additions & 0 deletions src/Operation/Unwrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace loophp\collection\Operation;

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

final class Unwrap extends AbstractOperation implements Operation
{
public function __invoke(): Closure
{
return static function (iterable $collection): Generator {
foreach ($collection as $key => $value) {
foreach ((array) $value as $k => $v) {
yield $k => $v;
}
}
};
}
}
21 changes: 21 additions & 0 deletions src/Operation/Wrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace loophp\collection\Operation;

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

final class Wrap extends AbstractOperation implements Operation
{
public function __invoke(): Closure
{
return static function (iterable $collection): Generator {
foreach ($collection as $key => $value) {
yield [$key => $value];
}
};
}
}

0 comments on commit 0bd0e42

Please sign in to comment.