diff --git a/README.md b/README.md index 8daad7de6..ebc700fcd 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/spec/drupol/collection/CollectionSpec.php b/spec/drupol/collection/CollectionSpec.php index 8512b031b..8f93ad512 100644 --- a/spec/drupol/collection/CollectionSpec.php +++ b/spec/drupol/collection/CollectionSpec.php @@ -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)); diff --git a/src/Collection.php b/src/Collection.php index cb99dc795..95c908b92 100644 --- a/src/Collection.php +++ b/src/Collection.php @@ -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; @@ -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} * diff --git a/src/Contract/Collection.php b/src/Contract/Collection.php index 617499365..3fd8a7e97 100644 --- a/src/Contract/Collection.php +++ b/src/Contract/Collection.php @@ -17,6 +17,7 @@ interface Collection extends Combineable, Containsable, Distinctable, + Explodeable, Filterable, Firstable, Flattenable, diff --git a/src/Contract/Explodeable.php b/src/Contract/Explodeable.php new file mode 100644 index 000000000..925c41883 --- /dev/null +++ b/src/Contract/Explodeable.php @@ -0,0 +1,18 @@ +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); + } +}