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