Add method for join, split and remove empty values

2.x 2.1.0
Julien Rosset 3 years ago
parent 4b7e60c003
commit 55a10b7896

@ -83,7 +83,7 @@ interface IImmutableCollection extends IteratorAggregate, JsonSerializable, Seri
* @return static * @return static
* @psalm-return static<TKey, TValue> * @psalm-return static<TKey, TValue>
*/ */
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 * 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 * @return static The result collection
* @psalm-return static<TKey, TValue> * @psalm-return static<TKey, TValue>
*/ */
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 * A new collection with $process applied on all elements
* *
@ -108,7 +124,7 @@ interface IImmutableCollection extends IteratorAggregate, JsonSerializable, Seri
* @return static * @return static
* @psalm-return static<TKey, TResultValue> * @psalm-return static<TKey, TResultValue>
*/ */
public function map (Closure $process): IImmutableCollection; public function map (Closure $process): self;
/** /**
* The list of all keys * The list of all keys
@ -116,12 +132,30 @@ interface IImmutableCollection extends IteratorAggregate, JsonSerializable, Seri
* @return static The list of all keys * @return static The list of all keys
* @psalm-return static<int, TKey> * @psalm-return static<int, TKey>
*/ */
public function keys (): IImmutableCollection; public function keys (): self;
/** /**
* The list of all values * The list of all values
* *
* @return static The list of all values * @return static The list of all values
* @psalm-return static<int, TValue> * @psalm-return static<int, TValue>
*/ */
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;
} }

@ -205,7 +205,7 @@ class ImmutableCollection implements IImmutableCollection {
/** /**
* @inheritDoc * @inheritDoc
*/ */
public function slice (int $offset, ?int $length = null): IImmutableCollection { public function slice (int $offset, ?int $length = null): self {
$output = new static(); $output = new static();
$currentIndex = 0; $currentIndex = 0;
@ -226,7 +226,7 @@ class ImmutableCollection implements IImmutableCollection {
/** /**
* @inheritDoc * @inheritDoc
*/ */
public function filter (Closure $filter): IImmutableCollection { public function filter (Closure $filter): self {
$output = new static(); $output = new static();
foreach ($this->elements as $key => $value) { foreach ($this->elements as $key => $value) {
if (!$filter($key, $value)) { if (!$filter($key, $value)) {
@ -239,7 +239,22 @@ class ImmutableCollection implements IImmutableCollection {
/** /**
* @inheritDoc * @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(); $output = new static();
foreach ($this->elements as $key => $value) { foreach ($this->elements as $key => $value) {
$output->_set($key, $process($key, $value)); $output->_set($key, $process($key, $value));
@ -250,13 +265,13 @@ class ImmutableCollection implements IImmutableCollection {
/** /**
* @inheritDoc * @inheritDoc
*/ */
public function keys (): IImmutableCollection { public function keys (): self {
return new static(array_keys($this->elements)); return new static(array_keys($this->elements));
} }
/** /**
* @inheritDoc * @inheritDoc
*/ */
public function values (): IImmutableCollection { public function values (): self {
return new static(array_values($this->elements)); return new static(array_values($this->elements));
} }
@ -268,4 +283,17 @@ class ImmutableCollection implements IImmutableCollection {
public function jsonSerialize () { public function jsonSerialize () {
return json_encode($this->elements, JSON_THROW_ON_ERROR); 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));
}
} }
Loading…
Cancel
Save