diff --git a/src/ArrayClasses/IArrayClass.php b/src/ArrayClasses/IArrayClass.php index 4971443..2e698e6 100644 --- a/src/ArrayClasses/IArrayClass.php +++ b/src/ArrayClasses/IArrayClass.php @@ -2,6 +2,11 @@ namespace jrosset\ArrayClasses; +/** + * Interface for array classes + * + * @see IImmutableArrayClass Immutable version + */ interface IArrayClass extends IImmutableArrayClass { /** * Create or replace a cell @@ -15,11 +20,12 @@ interface IArrayClass extends IImmutableArrayClass { /** * Remove a cell * - * @param mixed $cellName The cell name/offset + * @param mixed $cellName The cell name/offset + * @param bool $throwsForNonExistentElement Throws an exception for nonexistent elements ? * * @return $this */ - public function del ($cellName): self; + public function del ($cellName, ?bool $throwsForNonExistentElement = null): self; /** * Create an immutable copy diff --git a/src/ArrayClasses/IImmutableArrayClass.php b/src/ArrayClasses/IImmutableArrayClass.php index 3c7fb1e..9908cef 100644 --- a/src/ArrayClasses/IImmutableArrayClass.php +++ b/src/ArrayClasses/IImmutableArrayClass.php @@ -8,6 +8,11 @@ use IteratorAggregate; use JsonSerializable; use Serializable; +/** + * Interface for immutable array classes + * + * @see IArrayClass Mutable version + */ interface IImmutableArrayClass extends IteratorAggregate, JsonSerializable, Serializable, Countable, ArrayAccess, IArrayCast { /** * Check if a cell exists @@ -20,11 +25,12 @@ interface IImmutableArrayClass extends IteratorAggregate, JsonSerializable, Seri /** * Get the value of a cell * - * @param mixed $cellName The cell name/offset + * @param mixed $cellName The cell name/offset + * @param bool $throwsForNonExistentElement Throws an exception for nonexistent elements ? * * @return mixed The cell value */ - public function get ($cellName); + public function get ($cellName, ?bool $throwsForNonExistentElement = null); /** * Create a mutable copy diff --git a/src/ArrayClasses/ImmutableInsensitiveCaseArrayClass.php b/src/ArrayClasses/ImmutableInsensitiveCaseArrayClass.php new file mode 100644 index 0000000..bf523d4 --- /dev/null +++ b/src/ArrayClasses/ImmutableInsensitiveCaseArrayClass.php @@ -0,0 +1,33 @@ +has__TImmutableInternalArray(mb_strtolower($cellName)); + } + /** + * @inheritDoc + */ + public function get ($cellName, ?bool $throwsForNonExistentElement = null) { + return $this->get__TImmutableInternalArray(mb_strtolower($cellName), $throwsForNonExistentElement); + } + + /** + * @inheritDoc + */ + public function toMutable (): IArrayClass { + return new InsensitiveCaseArrayClass($this->array, $this->throwsForNonExistentElement()); + } +} \ No newline at end of file diff --git a/src/ArrayClasses/InsensitiveCaseArrayClass.php b/src/ArrayClasses/InsensitiveCaseArrayClass.php new file mode 100644 index 0000000..2978ad5 --- /dev/null +++ b/src/ArrayClasses/InsensitiveCaseArrayClass.php @@ -0,0 +1,33 @@ +set__TInternalArray(mb_strtolower($cellName), $cellValue); + } + /** + * @inheritDoc + */ + public function del ($cellName, ?bool $throwsForNonExistentElement = null): self { + return $this->del__TInternalArray(mb_strtolower($cellName), $throwsForNonExistentElement); + } + + /** + * @inheritDoc + */ + public function toImmutable (): IImmutableArrayClass { + return new ImmutableInsensitiveCaseArrayClass($this->array, $this->throwsForNonExistentElement()); + } +} \ No newline at end of file diff --git a/src/ArrayClasses/TImmutableInternalArray.php b/src/ArrayClasses/TImmutableInternalArray.php index 0ec0e8d..d8fbce8 100644 --- a/src/ArrayClasses/TImmutableInternalArray.php +++ b/src/ArrayClasses/TImmutableInternalArray.php @@ -53,26 +53,17 @@ trait TImmutableInternalArray { } /** - * Check if a cell offset exists - * - * @param mixed $offset The cell offset - * - * @return bool True if the cell offset exists + * @inheritDoc */ - public function has ($offset): bool { - return array_key_exists($offset, $this->array); + public function has ($cellName): bool { + return array_key_exists($cellName, $this->array); } /** - * Get the value of a cell - * - * @param mixed $offset The cell offset - * @param bool|null $throwsForNonExistentElement If set, temporarily override {@see ImmutableArrayClass::$throwsForNonExistentElement} - * - * @return mixed|null The cell value + * @inheritDoc */ - public function get ($offset, ?bool $throwsForNonExistentElement = null) { - if ($this->has($offset)) { - return $this->array[$offset]; + public function get ($cellName, ?bool $throwsForNonExistentElement = null) { + if ($this->has($cellName)) { + return $this->array[$cellName]; } if ($throwsForNonExistentElement ?? $this->throwsForNonExistentElement()) { throw new OutOfRangeException(); diff --git a/src/ArrayClasses/TInternalArray.php b/src/ArrayClasses/TInternalArray.php index e9ba1d0..a3a4036 100644 --- a/src/ArrayClasses/TInternalArray.php +++ b/src/ArrayClasses/TInternalArray.php @@ -6,30 +6,30 @@ use OutOfRangeException; trait TInternalArray { /** - * Set or replace a cell value + * Create or replace a cell value * - * @param mixed $offset The cell offset - * @param mixed $value The cell new value + * @param mixed $cellName The cell name/offset + * @param mixed $cellValue The cell new value * * @return $this */ - public function set ($offset, $value): self { - $this->array[$offset] = $value; + public function set ($cellName, $cellValue): self { + $this->array[$cellName] = $cellValue; return $this; } /** * Remove a cell * - * @param mixed $offset The cell offset + * @param mixed $cellName The cell name/offset * @param bool|null $throwsForNonExistentElement If set, temporarily override {@see ImmutableArrayClass::$throwsForNonExistentElement} * * @return $this */ - public function del ($offset, ?bool $throwsForNonExistentElement = null): self { - if ($throwsForNonExistentElement ?? $this->throwsForNonExistentElement() && !$this->has($offset)) { + public function del ($cellName, ?bool $throwsForNonExistentElement = null): self { + if ($throwsForNonExistentElement ?? $this->throwsForNonExistentElement() && !$this->has($cellName)) { throw new OutOfRangeException(); } - unset($this->array[$offset]); + unset($this->array[$cellName]); return $this; }