Compatible with PHP 8.1

master
Julien Rosset 2 years ago
parent 3da6845b05
commit 6c69b5504b

@ -5,7 +5,7 @@
"minimum-stability": "stable", "minimum-stability": "stable",
"require": { "require": {
"php": "^7.4 || ^8.0" "php": "^8.1"
}, },
"autoload": { "autoload": {
"psr-4": { "psr-4": {

@ -14,7 +14,7 @@ class Collection extends ImmutableCollection implements ICollection {
/** /**
* @inheritDoc * @inheritDoc
*/ */
public function set ($key, $value): self { public function set (int|string $key, mixed $value): self {
return $this->_set($key, $value); return $this->_set($key, $value);
} }
/** /**
@ -27,7 +27,7 @@ class Collection extends ImmutableCollection implements ICollection {
/** /**
* @inheritDoc * @inheritDoc
*/ */
public function add (...$values): self { public function add (mixed ...$values): self {
return $this->_add(...$values); return $this->_add(...$values);
} }
/** /**
@ -40,7 +40,7 @@ class Collection extends ImmutableCollection implements ICollection {
/** /**
* @inheritDoc * @inheritDoc
*/ */
public function prepend (...$values): ICollection { public function prepend (mixed ...$values): ICollection {
array_unshift($this->elements, ...$values); array_unshift($this->elements, ...$values);
return $this; return $this;
} }
@ -69,14 +69,14 @@ class Collection extends ImmutableCollection implements ICollection {
/** /**
* @inheritDoc * @inheritDoc
*/ */
public function remove ($key): self { public function remove (int|string $key): self {
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): self { public function removeValue (mixed $value, bool $strict = false): self {
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]);
@ -88,13 +88,13 @@ class Collection extends ImmutableCollection implements ICollection {
/** /**
* @inheritDoc * @inheritDoc
*/ */
public function offsetSet ($offset, $value): void { public function offsetSet (mixed $offset, mixed $value): void {
$this->set($offset, $value); $this->set($offset, $value);
} }
/** /**
* @inheritDoc * @inheritDoc
*/ */
public function offsetUnset ($offset): void { public function offsetUnset (mixed $offset): void {
$this->remove($offset); $this->remove($offset);
} }
} }

@ -21,7 +21,7 @@ interface ICollection extends IImmutableCollection {
* *
* @return $this * @return $this
*/ */
public function set ($key, $value): self; public function set (int|string $key, mixed $value): self;
/** /**
* Merge one or multiple collections in the current one * Merge one or multiple collections in the current one
* *
@ -40,7 +40,7 @@ interface ICollection extends IImmutableCollection {
* *
* @return $this * @return $this
*/ */
public function add (...$values): self; public function add (mixed ...$values): self;
/** /**
* Add the <b>values</b> of one or multiple collections * Add the <b>values</b> of one or multiple collections
* *
@ -59,7 +59,7 @@ interface ICollection extends IImmutableCollection {
* *
* @return $this * @return $this
*/ */
public function prepend (...$values): self; public function prepend (mixed ...$values): self;
/** /**
* 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
* *
@ -84,7 +84,7 @@ interface ICollection extends IImmutableCollection {
* *
* @return $this * @return $this
*/ */
public function remove ($key): self; public function remove (int|string $key): self;
/** /**
* Delete all instances of a value * Delete all instances of a value
* *
@ -95,5 +95,5 @@ interface ICollection extends IImmutableCollection {
* *
* @return $this * @return $this
*/ */
public function removeValue ($value, bool $strict = false): self; public function removeValue (mixed $value, bool $strict = false): self;
} }

@ -7,7 +7,6 @@ use Closure;
use Countable; use Countable;
use IteratorAggregate; use IteratorAggregate;
use JsonSerializable; use JsonSerializable;
use Serializable;
/** /**
* Interface for an immutable (read-only) collection * Interface for an immutable (read-only) collection
@ -18,7 +17,7 @@ use Serializable;
* @template-extends ArrayAccess<TKey, TValue> * @template-extends ArrayAccess<TKey, TValue>
* @template-extends IArrayCast<TKey, TValue> * @template-extends IArrayCast<TKey, TValue>
*/ */
interface IImmutableCollection extends IteratorAggregate, JsonSerializable, Serializable, Countable, ArrayAccess, IArrayCast { interface IImmutableCollection extends IteratorAggregate, JsonSerializable, Countable, ArrayAccess, IArrayCast {
/** /**
* Checks if the collection is empty * Checks if the collection is empty
* *
@ -35,7 +34,7 @@ interface IImmutableCollection extends IteratorAggregate, JsonSerializable, Seri
* *
* @return bool TRUE if the key exists, FALSE otherwise * @return bool TRUE if the key exists, FALSE otherwise
*/ */
public function exists ($key): bool; public function exists (int|string $key): bool;
/** /**
* Checks if contains a value * Checks if contains a value
* *
@ -46,7 +45,7 @@ interface IImmutableCollection extends IteratorAggregate, JsonSerializable, Seri
* *
* @return bool TRUE if the value exists, FALSE otherwise * @return bool TRUE if the value exists, FALSE otherwise
*/ */
public function contains ($value, bool $strict = false): bool; public function contains (mixed $value, bool $strict = false): bool;
/** /**
* Get the value of a key or null if not found * Get the value of a key or null if not found
@ -58,7 +57,7 @@ interface IImmutableCollection extends IteratorAggregate, JsonSerializable, Seri
* @return mixed The value * @return mixed The value
* @psalm-return TValue|null * @psalm-return TValue|null
*/ */
public function get ($key); public function get (int|string $key): mixed;
/** /**
* Get the first key of a value or null if not found * Get the first key of a value or null if not found
* *
@ -67,10 +66,10 @@ interface IImmutableCollection extends IteratorAggregate, JsonSerializable, Seri
* *
* @psalm-param TValue $value * @psalm-param TValue $value
* *
* @return array-key * @return array-key|null
* @psalm-return TKey|null * @psalm-return TKey|null
*/ */
public function key ($value, bool $strict = false); public function key (mixed $value, bool $strict = false): int|string|null;
/** /**
* Extract a slice of the collection * Extract a slice of the collection

@ -5,7 +5,6 @@ namespace jrosset\Collections;
use ArrayIterator; use ArrayIterator;
use Closure; use Closure;
use InvalidArgumentException; use InvalidArgumentException;
use JsonException;
use Traversable; use Traversable;
/** /**
@ -27,28 +26,27 @@ class ImmutableCollection implements IImmutableCollection {
/** /**
* Initialise a new collection * Initialise a new collection
* *
* @param array|Traversable|null $other The initial values * @param iterable|null $other The initial values
* *
* @psalm-param array<TKey, TValue>|Traversable<TKey, TValue>|null * @psalm-param array<TKey, TValue>|Traversable<TKey, TValue>|null $other
* *
* @throws InvalidArgumentException If the initial values aren't valid * @throws InvalidArgumentException If the initial values aren't valid
*/ */
public function __construct ($other = null) { public function __construct (?iterable $other = null) {
$this->_initialize($other); $this->_initialize($other);
} }
/** /**
* Initialise the internals elements * Initialise the internals elements
* *
* @param array|Traversable|null $other The initial values * @param iterable|null $other The initial values
* *
* @psalm-param array<TKey, TValue>|Traversable<TKey, TValue>|null * @psalm-param array<TKey, TValue>|Traversable<TKey, TValue>|null $other
* *
* @throws InvalidArgumentException If the initial values aren't valid * @throws InvalidArgumentException If the initial values aren't valid
* @internal * @internal
*
*/ */
protected function _initialize ($other = null) { protected function _initialize (?iterable $other = null) {
$this->elements = []; $this->elements = [];
if ($other !== null) { if ($other !== null) {
if (!is_array($other) && !$other instanceof Traversable) { if (!is_array($other) && !$other instanceof Traversable) {
@ -73,7 +71,7 @@ class ImmutableCollection implements IImmutableCollection {
* @internal * @internal
* *
*/ */
protected function _set ($key, $value): self { protected function _set (int|string $key, mixed $value): self {
$this->elements[$this->_normalizeKey($key)] = $value; $this->elements[$this->_normalizeKey($key)] = $value;
return $this; return $this;
} }
@ -103,7 +101,7 @@ class ImmutableCollection implements IImmutableCollection {
* @internal * @internal
* *
*/ */
protected function _add (...$values): self { protected function _add (mixed ...$values): self {
foreach ($values as $value) { foreach ($values as $value) {
$this->elements[] = $value; $this->elements[] = $value;
} }
@ -134,53 +132,49 @@ class ImmutableCollection implements IImmutableCollection {
* @return array-key The normalized key * @return array-key The normalized key
* @psalm-return TKey * @psalm-return TKey
*/ */
protected function _normalizeKey ($key) { protected function _normalizeKey (int|string $key): int|string {
return $key; return $key;
} }
/** /**
* @inheritDoc * @inheritDoc
*/ */
public function getIterator () { public function getIterator (): ArrayIterator {
return new ArrayIterator($this->elements); return new ArrayIterator($this->elements);
} }
/** /**
* @inheritDoc * @inheritDoc
*/ */
public function offsetExists ($offset): bool { public function offsetExists (mixed $offset): bool {
return $this->exists($offset); return $this->exists($offset);
} }
/** /**
* @inheritDoc * @inheritDoc
*/ */
public function offsetGet ($offset) { public function offsetGet (mixed $offset): mixed {
return $this->get($offset); return $this->get($offset);
} }
/** /**
* @inheritDoc * @inheritDoc
*/ */
public function offsetSet ($offset, $value): void { public function offsetSet (mixed $offset, mixed $value): void {
throw new ImmutableException(); throw new ImmutableException();
} }
/** /**
* @inheritDoc * @inheritDoc
*/ */
public function offsetUnset ($offset): void { public function offsetUnset (mixed $offset): void {
throw new ImmutableException(); throw new ImmutableException();
} }
/** public function __serialize (): array {
* @inheritDoc return [
*/ 'elements' => $this->elements,
public function serialize (): ?string { ];
return serialize($this->elements);
} }
/** public function __unserialize (array $data): void {
* @inheritDoc $this->_initialize($data['elements']);
*/
public function unserialize ($data) {
$this->_initialize(unserialize($data));
} }
/** /**
@ -206,26 +200,26 @@ class ImmutableCollection implements IImmutableCollection {
/** /**
* @inheritDoc * @inheritDoc
*/ */
public function exists ($key): bool { public function exists (int|string $key): bool {
return isset($this->elements[$this->_normalizeKey($key)]); return isset($this->elements[$this->_normalizeKey($key)]);
} }
/** /**
* @inheritDoc * @inheritDoc
*/ */
public function contains ($value, bool $strict = false): bool { public function contains (mixed $value, bool $strict = false): bool {
return in_array($value, $this->elements, $strict); return in_array($value, $this->elements, $strict);
} }
/** /**
* @inheritDoc * @inheritDoc
*/ */
public function get ($key) { public function get (int|string $key): mixed {
return $this->elements[$this->_normalizeKey($key)] ?? null; return $this->elements[$this->_normalizeKey($key)] ?? null;
} }
/** /**
* @inheritDoc * @inheritDoc
*/ */
public function key ($value, bool $strict = false) { public function key (mixed $value, bool $strict = false): int|string|null {
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)) {
return $currentKey; return $currentKey;
@ -309,11 +303,9 @@ class ImmutableCollection implements IImmutableCollection {
/** /**
* @inheritDoc * @inheritDoc
*
* @throws JsonException
*/ */
public function jsonSerialize () { public function jsonSerialize (): mixed {
return json_encode($this->elements, JSON_THROW_ON_ERROR); return $this->elements;
} }
/** /**

@ -7,13 +7,9 @@ namespace jrosset\Collections;
*/ */
trait TInsensitiveCaseKey { trait TInsensitiveCaseKey {
/** /**
* Normalize a key * @inheritDoc
*
* @param array-key $key The key to normalize
*
* @return array-key The normalized key
*/ */
protected function _normalizeKey ($key) { protected function _normalizeKey (int|string $key): int|string {
return mb_strtolower($key); return mb_strtolower($key);
} }
} }
Loading…
Cancel
Save