From 55a10b789633a34864042d0d81230c4fe587adbe Mon Sep 17 00:00:00 2001 From: Julien Rosset Date: Tue, 15 Nov 2022 14:56:32 +0100 Subject: [PATCH] Add method for join, split and remove empty values --- src/Collections/IImmutableCollection.php | 44 +++++++++++++++++++++--- src/Collections/ImmutableCollection.php | 38 +++++++++++++++++--- 2 files changed, 72 insertions(+), 10 deletions(-) diff --git a/src/Collections/IImmutableCollection.php b/src/Collections/IImmutableCollection.php index 131963c..ba58eb3 100644 --- a/src/Collections/IImmutableCollection.php +++ b/src/Collections/IImmutableCollection.php @@ -83,7 +83,7 @@ interface IImmutableCollection extends IteratorAggregate, JsonSerializable, Seri * @return static * @psalm-return static */ - public function slice (int $offset, ?int $length = null): IImmutableCollection; + public function slice (int $offset, ?int $length = null): self; /** * Get a collection of all elements that satisfy predicate $filter @@ -95,7 +95,23 @@ interface IImmutableCollection extends IteratorAggregate, JsonSerializable, Seri * @return static The result collection * @psalm-return static */ - public function filter (Closure $filter): IImmutableCollection; + public function filter (Closure $filter): self; + /** + * Get a collection of all not empty elements + * + * Use {@see https://www.php.net/manual/function.empty.php empty} to check if element is empty or not + * + * @return static The result collection + */ + public function withoutEmpties (): self; + /** + * Remove all empty elements on current collection + * + * Use {@see https://www.php.net/manual/function.empty.php empty} to check if element is empty or not + * + * @return $this + */ + public function removeEmpties (): self; /** * A new collection with $process applied on all elements * @@ -108,7 +124,7 @@ interface IImmutableCollection extends IteratorAggregate, JsonSerializable, Seri * @return static * @psalm-return static */ - public function map (Closure $process): IImmutableCollection; + public function map (Closure $process): self; /** * The list of all keys @@ -116,12 +132,30 @@ interface IImmutableCollection extends IteratorAggregate, JsonSerializable, Seri * @return static The list of all keys * @psalm-return static */ - public function keys (): IImmutableCollection; + public function keys (): self; /** * The list of all values * * @return static The list of all values * @psalm-return static */ - public function values (): IImmutableCollection; + public function values (): self; + + /** + * Join all values with a separator + * + * @param string $separator The separator + * + * @return string The joined values + */ + public function join (string $separator): string; + /** + * Create a collection of split values + * + * @param string $value The value to split + * @param string $separator The split separator + * + * @return static + */ + public static function split (string $value, string $separator): self; } \ No newline at end of file diff --git a/src/Collections/ImmutableCollection.php b/src/Collections/ImmutableCollection.php index a3a88ec..06711f0 100644 --- a/src/Collections/ImmutableCollection.php +++ b/src/Collections/ImmutableCollection.php @@ -205,7 +205,7 @@ class ImmutableCollection implements IImmutableCollection { /** * @inheritDoc */ - public function slice (int $offset, ?int $length = null): IImmutableCollection { + public function slice (int $offset, ?int $length = null): self { $output = new static(); $currentIndex = 0; @@ -226,7 +226,7 @@ class ImmutableCollection implements IImmutableCollection { /** * @inheritDoc */ - public function filter (Closure $filter): IImmutableCollection { + public function filter (Closure $filter): self { $output = new static(); foreach ($this->elements as $key => $value) { if (!$filter($key, $value)) { @@ -239,7 +239,22 @@ class ImmutableCollection implements IImmutableCollection { /** * @inheritDoc */ - public function map (Closure $process): IImmutableCollection { + public function withoutEmpties (): self { + return $this->filter(function ($value) { + return empty($value); + }); + } + /** + * @inheritDoc + */ + public function removeEmpties (): self { + $this->_initialize($this->withoutEmpties()); + return $this; + } + /** + * @inheritDoc + */ + public function map (Closure $process): self { $output = new static(); foreach ($this->elements as $key => $value) { $output->_set($key, $process($key, $value)); @@ -250,13 +265,13 @@ class ImmutableCollection implements IImmutableCollection { /** * @inheritDoc */ - public function keys (): IImmutableCollection { + public function keys (): self { return new static(array_keys($this->elements)); } /** * @inheritDoc */ - public function values (): IImmutableCollection { + public function values (): self { return new static(array_values($this->elements)); } @@ -268,4 +283,17 @@ class ImmutableCollection implements IImmutableCollection { public function jsonSerialize () { return json_encode($this->elements, JSON_THROW_ON_ERROR); } + + /** + * @inheritDoc + */ + public function join (string $separator): string { + return implode($separator, $this->toArray()); + } + /** + * @inheritDoc + */ + public static function split (string $value, string $separator): self { + return new static (explode($separator, $value)); + } } \ No newline at end of file