Compare commits

..

5 Commits
master ... 2.x

Author SHA1 Message Date
Julien Rosset 63e523f195 Add access with offset and first/last methods 2 years ago
Julien Rosset c0c03b9cae Add IImmutableCollection::fill and IImmutableCollection::fillWithKeys 2 years ago
Julien Rosset eb3d47e640 ICollection: add "self" version of multiple functions
+ Simplify/Correct template declarations
+ Fix method signatures (self → static)
2 years ago
Julien Rosset a603f32fd6 Switch to self implemented IComparable and IComparator 2 years ago
Julien Rosset d293b3fe7b Add unique collections 2 years ago

@ -2,20 +2,10 @@
"name": "jrosset/collections", "name": "jrosset/collections",
"description": "Classes for collections", "description": "Classes for collections",
"keywords": [ ], "keywords": [ ],
"type": "library",
"config": {
"sort-packages": true
},
"extra": {
"branch-alias": {
"dev-master": "3.x-dev"
}
},
"minimum-stability": "stable", "minimum-stability": "stable",
"require": { "require": {
"php": "^8.1" "php": "^7.4 || ^8.0"
}, },
"autoload": { "autoload": {
"psr-4": { "psr-4": {
@ -40,4 +30,4 @@
"docs": "https://git.jrosset.ovh/jrosset/PhpCollections/wiki", "docs": "https://git.jrosset.ovh/jrosset/PhpCollections/wiki",
"source": "https://git.jrosset.ovh/jrosset/PhpCollections" "source": "https://git.jrosset.ovh/jrosset/PhpCollections"
} }
} }

