ICollection: add "self" version of multiple functions

+ Simplify/Correct template declarations
+ Fix method signatures (self → static)
2.x 2.6.0
Julien Rosset 2 years ago
parent a603f32fd6
commit eb3d47e640

@ -2,52 +2,55 @@
namespace jrosset\Collections;
use Closure;
/**
* A collection
*
* @psalm-template TKey of array-key
* @template-covariant TValue
* @template-extends ImmutableCollection<TKey, TValue>
* @template-implements ICollection<TKey, TValue>
* @template TKey of array-key
* @template TValue
*
* @implements ImmutableCollection<TKey, TValue>
* @implements ICollection<TKey, TValue>
*/
class Collection extends ImmutableCollection implements ICollection {
/**
* @inheritDoc
*/
public function set ($key, $value): self {
public function set ($key, $value) {
return $this->_set($key, $value);
}
/**
* @inheritDoc
*/
public function merge (IImmutableCollection ...$collections): ICollection {
public function merge (IImmutableCollection ...$collections) {
return $this->_merge(...$collections);
}
/**
* @inheritDoc
*/
public function add (...$values): self {
public function add (...$values) {
return $this->_add(...$values);
}
/**
* @inheritDoc
*/
public function addCollection (IImmutableCollection ...$collections): ICollection {
public function addCollection (IImmutableCollection ...$collections) {
return $this->_addCollection(...$collections);
}
/**
* @inheritDoc
*/
public function prepend (...$values): ICollection {
public function prepend (...$values) {
array_unshift($this->elements, ...$values);
return $this;
}
/**
* @inheritDoc
*/
public function prependCollection (IImmutableCollection ...$collections): ICollection {
public function prependCollection (IImmutableCollection ...$collections) {
$prepend = array_merge(
...array_map(
function (IImmutableCollection $collection): array {
@ -62,21 +65,21 @@ class Collection extends ImmutableCollection implements ICollection {
/**
* @inheritDoc
*/
public function clear (): self {
public function clear () {
$this->_initialize();
return $this;
}
/**
* @inheritDoc
*/
public function remove ($key): self {
public function remove ($key) {
unset($this->elements[$this->_normalizeKey($key)]);
return $this;
}
/**
* @inheritDoc
*/
public function removeValue ($value, bool $strict = false): self {
public function removeValue ($value, bool $strict = false) {
foreach ($this->elements as $currentKey => $currentValue) {
if (($strict && $value === $currentValue) || (!$strict && $value == $currentValue)) {
unset($this->elements[$currentKey]);
@ -97,4 +100,50 @@ class Collection extends ImmutableCollection implements ICollection {
public function offsetUnset ($offset): void {
$this->remove($offset);
}
/**
* @inheritDoc
*/
public function sliceSelf (int $offset, ?int $length = null) {
$this->elements = array_slice($this->elements, $offset, $length, true);
return $this;
}
/**
* @inheritDoc
*/
public function filterSelf (Closure $filter) {
$this->elements = array_filter($this->elements, $filter);
return $this;
}
/**
* @inheritDoc
*/
public function removeEmpties () {
return $this->filterSelf(function ($value) {
return empty($value);
});
}
/**
* @inheritDoc
*/
public function mapSelf (Closure $process) {
$this->elements = array_map($process, $this->elements);
return $this;
}
/**
* @inheritDoc
*/
public function sortSelf ($sorter = null) {
uasort($this->elements, self::_normalizeSorter($sorter));
return $this;
}
/**
* @inheritDoc
*/
public function sortSelfByKey ($sorter) {
uksort($this->elements, self::_normalizeSorter($sorter));
return $this;
}
}

@ -5,15 +5,14 @@ namespace jrosset\Collections;
/**
* Interface for PHP-native array cast
*
* @psalm-template TKey of array-key
* @template-covariant TValue
* @template TKey of array-key
* @template TValue
*/
interface IArrayCast {
/**
* Transform to a PHP-native array
*
* @return array The native array
* @psalm-return array<TKey, TValue>
* @return array<TKey, TValue> The native array
*/
public function toArray (): array;
}

@ -2,98 +2,186 @@
namespace jrosset\Collections;
use Closure;
use Exception;
/**
* Interface for a collection
*
* @psalm-template TKey of array-key
* @template-covariant TValue
* @template-extends IImmutableCollection<TKey, TValue>
* @template TKey of array-key
* @template TValue
*
* @implements IImmutableCollection<TKey, TValue>
*/
interface ICollection extends IImmutableCollection {
/**
* Set a value to a key
*
* @param array-key $key The key
* @param mixed $value The value
*
* @psalm-param TKey $key
* @psalm-param TValue $value
* @param TKey $key The key
* @param TValue $value The value
*
* @return $this
*
* @noinspection PhpMissingReturnTypeInspection
*/
public function set ($key, $value): self;
public function set ($key, $value);
/**
* Merge one or multiple collections in the current one
* Merge one or multiple collections into the current one
*
* @param IImmutableCollection ...$collections The collections to merge
* @param IImmutableCollection<TKey, TValue> ...$collections The collections to merge
*
* @return $this
*
* @noinspection PhpMissingReturnTypeInspection
*/
public function merge (IImmutableCollection ...$collections): self;
public function merge (IImmutableCollection ...$collections);
/**
* Add one or multiple values
*
* @param mixed ...$values The values
*
* @psalm-param TValue ...$values
* @param TValue ...$values The values
*
* @return $this
*
* @noinspection PhpMissingReturnTypeInspection
*/
public function add (...$values): self;
public function add (...$values);
/**
* Add the <b>values</b> of one or multiple collections
*
* @param IImmutableCollection ...$collections The collections to add
* @param IImmutableCollection<TKey, TValue> ...$collections The collections to add
*
* @return mixed
* @return $this
*
* @noinspection PhpMissingReturnTypeInspection
*/
public function addCollection (IImmutableCollection ...$collections): self;
public function addCollection (IImmutableCollection ...$collections);
/**
* Add one or multiple values at the beginning of the current collection
*
* @param mixed ...$values The values
*
* @psalm-param TValue ...$values
* @param TValue ...$values The values
*
* @return $this
*
* @noinspection PhpMissingReturnTypeInspection
*/
public function prepend (...$values): self;
public function prepend (...$values);
/**
* Add the <b>values</b> of one or multiple collections at the beginning of the current collection
*
* @param IImmutableCollection ...$collections The collections to add
* @param IImmutableCollection<TKey, TValue> ...$collections The collections to add
*
* @return mixed
* @return $this
*
* @noinspection PhpMissingReturnTypeInspection
*/
public function prependCollection (IImmutableCollection ...$collections): self;
public function prependCollection (IImmutableCollection ...$collections);
/**
* Empties the collection
* Empty the collection
*
* @return $this
*
* @noinspection PhpMissingReturnTypeInspection
*/
public function clear (): self;
public function clear ();
/**
* Delete a key
*
* @param array-key $key The key
* Remove a value from it's key
*
* @psalm-param TKey $key
* @param TKey $key The key
*
* @return $this
*
* @noinspection PhpMissingReturnTypeInspection
*/
public function remove ($key): self;
public function remove ($key);
/**
* Delete all instances of a value
*
* @param mixed $value The value
* @param bool $strict Strict comparison ?
* @param TValue $value The value
* @param bool $strict Strict comparison ?
*
* @return $this
*
* @noinspection PhpMissingReturnTypeInspection
*/
public function removeValue ($value, bool $strict = false);
/**
* Keep only a slice of the collection
*
* Preserves the keys
*
* @param int $offset The start offset
* @param int|null $length The maximum length. Null if until end of the collection
*
* @return $this
*
* @noinspection PhpMissingReturnTypeInspection
*/
public function sliceSelf (int $offset, ?int $length = null);
/**
* Keep only elements that satisfy predicate $filter
*
* @param Closure(TKey, TValue):bool $filter The filtering predicate
*
* @return $this
*
* @noinspection PhpMissingReturnTypeInspection
*/
public function filterSelf (Closure $filter);
/**
* Keep only non-empty elements
*
* @psalm-param TValue $value
* 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 ();
/**
* 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);
/**
* Sort the elements (by value)
*
* @param null|IComparator|Closure(TValue, TValue):int $sorter The sorting method ; Null if values are object implementing {@see IComparable}.
* <br>Return :
* <br>&lt; 0 if value1 is before value2
* <br>= 0 if value1 equals value2
* <br>&gt; 0 if value1 is after value2
*
* @return $this
*
* @throws Exception If an error occurs
*
* @noinspection PhpMissingReturnTypeInspection
*/
public function sortSelf ($sorter = null);
/**
* Sort the elements by key
*
* @param IComparator|Closure(TKey, TKey):int $sorter The sorting method. Return :
* <br>&lt; 0 if key1 is before key2
* <br>= 0 if key1 equals key2
* <br>&gt; 0 if key1 is after key2
*
* @return $this
*
* @throws Exception If an error occurs
*
* @noinspection PhpMissingReturnTypeInspection
*/
public function removeValue ($value, bool $strict = false): self;
public function sortSelfByKey ($sorter);
}

@ -13,11 +13,12 @@ use Throwable;
/**
* Interface for an immutable (read-only) collection
*
* @psalm-template TKey of array-key
* @template-covariant TValue
* @template-extends IteratorAggregate<TKey, TValue>
* @template-extends ArrayAccess<TKey, TValue>
* @template-extends IArrayCast<TKey, TValue>
* @template TKey of array-key
* @template TValue
*
* @implements IteratorAggregate<TKey, TValue>
* @implements ArrayAccess<TKey, TValue>
* @implements IArrayCast<TKey, TValue>
*/
interface IImmutableCollection extends IteratorAggregate, JsonSerializable, Serializable, Countable, ArrayAccess, IArrayCast {
/**
@ -30,9 +31,7 @@ interface IImmutableCollection extends IteratorAggregate, JsonSerializable, Seri
/**
* Checks if a key exists
*
* @param array-key $key The key
*
* @psalm-param TKey $key
* @param TKey $key The key
*
* @return bool TRUE if the key exists, FALSE otherwise
*/
@ -40,10 +39,8 @@ interface IImmutableCollection extends IteratorAggregate, JsonSerializable, Seri
/**
* Checks if contains a value
*
* @param mixed $value The value
* @param bool $strict Strict comparison ?
*
* @psalm-param TValue $value
* @param TValue $value The value
* @param bool $strict Strict comparison ?
*
* @return bool TRUE if the value exists, FALSE otherwise
*/
@ -52,24 +49,18 @@ interface IImmutableCollection extends IteratorAggregate, JsonSerializable, Seri
/**
* Get the value of a key or null if not found
*
* @param array-key $key The key
* @param TKey $key The key
*
* @psalm-param TKey $key
*
* @return mixed The value
* @psalm-return TValue|null
* @return TValue|null The value
*/
public function get ($key);
/**
* Get the first key of a value or null if not found
*
* @param mixed $value The value
* @param bool $strict Strict comparison ?
*
* @psalm-param TValue $value
* @param TValue $value The value
* @param bool $strict Strict comparison ?
*
* @return array-key
* @psalm-return TKey|null
* @return TKey|null
*/
public function key ($value, bool $strict = false);
@ -81,54 +72,47 @@ interface IImmutableCollection extends IteratorAggregate, JsonSerializable, Seri
* @param int $offset The start offset
* @param int|null $length The maximum length. Null if until end of the collection
*
* @return static
* @psalm-return static<TKey, TValue>
* @return static<TKey, TValue> The result collection
*
* @noinspection PhpMissingReturnTypeInspection
*/
public function slice (int $offset, ?int $length = null): self;
public function slice (int $offset, ?int $length = null);
/**
* Get a collection of all elements that satisfy predicate $filter
*
* @param Closure $filter The filtering predicate
* @param Closure(TKey, TValue):bool $filter The filtering predicate
*
* @psalm-param Closure(TKey, TValue):bool $filter
* @return static<TKey, TValue> The result collection
*
* @return static The result collection
* @psalm-return static<TKey, TValue>
* @noinspection PhpMissingReturnTypeInspection
*/
public function filter (Closure $filter): self;
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 The result collection
*/
public function withoutEmpties (): self;
/**
* Remove all empty elements on current collection
*
* 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 $this
* @noinspection PhpMissingReturnTypeInspection
*/
public function removeEmpties (): self;
public function withoutEmpties ();
/**
* A new collection with $process applied on all elements
* A new collection with <b>$process</b> applied on all elements
*
* @psalm-template TResultValue
* @template TResultValue
*
* @param Closure $process The process function to apply on each element
* @param Closure(TKey, TValue): TResultValue $process The process function to apply on each element
*
* @psalm-param Closure(TKey, TValue): TResultValue $process
* @return static<TKey, TResultValue> The result collection
*
* @return static
* @psalm-return static<TKey, TResultValue>
* @noinspection PhpMissingReturnTypeInspection
*/
public function map (Closure $process): self;
public function map (Closure $process);
/**
* Sort the elements (by value)
* Get a collection with the elements sorted (by value)
*
* @param null|IComparator|Closure(TValue, TValue):int $sorter The sorting method ; Null if values are object implementing {@see IComparable}.
* <br>Return :
@ -136,8 +120,7 @@ interface IImmutableCollection extends IteratorAggregate, JsonSerializable, Seri
* <br>= 0 if value1 equals value2
* <br>&gt; 0 if value1 is after value2
*
* @return static The sorted collection
* @psalm-return static<TKey, TValue>
* @return static<TKey, TValue> The result collection
*
* @throws Throwable If an error occurs
*
@ -145,15 +128,14 @@ interface IImmutableCollection extends IteratorAggregate, JsonSerializable, Seri
*/
public function sort ($sorter = null);
/**
* Sort the elements by key
* Get a collection with the elements sorted by key
*
* @param IComparator|Closure(TKey, TKey):int $sorter The sorting method. Return :
* <br>&lt; 0 if key1 is before key2
* <br>= 0 if key1 equals key2
* <br>&gt; 0 if key1 is after key2
*
* @return static The sorted collection
* @psalm-return static<TKey, TValue>
* @return static<TKey, TValue> The result collection
*
* @throws Throwable If an error occurs
*
@ -164,17 +146,17 @@ interface IImmutableCollection extends IteratorAggregate, JsonSerializable, Seri
/**
* The list of all keys
*
* @return static The list of all keys
* @psalm-return static<int, TKey>
* @return IImmutableCollection<int, TKey> The list of all keys
*/
public function keys (): self;
public function keys (): IImmutableCollection;
/**
* The list of all values
*
* @return static The list of all values
* @psalm-return static<int, TValue>
* @return static<int, TValue> The list of all values
*
* @noinspection PhpMissingReturnTypeInspection
*/
public function values (): self;
public function values ();
/**
* Join all values with a separator
@ -190,16 +172,20 @@ interface IImmutableCollection extends IteratorAggregate, JsonSerializable, Seri
* @param string $value The value to split
* @param string $separator The split separator
*
* @return static
* @return static<int, string> The result collection
*
* @noinspection PhpMissingReturnTypeInspection
*/
public static function split (string $value, string $separator): self;
public static function split (string $value, string $separator);
/**
* Combine the current collection with one or multiple other collections
* Merge the current collection with one or multiple collections
*
* @param IImmutableCollection ...$collections The other collections to combine
* @param IImmutableCollection<TKey, TValue> ...$collections The other collections to merge
*
* @return static The combine collection
* @return static<TKey, TValue> The result collection
*
* @noinspection PhpMissingReturnTypeInspection
*/
public function combineWith (IImmutableCollection ...$collections): self;
public function mergeWith (IImmutableCollection ...$collections);
}

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

@ -5,9 +5,10 @@ namespace jrosset\Collections;
/**
* A collection with insensitive case keys
*
* @psalm-template TKey of array-key
* @template-covariant TValue
* @template-extends Collection<TKey, TValue>
* @template TKey of array-key
* @template TValue
*
* @implements Collection<TKey, TValue>
*/
class InsensitiveCaseKeyCollection extends Collection {
use TInsensitiveCaseKey;

@ -5,9 +5,10 @@ namespace jrosset\Collections;
/**
* An immutable collection with insensitive case keys
*
* @psalm-template TKey of array-key
* @template-covariant TValue
* @template-extends ImmutableCollection<TKey, TValue>
* @template TKey of array-key
* @template TValue
*
* @implements ImmutableCollection<TKey, TValue>
*/
class InsensitiveCaseKeyImmutableCollection extends ImmutableCollection implements IImmutableCollection {
use TInsensitiveCaseKey;

@ -5,9 +5,8 @@ namespace jrosset\Collections;
/**
* Implementation for unique collection
*
* @psalm-template TKey of array-key
* @template-covariant TValue
* @template-extends ImmutableCollection<TKey, TValue>
* @template TKey of array-key
* @template TValue
*/
trait TUniqueValues {
/**

@ -5,10 +5,11 @@ namespace jrosset\Collections;
/**
* A collection with unique values
*
* @psalm-template TKey of array-key
* @template-covariant TValue
* @template-extends Collection<TKey, TValue>
* @template-extends TUniqueValues<TKey, TValue>
* @template TKey of array-key
* @template TValue
*
* @implements Collection<TKey, TValue>
* @implements TUniqueValues<TKey, TValue>
*/
class UniqueCollection extends Collection {
use TUniqueValues;

@ -5,10 +5,11 @@ namespace jrosset\Collections;
/**
* An immutable collection with unique values
*
* @psalm-template TKey of array-key
* @template-covariant TValue
* @template-extends ImmutableCollection<TKey, TValue>
* @template-extends TUniqueValues<TKey, TValue>
* @template TKey of array-key
* @template TValue
*
* @implements ImmutableCollection<TKey, TValue>
* @implements TUniqueValues<TKey, TValue>
*/
class UniqueImmutableCollection extends ImmutableCollection {
use TUniqueValues;

@ -65,12 +65,15 @@ function getMeasures (): Collection {
return $measures;
}
$measures = getMeasures();
$measures->sortSelf();
echo 'Measures :' . PHP_EOL;
foreach (getMeasures()->sort() as $no => $measure) {
foreach ($measures as $no => $measure) {
echo "\t#" . $no . ' = ' . $measure->asString() . PHP_EOL;
}
echo 'Measures (reversed) :' . PHP_EOL;
foreach (getMeasures()->sort(new MeasureReverseComparator()) as $no => $measure) {
foreach ($measures->sort(new MeasureReverseComparator()) as $no => $measure) {
echo "\t#" . $no . ' = ' . $measure->asString() . PHP_EOL;
}
Loading…
Cancel
Save