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

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

@ -3,8 +3,8 @@
namespace jrosset\Collections;
use Closure;
use Exception;
use OutOfBoundsException;
use Throwable;
/**
* Interface for a collection
@ -22,16 +22,20 @@ interface ICollection extends IImmutableCollection {
* @param TValue $value The value
*
* @return $this
*
* @noinspection PhpMissingReturnTypeInspection
*/
public function set ($key, $value): static;
public function set ($key, $value);
/**
* Merge one or multiple collections into the current one
*
* @param IImmutableCollection<TKey, TValue> ...$collections The collections to merge
*
* @return $this
*
* @noinspection PhpMissingReturnTypeInspection
*/
public function merge (IImmutableCollection ...$collections): static;
public function merge (IImmutableCollection ...$collections);
/**
* Add one or multiple values
@ -39,16 +43,20 @@ interface ICollection extends IImmutableCollection {
* @param TValue ...$values The values
*
* @return $this
*
* @noinspection PhpMissingReturnTypeInspection
*/
public function add (...$values): static;
public function add (...$values);
/**
* Add the <b>values</b> of one or multiple collections
*
* @param IImmutableCollection<TKey, TValue> ...$collections The collections to add
*
* @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
@ -56,31 +64,39 @@ interface ICollection extends IImmutableCollection {
* @param TValue ...$values The values
*
* @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
*
* @param IImmutableCollection<TKey, TValue> ...$collections The collections to add
*
* @return $this
*
* @noinspection PhpMissingReturnTypeInspection
*/
public function prependCollection (IImmutableCollection ...$collections): static;
public function prependCollection (IImmutableCollection ...$collections);
/**
* Empty the collection
*
* @return $this
*
* @noinspection PhpMissingReturnTypeInspection
*/
public function clear (): static;
public function clear ();
/**
* Remove a value from it's key
*
* @param TKey $key The key
*
* @return $this
*
* @noinspection PhpMissingReturnTypeInspection
*/
public function remove ($key): static;
public function remove ($key);
/**
* Delete all instances of a value
*
@ -88,8 +104,10 @@ interface ICollection extends IImmutableCollection {
* @param bool $strict Strict comparison ?
*
* @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
@ -119,8 +137,10 @@ interface ICollection extends IImmutableCollection {
* @return $this
*
* @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
@ -128,24 +148,30 @@ interface ICollection extends IImmutableCollection {
* @param Closure(TKey, TValue):bool $filter The filtering predicate
*
* @return $this
*
* @noinspection PhpMissingReturnTypeInspection
*/
public function filterSelf (Closure $filter): static;
public function filterSelf (Closure $filter);
/**
* Keep only non-empty elements
*
* Use {@see https://www.php.net/manual/function.empty.php empty} to check if element is empty or not
*
* @return $this
*
* @noinspection PhpMissingReturnTypeInspection
*/
public function removeEmpties (): static;
public function removeEmpties ();
/**
* Applied <b>$process</b> on all elements
*
* @param Closure(TKey, TValue): TValue $process The process function to apply on each element
*
* @return $this
*
* @noinspection PhpMissingReturnTypeInspection
*/
public function mapSelf (Closure $process): static;
public function mapSelf (Closure $process);
/**
* Sort the elements (by value)
@ -158,9 +184,11 @@ interface ICollection extends IImmutableCollection {
*
* @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
*
@ -171,7 +199,9 @@ interface ICollection extends IImmutableCollection {
*
* @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 JsonSerializable;
use OutOfBoundsException;
use Serializable;
use Throwable;
/**
@ -20,7 +21,7 @@ use Throwable;
* @implements ArrayAccess<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
*
@ -130,8 +131,10 @@ interface IImmutableCollection extends IteratorAggregate, JsonSerializable, Coun
* @return static<TKey, TValue> The result collection
*
* @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
@ -139,16 +142,20 @@ interface IImmutableCollection extends IteratorAggregate, JsonSerializable, Coun
* @param Closure(TKey, TValue):bool $filter The filtering predicate
*
* @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
*
* 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
*
* @noinspection PhpMissingReturnTypeInspection
*/
public function withoutEmpties (): static;
public function withoutEmpties ();
/**
* 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
*
* @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)
@ -172,8 +181,10 @@ interface IImmutableCollection extends IteratorAggregate, JsonSerializable, Coun
* @return static<TKey, TValue> The result collection
*
* @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
*
@ -185,8 +196,10 @@ interface IImmutableCollection extends IteratorAggregate, JsonSerializable, Coun
* @return static<TKey, TValue> The result collection
*
* @throws Throwable If an error occurs
*
* @noinspection PhpMissingReturnTypeInspection
*/
public function sortByKey (Closure|IComparator $sorter): static;
public function sortByKey ($sorter);
/**
* The list of all keys
@ -198,8 +211,10 @@ interface IImmutableCollection extends IteratorAggregate, JsonSerializable, Coun
* 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
@ -216,8 +231,10 @@ interface IImmutableCollection extends IteratorAggregate, JsonSerializable, Coun
* @param string $separator The split separator
*
* @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
@ -226,8 +243,10 @@ interface IImmutableCollection extends IteratorAggregate, JsonSerializable, Coun
* @param TValue $value The value to fill the 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
*
@ -235,8 +254,10 @@ interface IImmutableCollection extends IteratorAggregate, JsonSerializable, Coun
* @param TValue $value The value to fill the 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
@ -244,6 +265,8 @@ interface IImmutableCollection extends IteratorAggregate, JsonSerializable, Coun
* @param IImmutableCollection<TKey, TValue> ...$collections The other collections to merge
*
* @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 InvalidArgumentException;
use OutOfBoundsException;
use ReflectionFunction;
use Traversable;
/**
@ -64,8 +63,10 @@ class ImmutableCollection implements IImmutableCollection {
* @return $this
*
* @internal
*
* @noinspection PhpMissingReturnTypeInspection
*/
protected function _set ($key, $value): static {
protected function _set ($key, $value) {
$this->elements[$this->_normalizeKey($key)] = $value;
return $this;
}
@ -77,8 +78,10 @@ class ImmutableCollection implements IImmutableCollection {
* @return $this
*
* @internal
*
* @noinspection PhpMissingReturnTypeInspection
*/
protected function _merge (IImmutableCollection ...$collections): static {
protected function _merge (IImmutableCollection ...$collections) {
foreach ($collections as $collection) {
foreach ($collection as $key => $value) {
$this->_set($key, $value);
@ -94,8 +97,10 @@ class ImmutableCollection implements IImmutableCollection {
* @return $this
*
* @internal
*
* @noinspection PhpMissingReturnTypeInspection
*/
protected function _add (...$values): static {
protected function _add (...$values) {
foreach ($values as $value) {
$this->elements[] = $value;
}
@ -107,8 +112,10 @@ class ImmutableCollection implements IImmutableCollection {
* @param IImmutableCollection ...$collections The collections to add
*
* @return $this
*
* @noinspection PhpMissingReturnTypeInspection
*/
protected function _addCollection (IImmutableCollection ...$collections): static {
protected function _addCollection (IImmutableCollection ...$collections) {
foreach ($collections as $collection) {
foreach ($collection as $value) {
$this->_add($value);
@ -151,25 +158,25 @@ class ImmutableCollection implements IImmutableCollection {
/**
* @inheritDoc
*/
public function offsetExists (mixed $offset): bool {
public function offsetExists ($offset): bool {
return $this->exists($offset);
}
/**
* @inheritDoc
*/
public function offsetGet (mixed $offset): mixed {
public function offsetGet ($offset) {
return $this->get($offset);
}
/**
* @inheritDoc
*/
public function offsetSet (mixed $offset, mixed $value): void {
public function offsetSet ($offset, $value): void {
throw new ImmutableException();
}
/**
* @inheritDoc
*/
public function offsetUnset (mixed $offset): void {
public function offsetUnset ($offset): void {
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
*
* @param array $data The list of serialized properties
*
* @return void
* @inheritDoc
*/
public function serialize (): ?string {
return serialize($this->elements);
}
/**
* @inheritDoc
*/
public function __unserialize (array $data): void {
$this->_initialize($data['elements']);
public function unserialize ($data) {
$this->_initialize(unserialize($data));
}
/**
@ -230,7 +239,7 @@ class ImmutableCollection implements IImmutableCollection {
/**
* @inheritDoc
*/
public function get ($key): mixed {
public function get ($key) {
return $this->elements[$this->_normalizeKey($key)] ?? null;
}
/**
@ -289,14 +298,11 @@ class ImmutableCollection implements IImmutableCollection {
/**
* @inheritDoc
*/
public function slice (int $offset, ?int $length = null): static {
$output = new static();
if ($this->count() === 0) {
return $output;
}
public function slice (int $offset, ?int $length = null) {
$this->_checkOffset($offset);
$output = new static();
$currentIndex = 0;
foreach ($this->elements as $key => $value) {
if ($currentIndex++ < $offset) {
@ -315,7 +321,7 @@ class ImmutableCollection implements IImmutableCollection {
/**
* @inheritDoc
*/
public function filter (Closure $filter): static {
public function filter (Closure $filter) {
$output = new static();
foreach ($this->elements as $key => $value) {
if (!$filter($key, $value)) {
@ -328,7 +334,7 @@ class ImmutableCollection implements IImmutableCollection {
/**
* @inheritDoc
*/
public function withoutEmpties (): static {
public function withoutEmpties () {
return $this->filter(function ($value) {
return empty($value);
});
@ -336,21 +342,10 @@ class ImmutableCollection implements IImmutableCollection {
/**
* @inheritDoc
*/
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
public function map (Closure $process) {
$output = new static();
foreach ($this->elements as $key => $value) {
$output->_set($key, $callProcessWithKey ? $process($key, $value) : $process($value));
$output->_set($key, $process($key, $value));
}
return $output;
}
@ -358,7 +353,7 @@ class ImmutableCollection implements IImmutableCollection {
/**
* @inheritDoc
*/
public function sort (Closure|IComparator|null $sorter = null): static {
public function sort ($sorter = null) {
$elements = $this->toArray();
uasort($elements, self::_normalizeSorter($sorter));
@ -367,7 +362,7 @@ class ImmutableCollection implements IImmutableCollection {
/**
* @inheritDoc
*/
public function sortByKey (Closure|IComparator $sorter): static {
public function sortByKey ($sorter) {
$elements = $this->toArray();
uksort($elements, self::_normalizeSorter($sorter));
@ -389,7 +384,7 @@ class ImmutableCollection implements IImmutableCollection {
* <br>= 0 if value1 equals 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) {
return function (IComparable $value1, IComparable $value2): int {
return $value1->compareTo($value2);
@ -414,14 +409,14 @@ class ImmutableCollection implements IImmutableCollection {
/**
* @inheritDoc
*/
public function values (): static {
public function values () {
return new static(array_values($this->elements));
}
/**
* @inheritDoc
*/
public function jsonSerialize (): mixed {
public function jsonSerialize () {
return $this->elements;
}
@ -434,14 +429,14 @@ class ImmutableCollection implements IImmutableCollection {
/**
* @inheritDoc
*/
public static function split (string $value, string $separator): static {
public static function split (string $value, string $separator) {
return new static (explode($separator, $value));
}
/**
* @inheritDoc
*/
public static function fill (int $size, $value): static {
public static function fill (int $size, $value) {
$keys = new ImmutableCollection();
for ($curr = 0; $curr < $size; $curr++) {
$keys->_add($curr);
@ -452,7 +447,7 @@ class ImmutableCollection implements IImmutableCollection {
/**
* @inheritDoc
*/
public static function fillWithKeys (IImmutableCollection $keys, $value): static {
public static function fillWithKeys (IImmutableCollection $keys, $value) {
$collection = new static();
foreach ($keys as $key) {
$collection->_set($key, $value);
@ -463,7 +458,7 @@ class ImmutableCollection implements IImmutableCollection {
/**
* @inheritDoc
*/
public function mergeWith (IImmutableCollection ...$collections): static {
public function mergeWith (IImmutableCollection ...$collections) {
return (clone $this)->_merge(...$collections);
}
}

@ -7,9 +7,11 @@ namespace jrosset\Collections;
*/
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) {
return mb_strtolower($key);

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

Loading…
Cancel
Save