|
|
@ -6,7 +6,6 @@ use ArrayIterator;
|
|
|
|
use Closure;
|
|
|
|
use Closure;
|
|
|
|
use InvalidArgumentException;
|
|
|
|
use InvalidArgumentException;
|
|
|
|
use OutOfBoundsException;
|
|
|
|
use OutOfBoundsException;
|
|
|
|
use ReflectionFunction;
|
|
|
|
|
|
|
|
use Traversable;
|
|
|
|
use Traversable;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
@ -64,8 +63,10 @@ class ImmutableCollection implements IImmutableCollection {
|
|
|
|
* @return $this
|
|
|
|
* @return $this
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @internal
|
|
|
|
* @internal
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @noinspection PhpMissingReturnTypeInspection
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
protected function _set ($key, $value): static {
|
|
|
|
protected function _set ($key, $value) {
|
|
|
|
$this->elements[$this->_normalizeKey($key)] = $value;
|
|
|
|
$this->elements[$this->_normalizeKey($key)] = $value;
|
|
|
|
return $this;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -77,8 +78,10 @@ class ImmutableCollection implements IImmutableCollection {
|
|
|
|
* @return $this
|
|
|
|
* @return $this
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @internal
|
|
|
|
* @internal
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @noinspection PhpMissingReturnTypeInspection
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
protected function _merge (IImmutableCollection ...$collections): static {
|
|
|
|
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);
|
|
|
@ -94,8 +97,10 @@ class ImmutableCollection implements IImmutableCollection {
|
|
|
|
* @return $this
|
|
|
|
* @return $this
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @internal
|
|
|
|
* @internal
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @noinspection PhpMissingReturnTypeInspection
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
protected function _add (...$values): static {
|
|
|
|
protected function _add (...$values) {
|
|
|
|
foreach ($values as $value) {
|
|
|
|
foreach ($values as $value) {
|
|
|
|
$this->elements[] = $value;
|
|
|
|
$this->elements[] = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -107,8 +112,10 @@ class ImmutableCollection implements IImmutableCollection {
|
|
|
|
* @param IImmutableCollection ...$collections The collections to add
|
|
|
|
* @param IImmutableCollection ...$collections The collections to add
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @return $this
|
|
|
|
* @return $this
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @noinspection PhpMissingReturnTypeInspection
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
protected function _addCollection (IImmutableCollection ...$collections): static {
|
|
|
|
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);
|
|
|
@ -151,25 +158,25 @@ class ImmutableCollection implements IImmutableCollection {
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public function offsetExists (mixed $offset): bool {
|
|
|
|
public function offsetExists ($offset): bool {
|
|
|
|
return $this->exists($offset);
|
|
|
|
return $this->exists($offset);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public function offsetGet (mixed $offset): mixed {
|
|
|
|
public function offsetGet ($offset) {
|
|
|
|
return $this->get($offset);
|
|
|
|
return $this->get($offset);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public function offsetSet (mixed $offset, mixed $value): void {
|
|
|
|
public function offsetSet ($offset, $value): void {
|
|
|
|
throw new ImmutableException();
|
|
|
|
throw new ImmutableException();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public function offsetUnset (mixed $offset): void {
|
|
|
|
public function offsetUnset ($offset): void {
|
|
|
|
throw new ImmutableException();
|
|
|
|
throw new ImmutableException();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -184,14 +191,16 @@ class ImmutableCollection implements IImmutableCollection {
|
|
|
|
];
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* {@see https://www.php.net/manual/function.unserialize.php Unserialize} from the list of serialized properties
|
|
|
|
* @inheritDoc
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
* @param array $data The list of serialized properties
|
|
|
|
public function serialize (): ?string {
|
|
|
|
*
|
|
|
|
return serialize($this->elements);
|
|
|
|
* @return void
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public function __unserialize (array $data): void {
|
|
|
|
public function unserialize ($data) {
|
|
|
|
$this->_initialize($data['elements']);
|
|
|
|
$this->_initialize(unserialize($data));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
@ -230,7 +239,7 @@ class ImmutableCollection implements IImmutableCollection {
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public function get ($key): mixed {
|
|
|
|
public function get ($key) {
|
|
|
|
return $this->elements[$this->_normalizeKey($key)] ?? null;
|
|
|
|
return $this->elements[$this->_normalizeKey($key)] ?? null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
/**
|
|
|
@ -289,14 +298,11 @@ class ImmutableCollection implements IImmutableCollection {
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public function slice (int $offset, ?int $length = null): static {
|
|
|
|
public function slice (int $offset, ?int $length = null) {
|
|
|
|
$output = new static();
|
|
|
|
|
|
|
|
if ($this->count() === 0) {
|
|
|
|
|
|
|
|
return $output;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$this->_checkOffset($offset);
|
|
|
|
$this->_checkOffset($offset);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$output = new static();
|
|
|
|
|
|
|
|
|
|
|
|
$currentIndex = 0;
|
|
|
|
$currentIndex = 0;
|
|
|
|
foreach ($this->elements as $key => $value) {
|
|
|
|
foreach ($this->elements as $key => $value) {
|
|
|
|
if ($currentIndex++ < $offset) {
|
|
|
|
if ($currentIndex++ < $offset) {
|
|
|
@ -315,7 +321,7 @@ class ImmutableCollection implements IImmutableCollection {
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public function filter (Closure $filter): static {
|
|
|
|
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)) {
|
|
|
@ -328,7 +334,7 @@ class ImmutableCollection implements IImmutableCollection {
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public function withoutEmpties (): static {
|
|
|
|
public function withoutEmpties () {
|
|
|
|
return $this->filter(function ($value) {
|
|
|
|
return $this->filter(function ($value) {
|
|
|
|
return empty($value);
|
|
|
|
return empty($value);
|
|
|
|
});
|
|
|
|
});
|
|
|
@ -336,21 +342,10 @@ class ImmutableCollection implements IImmutableCollection {
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public function map (Closure $process): static {
|
|
|
|
public function map (Closure $process) {
|
|
|
|
//region Regarde si $process doit être appelée avec la clé en plus de la valeur
|
|
|
|
|
|
|
|
$callProcessWithKey = false;
|
|
|
|
|
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */
|
|
|
|
|
|
|
|
$processReflection = new ReflectionFunction($process);
|
|
|
|
|
|
|
|
if (count($processReflection->getParameters()) >= 2) {
|
|
|
|
|
|
|
|
if (!$processReflection->getParameters()[1]->isDefaultValueAvailable()) {
|
|
|
|
|
|
|
|
$callProcessWithKey = true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
//endregion
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$output = new static();
|
|
|
|
$output = new static();
|
|
|
|
foreach ($this->elements as $key => $value) {
|
|
|
|
foreach ($this->elements as $key => $value) {
|
|
|
|
$output->_set($key, $callProcessWithKey ? $process($key, $value) : $process($value));
|
|
|
|
$output->_set($key, $process($key, $value));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $output;
|
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -358,7 +353,7 @@ class ImmutableCollection implements IImmutableCollection {
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public function sort (Closure|IComparator|null $sorter = null): static {
|
|
|
|
public function sort ($sorter = null) {
|
|
|
|
$elements = $this->toArray();
|
|
|
|
$elements = $this->toArray();
|
|
|
|
uasort($elements, self::_normalizeSorter($sorter));
|
|
|
|
uasort($elements, self::_normalizeSorter($sorter));
|
|
|
|
|
|
|
|
|
|
|
@ -367,7 +362,7 @@ class ImmutableCollection implements IImmutableCollection {
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public function sortByKey (Closure|IComparator $sorter): static {
|
|
|
|
public function sortByKey ($sorter) {
|
|
|
|
$elements = $this->toArray();
|
|
|
|
$elements = $this->toArray();
|
|
|
|
uksort($elements, self::_normalizeSorter($sorter));
|
|
|
|
uksort($elements, self::_normalizeSorter($sorter));
|
|
|
|
|
|
|
|
|
|
|
@ -389,7 +384,7 @@ class ImmutableCollection implements IImmutableCollection {
|
|
|
|
* <br>= 0 if value1 equals value2
|
|
|
|
* <br>= 0 if value1 equals value2
|
|
|
|
* <br>> 0 if value1 is after value2
|
|
|
|
* <br>> 0 if value1 is after value2
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public static function _normalizeSorter (Closure|IComparator|null $sorter): Closure {
|
|
|
|
public static function _normalizeSorter ($sorter): Closure {
|
|
|
|
if ($sorter === null) {
|
|
|
|
if ($sorter === null) {
|
|
|
|
return function (IComparable $value1, IComparable $value2): int {
|
|
|
|
return function (IComparable $value1, IComparable $value2): int {
|
|
|
|
return $value1->compareTo($value2);
|
|
|
|
return $value1->compareTo($value2);
|
|
|
@ -414,14 +409,14 @@ class ImmutableCollection implements IImmutableCollection {
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public function values (): static {
|
|
|
|
public function values () {
|
|
|
|
return new static(array_values($this->elements));
|
|
|
|
return new static(array_values($this->elements));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public function jsonSerialize (): mixed {
|
|
|
|
public function jsonSerialize () {
|
|
|
|
return $this->elements;
|
|
|
|
return $this->elements;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -434,14 +429,14 @@ class ImmutableCollection implements IImmutableCollection {
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public static function split (string $value, string $separator): static {
|
|
|
|
public static function split (string $value, string $separator) {
|
|
|
|
return new static (explode($separator, $value));
|
|
|
|
return new static (explode($separator, $value));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public static function fill (int $size, $value): static {
|
|
|
|
public static function fill (int $size, $value) {
|
|
|
|
$keys = new ImmutableCollection();
|
|
|
|
$keys = new ImmutableCollection();
|
|
|
|
for ($curr = 0; $curr < $size; $curr++) {
|
|
|
|
for ($curr = 0; $curr < $size; $curr++) {
|
|
|
|
$keys->_add($curr);
|
|
|
|
$keys->_add($curr);
|
|
|
@ -452,7 +447,7 @@ class ImmutableCollection implements IImmutableCollection {
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public static function fillWithKeys (IImmutableCollection $keys, $value): static {
|
|
|
|
public static function fillWithKeys (IImmutableCollection $keys, $value) {
|
|
|
|
$collection = new static();
|
|
|
|
$collection = new static();
|
|
|
|
foreach ($keys as $key) {
|
|
|
|
foreach ($keys as $key) {
|
|
|
|
$collection->_set($key, $value);
|
|
|
|
$collection->_set($key, $value);
|
|
|
@ -463,7 +458,7 @@ class ImmutableCollection implements IImmutableCollection {
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public function mergeWith (IImmutableCollection ...$collections): static {
|
|
|
|
public function mergeWith (IImmutableCollection ...$collections) {
|
|
|
|
return (clone $this)->_merge(...$collections);
|
|
|
|
return (clone $this)->_merge(...$collections);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|