|
|
@ -5,50 +5,43 @@ namespace jrosset\Collections;
|
|
|
|
use ArrayIterator;
|
|
|
|
use ArrayIterator;
|
|
|
|
use Closure;
|
|
|
|
use Closure;
|
|
|
|
use InvalidArgumentException;
|
|
|
|
use InvalidArgumentException;
|
|
|
|
use JsonException;
|
|
|
|
|
|
|
|
use Traversable;
|
|
|
|
use Traversable;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* An immutable collection
|
|
|
|
* An immutable collection
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @psalm-template TKey of array-key
|
|
|
|
* @template TKey of array-key
|
|
|
|
* @template-covariant TValue
|
|
|
|
* @template TValue
|
|
|
|
* @template-implements IImmutableCollection<TKey, TValue>
|
|
|
|
*
|
|
|
|
|
|
|
|
* @implements IImmutableCollection<TKey, TValue>
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
class ImmutableCollection implements IImmutableCollection {
|
|
|
|
class ImmutableCollection implements IImmutableCollection {
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* @var array The internal array of elements
|
|
|
|
* @var array<TKey, TValue> The internal array of elements
|
|
|
|
* @psalm-var array<TKey, TValue>
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @internal
|
|
|
|
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
protected array $elements;
|
|
|
|
protected array $elements;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Initialise a new collection
|
|
|
|
* Initialise a new collection
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @param array|Traversable|null $other The initial values
|
|
|
|
* @param iterable|null $other The initial values
|
|
|
|
*
|
|
|
|
|
|
|
|
* @psalm-param array<TKey, TValue>|Traversable<TKey, TValue>|null
|
|
|
|
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @throws InvalidArgumentException If the initial values aren't valid
|
|
|
|
* @throws InvalidArgumentException If the initial values aren't valid
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public function __construct ($other = null) {
|
|
|
|
public function __construct (?iterable $other = null) {
|
|
|
|
$this->_initialize($other);
|
|
|
|
$this->_initialize($other);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Initialise the internals elements
|
|
|
|
* Initialise the internals elements
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @param array|Traversable|null $other The initial values
|
|
|
|
* @param iterable|null $other The initial values
|
|
|
|
*
|
|
|
|
|
|
|
|
* @psalm-param array<TKey, TValue>|Traversable<TKey, TValue>|null
|
|
|
|
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @throws InvalidArgumentException If the initial values aren't valid
|
|
|
|
* @throws InvalidArgumentException If the initial values aren't valid
|
|
|
|
* @internal
|
|
|
|
|
|
|
|
*
|
|
|
|
*
|
|
|
|
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
protected function _initialize ($other = null) {
|
|
|
|
protected function _initialize (?iterable $other = null): void {
|
|
|
|
$this->elements = [];
|
|
|
|
$this->elements = [];
|
|
|
|
if ($other !== null) {
|
|
|
|
if ($other !== null) {
|
|
|
|
if (!is_array($other) && !$other instanceof Traversable) {
|
|
|
|
if (!is_array($other) && !$other instanceof Traversable) {
|
|
|
@ -63,17 +56,16 @@ class ImmutableCollection implements IImmutableCollection {
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Internally set a value to a key
|
|
|
|
* Internally set a value to a key
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @param array-key $key The key
|
|
|
|
* @param TKey $key The key
|
|
|
|
* @param mixed $value The value
|
|
|
|
* @param TValue $value The value
|
|
|
|
*
|
|
|
|
|
|
|
|
* @psalm-param TKey $key
|
|
|
|
|
|
|
|
* @psalm-param TValue $value
|
|
|
|
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @return $this
|
|
|
|
* @return $this
|
|
|
|
|
|
|
|
*
|
|
|
|
* @internal
|
|
|
|
* @internal
|
|
|
|
*
|
|
|
|
*
|
|
|
|
|
|
|
|
* @noinspection PhpMissingReturnTypeInspection
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
protected function _set ($key, $value): self {
|
|
|
|
protected function _set ($key, $value) {
|
|
|
|
$this->elements[$this->_normalizeKey($key)] = $value;
|
|
|
|
$this->elements[$this->_normalizeKey($key)] = $value;
|
|
|
|
return $this;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -83,8 +75,12 @@ class ImmutableCollection implements IImmutableCollection {
|
|
|
|
* @param IImmutableCollection ...$collections The collections to merge
|
|
|
|
* @param IImmutableCollection ...$collections The collections to merge
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @return $this
|
|
|
|
* @return $this
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @internal
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @noinspection PhpMissingReturnTypeInspection
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
protected function _merge (IImmutableCollection ...$collections): self {
|
|
|
|
protected function _merge (IImmutableCollection ...$collections) {
|
|
|
|
foreach ($collections as $collection) {
|
|
|
|
foreach ($collections as $collection) {
|
|
|
|
foreach ($collection as $key => $value) {
|
|
|
|
foreach ($collection as $key => $value) {
|
|
|
|
$this->_set($key, $value);
|
|
|
|
$this->_set($key, $value);
|
|
|
@ -95,15 +91,15 @@ class ImmutableCollection implements IImmutableCollection {
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Internally add a value
|
|
|
|
* Internally add a value
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @param mixed ...$values The value
|
|
|
|
* @param TValue ...$values The value
|
|
|
|
*
|
|
|
|
|
|
|
|
* @psalm-param TValue ...$value
|
|
|
|
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @return $this
|
|
|
|
* @return $this
|
|
|
|
|
|
|
|
*
|
|
|
|
* @internal
|
|
|
|
* @internal
|
|
|
|
*
|
|
|
|
*
|
|
|
|
|
|
|
|
* @noinspection PhpMissingReturnTypeInspection
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
protected function _add (...$values): self {
|
|
|
|
protected function _add (...$values) {
|
|
|
|
foreach ($values as $value) {
|
|
|
|
foreach ($values as $value) {
|
|
|
|
$this->elements[] = $value;
|
|
|
|
$this->elements[] = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -114,9 +110,11 @@ class ImmutableCollection implements IImmutableCollection {
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @param IImmutableCollection ...$collections The collections to add
|
|
|
|
* @param IImmutableCollection ...$collections The collections to add
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
* @return $this
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @noinspection PhpMissingReturnTypeInspection
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
protected function _addCollection (IImmutableCollection ...$collections): self {
|
|
|
|
protected function _addCollection (IImmutableCollection ...$collections) {
|
|
|
|
foreach ($collections as $collection) {
|
|
|
|
foreach ($collections as $collection) {
|
|
|
|
foreach ($collection as $value) {
|
|
|
|
foreach ($collection as $value) {
|
|
|
|
$this->_add($value);
|
|
|
|
$this->_add($value);
|
|
|
@ -127,12 +125,9 @@ class ImmutableCollection implements IImmutableCollection {
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Normalize a key
|
|
|
|
* Normalize a key
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @param array-key $key The key to normalize
|
|
|
|
* @param TKey $key The key to normalize
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @psalm-param TKey $key
|
|
|
|
* @return TKey The normalized key
|
|
|
|
*
|
|
|
|
|
|
|
|
* @return array-key The normalized key
|
|
|
|
|
|
|
|
* @psalm-return TKey
|
|
|
|
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
protected function _normalizeKey ($key) {
|
|
|
|
protected function _normalizeKey ($key) {
|
|
|
|
return $key;
|
|
|
|
return $key;
|
|
|
@ -141,7 +136,7 @@ class ImmutableCollection implements IImmutableCollection {
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public function getIterator () {
|
|
|
|
public function getIterator (): ArrayIterator {
|
|
|
|
return new ArrayIterator($this->elements);
|
|
|
|
return new ArrayIterator($this->elements);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -170,6 +165,16 @@ class ImmutableCollection implements IImmutableCollection {
|
|
|
|
throw new ImmutableException();
|
|
|
|
throw new ImmutableException();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* List of properties to {@see https://www.php.net/manual/function.serialize.php serialize}
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @return array[] List of properties to {@see https://www.php.net/manual/function.serialize.php serialize}
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
public function __serialize (): array {
|
|
|
|
|
|
|
|
return [
|
|
|
|
|
|
|
|
'elements' => $this->elements,
|
|
|
|
|
|
|
|
];
|
|
|
|
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
*/
|
|
|
@ -237,7 +242,7 @@ class ImmutableCollection implements IImmutableCollection {
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public function slice (int $offset, ?int $length = null): self {
|
|
|
|
public function slice (int $offset, ?int $length = null) {
|
|
|
|
$output = new static();
|
|
|
|
$output = new static();
|
|
|
|
|
|
|
|
|
|
|
|
$currentIndex = 0;
|
|
|
|
$currentIndex = 0;
|
|
|
@ -258,7 +263,7 @@ class ImmutableCollection implements IImmutableCollection {
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public function filter (Closure $filter): self {
|
|
|
|
public function filter (Closure $filter) {
|
|
|
|
$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)) {
|
|
|
@ -271,7 +276,7 @@ class ImmutableCollection implements IImmutableCollection {
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public function withoutEmpties (): self {
|
|
|
|
public function withoutEmpties () {
|
|
|
|
return $this->filter(function ($value) {
|
|
|
|
return $this->filter(function ($value) {
|
|
|
|
return empty($value);
|
|
|
|
return empty($value);
|
|
|
|
});
|
|
|
|
});
|
|
|
@ -279,14 +284,7 @@ class ImmutableCollection implements IImmutableCollection {
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public function removeEmpties (): self {
|
|
|
|
public function map (Closure $process) {
|
|
|
|
$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));
|
|
|
@ -298,19 +296,8 @@ class ImmutableCollection implements IImmutableCollection {
|
|
|
|
* @inheritDoc
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public function sort ($sorter = null) {
|
|
|
|
public function sort ($sorter = null) {
|
|
|
|
if ($sorter === null) {
|
|
|
|
|
|
|
|
$sorter = function (IComparable $value1, IComparable $value2): int {
|
|
|
|
|
|
|
|
return $value1->compareTo($value2);
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
elseif ($sorter instanceof IComparator) {
|
|
|
|
|
|
|
|
$sorter = function ($value1, $value2) use ($sorter): int {
|
|
|
|
|
|
|
|
return $sorter->compare($value1, $value2);
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$elements = $this->toArray();
|
|
|
|
$elements = $this->toArray();
|
|
|
|
uasort($elements, $sorter);
|
|
|
|
uasort($elements, self::_normalizeSorter($sorter));
|
|
|
|
|
|
|
|
|
|
|
|
return new static($elements);
|
|
|
|
return new static($elements);
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -318,38 +305,61 @@ class ImmutableCollection implements IImmutableCollection {
|
|
|
|
* @inheritDoc
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public function sortByKey ($sorter) {
|
|
|
|
public function sortByKey ($sorter) {
|
|
|
|
if ($sorter instanceof IComparator) {
|
|
|
|
|
|
|
|
$sorter = function ($value1, $value2) use ($sorter): int {
|
|
|
|
|
|
|
|
return $sorter->compare($value1, $value2);
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$elements = $this->toArray();
|
|
|
|
$elements = $this->toArray();
|
|
|
|
uksort($elements, $sorter);
|
|
|
|
uksort($elements, self::_normalizeSorter($sorter));
|
|
|
|
|
|
|
|
|
|
|
|
return new static($elements);
|
|
|
|
return new static($elements);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Normalize a sorter method
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @param null|IComparator|Closure(TValue, TValue):int $sorter The sorting method ; Null if values are object implementing {@see IComparable}.
|
|
|
|
|
|
|
|
* <br>Return :
|
|
|
|
|
|
|
|
* <br>< 0 if value1 is before value2
|
|
|
|
|
|
|
|
* <br>= 0 if value1 equals value2
|
|
|
|
|
|
|
|
* <br>> 0 if value1 is after value2
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @return Closure(TValue, TValue):int The normalized sorter method
|
|
|
|
|
|
|
|
* <br>Return :
|
|
|
|
|
|
|
|
* <br>< 0 if value1 is before value2
|
|
|
|
|
|
|
|
* <br>= 0 if value1 equals value2
|
|
|
|
|
|
|
|
* <br>> 0 if value1 is after value2
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
public static function _normalizeSorter ($sorter): Closure {
|
|
|
|
|
|
|
|
if ($sorter === null) {
|
|
|
|
|
|
|
|
return function (IComparable $value1, IComparable $value2): int {
|
|
|
|
|
|
|
|
return $value1->compareTo($value2);
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
elseif ($sorter instanceof IComparator) {
|
|
|
|
|
|
|
|
return function ($value1, $value2) use ($sorter): int {
|
|
|
|
|
|
|
|
return $sorter->compare($value1, $value2);
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
else {
|
|
|
|
|
|
|
|
return $sorter;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public function keys (): self {
|
|
|
|
public function keys (): IImmutableCollection {
|
|
|
|
return new static(array_keys($this->elements));
|
|
|
|
return new static(array_keys($this->elements));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public function values (): self {
|
|
|
|
public function values () {
|
|
|
|
return new static(array_values($this->elements));
|
|
|
|
return new static(array_values($this->elements));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
* @inheritDoc
|
|
|
|
*
|
|
|
|
|
|
|
|
* @throws JsonException
|
|
|
|
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public function jsonSerialize () {
|
|
|
|
public function jsonSerialize () {
|
|
|
|
return json_encode($this->elements, JSON_THROW_ON_ERROR);
|
|
|
|
return $this->elements;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
@ -361,14 +371,14 @@ class ImmutableCollection implements IImmutableCollection {
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public static function split (string $value, string $separator): self {
|
|
|
|
public static function split (string $value, string $separator) {
|
|
|
|
return new static (explode($separator, $value));
|
|
|
|
return new static (explode($separator, $value));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public function combineWith (IImmutableCollection ...$collections): IImmutableCollection {
|
|
|
|
public function mergeWith (IImmutableCollection ...$collections) {
|
|
|
|
return (clone $this)->_merge(...$collections);
|
|
|
|
return (clone $this)->_merge(...$collections);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|