diff --git a/composer.json b/composer.json
index 2cd9539..c19ff16 100644
--- a/composer.json
+++ b/composer.json
@@ -5,7 +5,7 @@
"minimum-stability": "stable",
"require": {
- "php": "^7.4 || ^8.0"
+ "php": "^8.1"
},
"autoload": {
"psr-4": {
diff --git a/src/Collections/Collection.php b/src/Collections/Collection.php
index 509a167..ea98db6 100644
--- a/src/Collections/Collection.php
+++ b/src/Collections/Collection.php
@@ -14,7 +14,7 @@ class Collection extends ImmutableCollection implements ICollection {
/**
* @inheritDoc
*/
- public function set ($key, $value): self {
+ public function set (int|string $key, mixed $value): self {
return $this->_set($key, $value);
}
/**
@@ -27,7 +27,7 @@ class Collection extends ImmutableCollection implements ICollection {
/**
* @inheritDoc
*/
- public function add (...$values): self {
+ public function add (mixed ...$values): self {
return $this->_add(...$values);
}
/**
@@ -40,7 +40,7 @@ class Collection extends ImmutableCollection implements ICollection {
/**
* @inheritDoc
*/
- public function prepend (...$values): ICollection {
+ public function prepend (mixed ...$values): ICollection {
array_unshift($this->elements, ...$values);
return $this;
}
@@ -69,14 +69,14 @@ class Collection extends ImmutableCollection implements ICollection {
/**
* @inheritDoc
*/
- public function remove ($key): self {
+ public function remove (int|string $key): self {
unset($this->elements[$this->_normalizeKey($key)]);
return $this;
}
/**
* @inheritDoc
*/
- public function removeValue ($value, bool $strict = false): self {
+ public function removeValue (mixed $value, bool $strict = false): self {
foreach ($this->elements as $currentKey => $currentValue) {
if (($strict && $value === $currentValue) || (!$strict && $value == $currentValue)) {
unset($this->elements[$currentKey]);
@@ -88,13 +88,13 @@ class Collection extends ImmutableCollection implements ICollection {
/**
* @inheritDoc
*/
- public function offsetSet ($offset, $value): void {
+ public function offsetSet (mixed $offset, mixed $value): void {
$this->set($offset, $value);
}
/**
* @inheritDoc
*/
- public function offsetUnset ($offset): void {
+ public function offsetUnset (mixed $offset): void {
$this->remove($offset);
}
}
\ No newline at end of file
diff --git a/src/Collections/ICollection.php b/src/Collections/ICollection.php
index 857e62c..cabb3b3 100644
--- a/src/Collections/ICollection.php
+++ b/src/Collections/ICollection.php
@@ -21,7 +21,7 @@ interface ICollection extends IImmutableCollection {
*
* @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
*
@@ -40,7 +40,7 @@ interface ICollection extends IImmutableCollection {
*
* @return $this
*/
- public function add (...$values): self;
+ public function add (mixed ...$values): self;
/**
* Add the values of one or multiple collections
*
@@ -59,7 +59,7 @@ interface ICollection extends IImmutableCollection {
*
* @return $this
*/
- public function prepend (...$values): self;
+ public function prepend (mixed ...$values): self;
/**
* Add the values of one or multiple collections at the beginning of the current collection
*
@@ -84,7 +84,7 @@ interface ICollection extends IImmutableCollection {
*
* @return $this
*/
- public function remove ($key): self;
+ public function remove (int|string $key): self;
/**
* Delete all instances of a value
*
@@ -95,5 +95,5 @@ interface ICollection extends IImmutableCollection {
*
* @return $this
*/
- public function removeValue ($value, bool $strict = false): self;
+ public function removeValue (mixed $value, bool $strict = false): self;
}
\ No newline at end of file
diff --git a/src/Collections/IImmutableCollection.php b/src/Collections/IImmutableCollection.php
index 45a74ea..6bc3bc6 100644
--- a/src/Collections/IImmutableCollection.php
+++ b/src/Collections/IImmutableCollection.php
@@ -7,7 +7,6 @@ use Closure;
use Countable;
use IteratorAggregate;
use JsonSerializable;
-use Serializable;
/**
* Interface for an immutable (read-only) collection
@@ -18,7 +17,7 @@ use Serializable;
* @template-extends ArrayAccess
* @template-extends IArrayCast
*/
-interface IImmutableCollection extends IteratorAggregate, JsonSerializable, Serializable, Countable, ArrayAccess, IArrayCast {
+interface IImmutableCollection extends IteratorAggregate, JsonSerializable, Countable, ArrayAccess, IArrayCast {
/**
* 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
*/
- public function exists ($key): bool;
+ public function exists (int|string $key): bool;
/**
* Checks if contains a value
*
@@ -46,7 +45,7 @@ interface IImmutableCollection extends IteratorAggregate, JsonSerializable, Seri
*
* @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
@@ -58,7 +57,7 @@ interface IImmutableCollection extends IteratorAggregate, JsonSerializable, Seri
* @return mixed The value
* @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
*
@@ -67,10 +66,10 @@ interface IImmutableCollection extends IteratorAggregate, JsonSerializable, Seri
*
* @psalm-param TValue $value
*
- * @return array-key
+ * @return array-key|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
diff --git a/src/Collections/ImmutableCollection.php b/src/Collections/ImmutableCollection.php
index e358d87..d1e8874 100644
--- a/src/Collections/ImmutableCollection.php
+++ b/src/Collections/ImmutableCollection.php
@@ -5,7 +5,6 @@ namespace jrosset\Collections;
use ArrayIterator;
use Closure;
use InvalidArgumentException;
-use JsonException;
use Traversable;
/**
@@ -27,28 +26,27 @@ class ImmutableCollection implements IImmutableCollection {
/**
* Initialise a new collection
*
- * @param array|Traversable|null $other The initial values
+ * @param iterable|null $other The initial values
*
- * @psalm-param array|Traversable|null
+ * @psalm-param array|Traversable|null $other
*
* @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
+ * @param iterable|null $other The initial values
*
- * @psalm-param array|Traversable|null
+ * @psalm-param array|Traversable|null $other
*
* @throws InvalidArgumentException If the initial values aren't valid
* @internal
- *
*/
- protected function _initialize ($other = null) {
+ protected function _initialize (?iterable $other = null) {
$this->elements = [];
if ($other !== null) {
if (!is_array($other) && !$other instanceof Traversable) {
@@ -73,7 +71,7 @@ class ImmutableCollection implements IImmutableCollection {
* @internal
*
*/
- protected function _set ($key, $value): self {
+ protected function _set (int|string $key, mixed $value): self {
$this->elements[$this->_normalizeKey($key)] = $value;
return $this;
}
@@ -103,7 +101,7 @@ class ImmutableCollection implements IImmutableCollection {
* @internal
*
*/
- protected function _add (...$values): self {
+ protected function _add (mixed ...$values): self {
foreach ($values as $value) {
$this->elements[] = $value;
}
@@ -134,53 +132,49 @@ class ImmutableCollection implements IImmutableCollection {
* @return array-key The normalized key
* @psalm-return TKey
*/
- protected function _normalizeKey ($key) {
+ protected function _normalizeKey (int|string $key): int|string {
return $key;
}
/**
* @inheritDoc
*/
- public function getIterator () {
+ public function getIterator (): ArrayIterator {
return new ArrayIterator($this->elements);
}
/**
* @inheritDoc
*/
- public function offsetExists ($offset): bool {
+ public function offsetExists (mixed $offset): bool {
return $this->exists($offset);
}
/**
* @inheritDoc
*/
- public function offsetGet ($offset) {
+ public function offsetGet (mixed $offset): mixed {
return $this->get($offset);
}
/**
* @inheritDoc
*/
- public function offsetSet ($offset, $value): void {
+ public function offsetSet (mixed $offset, mixed $value): void {
throw new ImmutableException();
}
/**
* @inheritDoc
*/
- public function offsetUnset ($offset): void {
+ public function offsetUnset (mixed $offset): void {
throw new ImmutableException();
}
- /**
- * @inheritDoc
- */
- public function serialize (): ?string {
- return serialize($this->elements);
+ public function __serialize (): array {
+ return [
+ 'elements' => $this->elements,
+ ];
}
- /**
- * @inheritDoc
- */
- public function unserialize ($data) {
- $this->_initialize(unserialize($data));
+ public function __unserialize (array $data): void {
+ $this->_initialize($data['elements']);
}
/**
@@ -206,26 +200,26 @@ class ImmutableCollection implements IImmutableCollection {
/**
* @inheritDoc
*/
- public function exists ($key): bool {
+ public function exists (int|string $key): bool {
return isset($this->elements[$this->_normalizeKey($key)]);
}
/**
* @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);
}
/**
* @inheritDoc
*/
- public function get ($key) {
+ public function get (int|string $key): mixed {
return $this->elements[$this->_normalizeKey($key)] ?? null;
}
/**
* @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) {
if (($strict && $value === $currentValue) || (!$strict && $value == $currentValue)) {
return $currentKey;
@@ -309,11 +303,9 @@ class ImmutableCollection implements IImmutableCollection {
/**
* @inheritDoc
- *
- * @throws JsonException
*/
- public function jsonSerialize () {
- return json_encode($this->elements, JSON_THROW_ON_ERROR);
+ public function jsonSerialize (): mixed {
+ return $this->elements;
}
/**
diff --git a/src/Collections/TInsensitiveCaseKey.php b/src/Collections/TInsensitiveCaseKey.php
index d2d4757..9e1f794 100644
--- a/src/Collections/TInsensitiveCaseKey.php
+++ b/src/Collections/TInsensitiveCaseKey.php
@@ -7,13 +7,9 @@ namespace jrosset\Collections;
*/
trait TInsensitiveCaseKey {
/**
- * Normalize a key
- *
- * @param array-key $key The key to normalize
- *
- * @return array-key The normalized key
+ * @inheritDoc
*/
- protected function _normalizeKey ($key) {
+ protected function _normalizeKey (int|string $key): int|string {
return mb_strtolower($key);
}
}
\ No newline at end of file