@ -17,40 +17,40 @@ class Collection extends ImmutableCollection implements ICollection {
/** /**
* @inheritDoc * @inheritDoc
*/ */
public function set ($key, $value): static { public function set ($key, $value) {
return $this->_set($key, $value); return $this->_set($key, $value);
} }
/** /**
* @inheritDoc * @inheritDoc
*/ */
public function merge (IImmutableCollection ...$collections): static { public function merge (IImmutableCollection ...$collections) {
return $this->_merge(...$collections); return $this->_merge(...$collections);
} }
/** /**
* @inheritDoc * @inheritDoc
*/ */
public function add (...$values): static { public function add (...$values) {
return $this->_add(...$values); return $this->_add(...$values);
} }
/** /**
* @inheritDoc * @inheritDoc
*/ */
public function addCollection (IImmutableCollection ...$collections): static { public function addCollection (IImmutableCollection ...$collections) {
return $this->_addCollection(...$collections); return $this->_addCollection(...$collections);
} }
/** /**
* @inheritDoc * @inheritDoc
*/ */
public function prepend (...$values): static { public function prepend (...$values) {
array_unshift($this->elements, ...$values); array_unshift($this->elements, ...$values);
return $this; return $this;
} }
/** /**
* @inheritDoc * @inheritDoc
*/ */
public function prependCollection (IImmutableCollection ...$collections): static { public function prependCollection (IImmutableCollection ...$collections) {
$prepend = array_merge( $prepend = array_merge(
...array_map( ...array_map(
function (IImmutableCollection $collection): array { function (IImmutableCollection $collection): array {
@ -65,21 +65,21 @@ class Collection extends ImmutableCollection implements ICollection {
/** /**
* @inheritDoc * @inheritDoc
*/ */
public function clear (): static { public function clear () {
$this->_initialize(); $this->_initialize();
return $this; return $this;
} }
/** /**
* @inheritDoc * @inheritDoc
*/ */
public function remove ($key): static { public function remove ($key) {
unset($this->elements[$this->_normalizeKey($key)]); unset($this->elements[$this->_normalizeKey($key)]);
return $this; return $this;
} }
/** /**
* @inheritDoc * @inheritDoc
*/ */
public function removeValue ($value, bool $strict = false): static { public function removeValue ($value, bool $strict = false) {
foreach ($this->elements as $currentKey => $currentValue) { foreach ($this->elements as $currentKey => $currentValue) {
if (($strict && $value === $currentValue) || (!$strict && $value == $currentValue)) { if (($strict && $value === $currentValue) || (!$strict && $value == $currentValue)) {
unset($this->elements[$currentKey]); unset($this->elements[$currentKey]);
@ -112,41 +112,37 @@ class Collection extends ImmutableCollection implements ICollection {
/** /**
* @inheritDoc * @inheritDoc
*/ */
public function offsetSet (mixed $offset, mixed $value): void { public function offsetSet ($offset, $value): void {
$this->set($offset, $value); $this->set($offset, $value);
} }
/** /**
* @inheritDoc * @inheritDoc
*/ */
public function offsetUnset (mixed $offset): void { public function offsetUnset ($offset): void {
$this->remove($offset); $this->remove($offset);
} }
/** /**
* @inheritDoc * @inheritDoc
*/ */
public function sliceSelf (int $offset, ?int $length = null): static { public function sliceSelf (int $offset, ?int $length = null) {
if ($this->count() === 0) { $this->_checkOffset($offset);
$this->elements = [];
} $this->elements = array_slice($this->elements, $offset, $length, true);
else {
$this->_checkOffset($offset);
$this->elements = array_slice($this->elements, $offset, $length, true);
}
return $this; return $this;
} }
/** /**
* @inheritDoc * @inheritDoc
*/ */
public function filterSelf (Closure $filter): static { public function filterSelf (Closure $filter) {
$this->elements = array_filter($this->elements, $filter); $this->elements = array_filter($this->elements, $filter);
return $this; return $this;
} }
/** /**
* @inheritDoc * @inheritDoc
*/ */
public function removeEmpties (): static { public function removeEmpties () {
return $this->filterSelf(function ($value) { return $this->filterSelf(function ($value) {
return empty($value); return empty($value);
}); });
@ -154,22 +150,22 @@ class Collection extends ImmutableCollection implements ICollection {
/** /**
* @inheritDoc * @inheritDoc
*/ */
public function mapSelf (Closure $process): static { public function mapSelf (Closure $process) {
$this->elements = $this->map($process)->elements; $this->elements = array_map($process, $this->elements);
return $this; return $this;
} }
/** /**
* @inheritDoc * @inheritDoc
*/ */
public function sortSelf (Closure|IComparator|null $sorter = null): static { public function sortSelf ($sorter = null) {
uasort($this->elements, self::_normalizeSorter($sorter)); uasort($this->elements, self::_normalizeSorter($sorter));
return $this; return $this;
} }
/** /**
* @inheritDoc * @inheritDoc
*/ */
public function sortSelfByKey (Closure|IComparator $sorter): static { public function sortSelfByKey ($sorter) {
uksort($this->elements, self::_normalizeSorter($sorter)); uksort($this->elements, self::_normalizeSorter($sorter));
return $this; return $this;
} }

@ -3,8 +3,8 @@
namespace jrosset\Collections; namespace jrosset\Collections;
use Closure; use Closure;
use Exception;
use OutOfBoundsException; use OutOfBoundsException;
use Throwable;
/** /**
* Interface for a collection * Interface for a collection
@ -22,16 +22,20 @@ interface ICollection extends IImmutableCollection {
* @param TValue $value The value * @param TValue $value The value
* *
* @return $this * @return $this
*
* @noinspection PhpMissingReturnTypeInspection
*/ */
public function set ($key, $value): static; public function set ($key, $value);
/** /**
* Merge one or multiple collections into the current one * Merge one or multiple collections into the current one
* *
* @param IImmutableCollection<TKey, TValue> ...$collections The collections to merge * @param IImmutableCollection<TKey, TValue> ...$collections The collections to merge
* *
* @return $this * @return $this
*
* @noinspection PhpMissingReturnTypeInspection
*/ */
public function merge (IImmutableCollection ...$collections): static; public function merge (IImmutableCollection ...$collections);
/** /**
* Add one or multiple values * Add one or multiple values
@ -39,16 +43,20 @@ interface ICollection extends IImmutableCollection {
* @param TValue ...$values The values * @param TValue ...$values The values
* *
* @return $this * @return $this
*
* @noinspection PhpMissingReturnTypeInspection
*/ */
public function add (...$values): static; public function add (...$values);
/** /**
* Add the <b>values</b> of one or multiple collections * Add the <b>values</b> of one or multiple collections
* *
* @param IImmutableCollection<TKey, TValue> ...$collections The collections to add * @param IImmutableCollection<TKey, TValue> ...$collections The collections to add
* *
* @return $this * @return $this
*
* @noinspection PhpMissingReturnTypeInspection
*/ */
public function addCollection (IImmutableCollection ...$collections): static; public function addCollection (IImmutableCollection ...$collections);
/** /**
* Add one or multiple values at the beginning of the current collection * Add one or multiple values at the beginning of the current collection
@ -56,31 +64,39 @@ interface ICollection extends IImmutableCollection {
* @param TValue ...$values The values * @param TValue ...$values The values
* *
* @return $this * @return $this
*
* @noinspection PhpMissingReturnTypeInspection
*/ */
public function prepend (...$values): static; public function prepend (...$values);
/** /**
* Add the <b>values</b> of one or multiple collections at the beginning of the current collection * Add the <b>values</b> of one or multiple collections at the beginning of the current collection
* *
* @param IImmutableCollection<TKey, TValue> ...$collections The collections to add * @param IImmutableCollection<TKey, TValue> ...$collections The collections to add
* *
* @return $this * @return $this
*
* @noinspection PhpMissingReturnTypeInspection
*/ */
public function prependCollection (IImmutableCollection ...$collections): static; public function prependCollection (IImmutableCollection ...$collections);
/** /**
* Empty the collection * Empty the collection
* *
* @return $this * @return $this
*
* @noinspection PhpMissingReturnTypeInspection
*/ */
public function clear (): static; public function clear ();
/** /**
* Remove a value from it's key * Remove a value from it's key
* *
* @param TKey $key The key * @param TKey $key The key
* *
* @return $this * @return $this
*
* @noinspection PhpMissingReturnTypeInspection
*/ */
public function remove ($key): static; public function remove ($key);
/** /**
* Delete all instances of a value * Delete all instances of a value
* *
@ -88,8 +104,10 @@ interface ICollection extends IImmutableCollection {
* @param bool $strict Strict comparison ? * @param bool $strict Strict comparison ?
* *
* @return $this * @return $this
*
* @noinspection PhpMissingReturnTypeInspection
*/ */
public function removeValue ($value, bool $strict = false): static; public function removeValue ($value, bool $strict = false);
/** /**
* Get and remove the first element * Get and remove the first element
@ -119,8 +137,10 @@ interface ICollection extends IImmutableCollection {
* @return $this * @return $this
* *
* @throws OutOfBoundsException If the offset is not valid * @throws OutOfBoundsException If the offset is not valid
*
* @noinspection PhpMissingReturnTypeInspection
*/ */
public function sliceSelf (int $offset, ?int $length = null): static; public function sliceSelf (int $offset, ?int $length = null);
/** /**
* Keep only elements that satisfy predicate $filter * Keep only elements that satisfy predicate $filter
@ -128,24 +148,30 @@ interface ICollection extends IImmutableCollection {
* @param Closure(TKey, TValue):bool $filter The filtering predicate * @param Closure(TKey, TValue):bool $filter The filtering predicate
* *
* @return $this * @return $this
*
* @noinspection PhpMissingReturnTypeInspection
*/ */
public function filterSelf (Closure $filter): static; public function filterSelf (Closure $filter);
/** /**
* Keep only non-empty elements * Keep only non-empty elements
* *
* Use {@see https://www.php.net/manual/function.empty.php empty} to check if element is empty or not * Use {@see https://www.php.net/manual/function.empty.php empty} to check if element is empty or not
* *
* @return $this * @return $this
*
* @noinspection PhpMissingReturnTypeInspection
*/ */
public function removeEmpties (): static; public function removeEmpties ();
/** /**
* Applied <b>$process</b> on all elements * Applied <b>$process</b> on all elements
* *
* @param Closure(TKey, TValue): TValue $process The process function to apply on each element * @param Closure(TKey, TValue): TValue $process The process function to apply on each element
* *
* @return $this * @return $this
*
* @noinspection PhpMissingReturnTypeInspection
*/ */
public function mapSelf (Closure $process): static; public function mapSelf (Closure $process);
/** /**
* Sort the elements (by value) * Sort the elements (by value)
@ -158,9 +184,11 @@ interface ICollection extends IImmutableCollection {
* *
* @return $this * @return $this
* *
* @throws Throwable If an error occurs * @throws Exception If an error occurs
*
* @noinspection PhpMissingReturnTypeInspection
*/ */
public function sortSelf (Closure|IComparator|null $sorter = null): static; public function sortSelf ($sorter = null);
/** /**
* Sort the elements by key * Sort the elements by key
* *
@ -171,7 +199,9 @@ interface ICollection extends IImmutableCollection {
* *
* @return $this * @return $this
* *
* @throws Throwable If an error occurs * @throws Exception If an error occurs
*
* @noinspection PhpMissingReturnTypeInspection
*/ */
public function sortSelfByKey (Closure|IComparator $sorter): static; public function sortSelfByKey ($sorter);
} }

@ -8,6 +8,7 @@ use Countable;
use IteratorAggregate; use IteratorAggregate;
use JsonSerializable; use JsonSerializable;
use OutOfBoundsException; use OutOfBoundsException;
use Serializable;
use Throwable; use Throwable;
/** /**
@ -20,7 +21,7 @@ use Throwable;
* @implements ArrayAccess<TKey, TValue> * @implements ArrayAccess<TKey, TValue>
* @implements IArrayCast<TKey, TValue> * @implements IArrayCast<TKey, TValue>
*/ */
interface IImmutableCollection extends IteratorAggregate, JsonSerializable, Countable, ArrayAccess, IArrayCast { interface IImmutableCollection extends IteratorAggregate, JsonSerializable, Serializable, Countable, ArrayAccess, IArrayCast {
/** /**
* Checks if the collection is empty * Checks if the collection is empty
* *
@ -130,8 +131,10 @@ interface IImmutableCollection extends IteratorAggregate, JsonSerializable, Coun
* @return static<TKey, TValue> The result collection * @return static<TKey, TValue> The result collection
* *
* @throws OutOfBoundsException If the offset is not valid * @throws OutOfBoundsException If the offset is not valid
*
* @noinspection PhpMissingReturnTypeInspection
*/ */
public function slice (int $offset, ?int $length = null): static; public function slice (int $offset, ?int $length = null);
/** /**
* Get a collection of all elements that satisfy predicate $filter * Get a collection of all elements that satisfy predicate $filter
@ -139,16 +142,20 @@ interface IImmutableCollection extends IteratorAggregate, JsonSerializable, Coun
* @param Closure(TKey, TValue):bool $filter The filtering predicate * @param Closure(TKey, TValue):bool $filter The filtering predicate
* *
* @return static<TKey, TValue> The result collection * @return static<TKey, TValue> The result collection
*
* @noinspection PhpMissingReturnTypeInspection
*/ */
public function filter (Closure $filter): static; public function filter (Closure $filter);
/** /**
* Get a collection of all not empty elements * 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 * Use {@see https://www.php.net/manual/function.empty.php empty} to check if element is empty or not
* *
* @return static<TKey, TValue> The result collection * @return static<TKey, TValue> The result collection
*
* @noinspection PhpMissingReturnTypeInspection
*/ */
public function withoutEmpties (): static; public function withoutEmpties ();
/** /**
* A new collection with <b>$process</b> applied on all elements * A new collection with <b>$process</b> applied on all elements
* *
@ -157,8 +164,10 @@ interface IImmutableCollection extends IteratorAggregate, JsonSerializable, Coun
* @param Closure(TKey, TValue): TResultValue $process The process function to apply on each element * @param Closure(TKey, TValue): TResultValue $process The process function to apply on each element
* *
* @return static<TKey, TResultValue> The result collection * @return static<TKey, TResultValue> The result collection
*
* @noinspection PhpMissingReturnTypeInspection
*/ */
public function map (Closure $process): static; public function map (Closure $process);
/** /**
* Get a collection with the elements sorted (by value) * Get a collection with the elements sorted (by value)
@ -172,8 +181,10 @@ interface IImmutableCollection extends IteratorAggregate, JsonSerializable, Coun
* @return static<TKey, TValue> The result collection * @return static<TKey, TValue> The result collection
* *
* @throws Throwable If an error occurs * @throws Throwable If an error occurs
*
* @noinspection PhpMissingReturnTypeInspection
*/ */
public function sort (Closure|IComparator|null $sorter = null): static; public function sort ($sorter = null);
/** /**
* Get a collection with the elements sorted by key * Get a collection with the elements sorted by key
* *
@ -185,8 +196,10 @@ interface IImmutableCollection extends IteratorAggregate, JsonSerializable, Coun
* @return static<TKey, TValue> The result collection * @return static<TKey, TValue> The result collection
* *
* @throws Throwable If an error occurs * @throws Throwable If an error occurs
*
* @noinspection PhpMissingReturnTypeInspection
*/ */
public function sortByKey (Closure|IComparator $sorter): static; public function sortByKey ($sorter);
/** /**
* The list of all keys * The list of all keys
@ -198,8 +211,10 @@ interface IImmutableCollection extends IteratorAggregate, JsonSerializable, Coun
* The list of all values * The list of all values
* *
* @return static<int, TValue> The list of all values * @return static<int, TValue> The list of all values
*
* @noinspection PhpMissingReturnTypeInspection
*/ */
public function values (): static; public function values ();
/** /**
* Join all values with a separator * Join all values with a separator
@ -216,8 +231,10 @@ interface IImmutableCollection extends IteratorAggregate, JsonSerializable, Coun
* @param string $separator The split separator * @param string $separator The split separator
* *
* @return static<int, string> The result collection * @return static<int, string> The result collection
*
* @noinspection PhpMissingReturnTypeInspection
*/ */
public static function split (string $value, string $separator): static; public static function split (string $value, string $separator);
/** /**
* Create a collection filled with the same element * Create a collection filled with the same element
@ -226,8 +243,10 @@ interface IImmutableCollection extends IteratorAggregate, JsonSerializable, Coun
* @param TValue $value The value to fill the collection * @param TValue $value The value to fill the collection
* *
* @return static<int, TValue> The result collection * @return static<int, TValue> The result collection
*
* @noinspection PhpMissingReturnTypeInspection
*/ */
public static function fill (int $size, $value): static; public static function fill (int $size, $value);
/** /**
* Create a collection filled with the same element, using a list of keys * Create a collection filled with the same element, using a list of keys
* *
@ -235,8 +254,10 @@ interface IImmutableCollection extends IteratorAggregate, JsonSerializable, Coun
* @param TValue $value The value to fill the collection * @param TValue $value The value to fill the collection
* *
* @return static<TKey, TValue> The result collection * @return static<TKey, TValue> The result collection
*
* @noinspection PhpMissingReturnTypeInspection
*/ */
public static function fillWithKeys (IImmutableCollection $keys, $value): static; public static function fillWithKeys (IImmutableCollection $keys, $value);
/** /**
* Merge the current collection with one or multiple collections * Merge the current collection with one or multiple collections
@ -244,6 +265,8 @@ interface IImmutableCollection extends IteratorAggregate, JsonSerializable, Coun
* @param IImmutableCollection<TKey, TValue> ...$collections The other collections to merge * @param IImmutableCollection<TKey, TValue> ...$collections The other collections to merge
* *
* @return static<TKey, TValue> The result collection * @return static<TKey, TValue> The result collection
*
* @noinspection PhpMissingReturnTypeInspection
*/ */
public function mergeWith (IImmutableCollection ...$collections): static; public function mergeWith (IImmutableCollection ...$collections);
} }

@ -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>&gt; 0 if value1 is after value2 * <br>&gt; 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);
} }
} }

@ -7,9 +7,11 @@ namespace jrosset\Collections;
*/ */
trait TInsensitiveCaseKey { trait TInsensitiveCaseKey {
/** /**
* @inheritDoc * Normalize a key
* *
* @noinspection PhpMissingReturnTypeInspection * @param array-key $key The key to normalize
*
* @return array-key The normalized key
*/ */
protected function _normalizeKey ($key) { protected function _normalizeKey ($key) {
return mb_strtolower($key); return mb_strtolower($key);

@ -12,7 +12,7 @@ trait TUniqueValues {
/** /**
* @inheritDoc * @inheritDoc
*/ */
protected function _set ($key, $value): static { protected function _set ($key, $value): self {
if ($this->contains($value)) { if ($this->contains($value)) {
return $this; return $this;
} }
@ -23,7 +23,7 @@ trait TUniqueValues {
/** /**
* @inheritDoc * @inheritDoc
*/ */
protected function _add (...$values): static { protected function _add (...$values): self {
foreach ($values as $value) { foreach ($values as $value) {
if ($this->contains($value)) { if ($this->contains($value)) {
continue; continue;

Loading…
Cancel
Save