Add insensitive case versions

2.x 1.1.0
Julien Rosset 3 years ago
parent 096d4bc3ed
commit 044e50007c

@ -2,6 +2,11 @@
namespace jrosset\ArrayClasses;
/**
* Interface for array classes
*
* @see IImmutableArrayClass Immutable version
*/
interface IArrayClass extends IImmutableArrayClass {
/**
* Create or replace a cell
@ -16,10 +21,11 @@ interface IArrayClass extends IImmutableArrayClass {
* Remove a cell
*
* @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

@ -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
@ -21,10 +26,11 @@ interface IImmutableArrayClass extends IteratorAggregate, JsonSerializable, Seri
* Get the value of a cell
*
* @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

@ -0,0 +1,33 @@
<?php
namespace jrosset\ArrayClasses;
/**
* An immutable array with insensitive case cell name
*/
class ImmutableInsensitiveCaseArrayClass implements IImmutableArrayClass {
use TImmutableInternalArray {
has as private has__TImmutableInternalArray;
get as private get__TImmutableInternalArray;
}
/**
* @inheritDoc
*/
public function has ($cellName): bool {
return $this->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());
}
}

@ -0,0 +1,33 @@
<?php
namespace jrosset\ArrayClasses;
/**
* An array with insensitive case cell name
*/
class InsensitiveCaseArrayClass extends ImmutableInsensitiveCaseArrayClass implements IArrayClass {
use TInternalArray {
set as private set__TInternalArray;
del as private del__TInternalArray;
}
/**
* @inheritDoc
*/
public function set ($cellName, $cellValue): self {
return $this->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());
}
}

@ -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();

@ -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;
}

Loading…
Cancel
Save