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; namespace jrosset\ArrayClasses;
/**
* Interface for array classes
*
* @see IImmutableArrayClass Immutable version
*/
interface IArrayClass extends IImmutableArrayClass { interface IArrayClass extends IImmutableArrayClass {
/** /**
* Create or replace a cell * Create or replace a cell
@ -16,10 +21,11 @@ interface IArrayClass extends IImmutableArrayClass {
* Remove a cell * 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 * @return $this
*/ */
public function del ($cellName): self; public function del ($cellName, ?bool $throwsForNonExistentElement = null): self;
/** /**
* Create an immutable copy * Create an immutable copy

@ -8,6 +8,11 @@ use IteratorAggregate;
use JsonSerializable; use JsonSerializable;
use Serializable; use Serializable;
/**
* Interface for immutable array classes
*
* @see IArrayClass Mutable version
*/
interface IImmutableArrayClass extends IteratorAggregate, JsonSerializable, Serializable, Countable, ArrayAccess, IArrayCast { interface IImmutableArrayClass extends IteratorAggregate, JsonSerializable, Serializable, Countable, ArrayAccess, IArrayCast {
/** /**
* Check if a cell exists * Check if a cell exists
@ -21,10 +26,11 @@ interface IImmutableArrayClass extends IteratorAggregate, JsonSerializable, Seri
* Get the value of a cell * 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 * @return mixed The cell value
*/ */
public function get ($cellName); public function get ($cellName, ?bool $throwsForNonExistentElement = null);
/** /**
* Create a mutable copy * 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 * @inheritDoc
*
* @param mixed $offset The cell offset
*
* @return bool True if the cell offset exists
*/ */
public function has ($offset): bool { public function has ($cellName): bool {
return array_key_exists($offset, $this->array); return array_key_exists($cellName, $this->array);
} }
/** /**
* Get the value of a cell * @inheritDoc
*
* @param mixed $offset The cell offset
* @param bool|null $throwsForNonExistentElement If set, temporarily override {@see ImmutableArrayClass::$throwsForNonExistentElement}
*
* @return mixed|null The cell value
*/ */
public function get ($offset, ?bool $throwsForNonExistentElement = null) { public function get ($cellName, ?bool $throwsForNonExistentElement = null) {
if ($this->has($offset)) { if ($this->has($cellName)) {
return $this->array[$offset]; return $this->array[$cellName];
} }
if ($throwsForNonExistentElement ?? $this->throwsForNonExistentElement()) { if ($throwsForNonExistentElement ?? $this->throwsForNonExistentElement()) {
throw new OutOfRangeException(); throw new OutOfRangeException();

@ -6,30 +6,30 @@ use OutOfRangeException;
trait TInternalArray { trait TInternalArray {
/** /**
* Set or replace a cell value * Create or replace a cell value
* *
* @param mixed $offset The cell offset * @param mixed $cellName The cell name/offset
* @param mixed $value The cell new value * @param mixed $cellValue The cell new value
* *
* @return $this * @return $this
*/ */
public function set ($offset, $value): self { public function set ($cellName, $cellValue): self {
$this->array[$offset] = $value; $this->array[$cellName] = $cellValue;
return $this; return $this;
} }
/** /**
* Remove a cell * 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} * @param bool|null $throwsForNonExistentElement If set, temporarily override {@see ImmutableArrayClass::$throwsForNonExistentElement}
* *
* @return $this * @return $this
*/ */
public function del ($offset, ?bool $throwsForNonExistentElement = null): self { public function del ($cellName, ?bool $throwsForNonExistentElement = null): self {
if ($throwsForNonExistentElement ?? $this->throwsForNonExistentElement() && !$this->has($offset)) { if ($throwsForNonExistentElement ?? $this->throwsForNonExistentElement() && !$this->has($cellName)) {
throw new OutOfRangeException(); throw new OutOfRangeException();
} }
unset($this->array[$offset]); unset($this->array[$cellName]);
return $this; return $this;
} }

Loading…
Cancel
Save