Skip to content

Commit

Permalink
Add the Explode operation.
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Sep 9, 2019
1 parent b2007be commit e21e84f
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,8 @@ the methods always return the same values for the same inputs.
| `collapse` | new Collection object | [Collapse.php](./src/Operation/Collapse.php)
| `combine` | new Collection object | [Combine.php](./src/Operation/Combine.php)
| `count` | int | [Count.php](./src/Operation/Count.php)
| `distinct` | new Collection object | [Distinct.php](./src/Operation/Distinct.php)
| `explode` | new Collection object | [Explode.php](./src/Operation/Explode.php)
| `filter` | new Collection object | [Filter.php](./src/Operation/Filter.php)
| `first` | mixed | [First.php](./src/Operation/First.php)
| `flatten` | new Collection object | [Flatten.php](./src/Operation/Flatten.php)
Expand Down
59 changes: 59 additions & 0 deletions spec/drupol/collection/CollectionSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,65 @@ public function it_can_distinct(): void
->shouldIterateAs([0 => 1, 2 => 2, 4 => 3, 6 => $stdclass]);
}

public function it_can_explode(): void
{
$string = 'I am just a random piece of text.';

$this
->beConstructedThrough('with', [$string]);

$this
->explode('o')
->map(
static function ($item) {
return \iterator_to_array($item);
}
)
->shouldIterateAs(
[
0 => [
0 => 'I',
1 => ' ',
2 => 'a',
3 => 'm',
4 => ' ',
5 => 'j',
6 => 'u',
7 => 's',
8 => 't',
9 => ' ',
10 => 'a',
11 => ' ',
12 => 'r',
13 => 'a',
14 => 'n',
15 => 'd',
16 => 'o',
],
1 => [
0 => 'm',
1 => ' ',
2 => 'p',
3 => 'i',
4 => 'e',
5 => 'c',
6 => 'e',
7 => ' ',
8 => 'o',
],
2 => [
0 => 'f',
1 => ' ',
2 => 't',
3 => 'e',
4 => 'x',
5 => 't',
6 => '.',
],
]
);
}

public function it_can_filter_its_element(): void
{
$input = \array_merge([0, false], \range(1, 10));
Expand Down
11 changes: 11 additions & 0 deletions src/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use drupol\collection\Operation\Collapse;
use drupol\collection\Operation\Combine;
use drupol\collection\Operation\Distinct;
use drupol\collection\Operation\Explode;
use drupol\collection\Operation\Filter;
use drupol\collection\Operation\Flatten;
use drupol\collection\Operation\Flip;
Expand Down Expand Up @@ -142,6 +143,16 @@ public static function empty(): CollectionInterface
return new Collection();
}

/**
* {@inheritdoc}
*
* @return \drupol\collection\Contract\Collection
*/
public function explode(string ...$strings): BaseInterface
{
return new Collection($this->run(new Explode(...$strings)));
}

/**
* {@inheritdoc}
*
Expand Down
1 change: 1 addition & 0 deletions src/Contract/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ interface Collection extends
Combineable,
Containsable,
Distinctable,
Explodeable,
Filterable,
Firstable,
Flattenable,
Expand Down
18 changes: 18 additions & 0 deletions src/Contract/Explodeable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace drupol\collection\Contract;

/**
* Interface Explodeable.
*/
interface Explodeable
{
/**
* @param string ...$explodes
*
* @return \drupol\collection\Contract\Collection
*/
public function explode(string ...$explodes): Base;
}
45 changes: 45 additions & 0 deletions src/Operation/Explode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

declare(strict_types=1);

namespace drupol\collection\Operation;

use drupol\collection\Contract\Operation;

/**
* Class Explode.
*/
final class Explode implements Operation
{
/**
* @var string[]
*/
private $explodes;

/**
* Explode constructor.
*
* @param string ...$explodes
*/
public function __construct(string ...$explodes)
{
$this->explodes = $explodes;
}

/**
* {@inheritdoc}
*/
public function on(iterable $collection): \Closure
{
$callbacks = \array_map(
static function ($explode) {
return static function ($value) use ($explode) {
return $value === $explode;
};
},
$this->explodes
);

return (new Split(...$callbacks))->on($collection);
}
}

0 comments on commit e21e84f

Please sign in to comment.