diff --git a/README.md b/README.md index 68e0a15..05555d9 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,3 @@ -# PhpArrayClass +# PhpArrayClasses Classes for array embedding \ No newline at end of file diff --git a/composer.json b/composer.json index 71a44b5..7e46304 100644 --- a/composer.json +++ b/composer.json @@ -1,5 +1,5 @@ { - "name": "jrosset/arrayclass", + "name": "jrosset/arrayclasses", "description": "Classes for array embedding", "keywords": [ ], diff --git a/src/ArrayClasses/ArrayClass.php b/src/ArrayClasses/ArrayClass.php new file mode 100644 index 0000000..a32b610 --- /dev/null +++ b/src/ArrayClasses/ArrayClass.php @@ -0,0 +1,61 @@ +array[$offset] = $value; + return $this; + } + /** + * Remove a cell + * + * @param mixed $offset The cell 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)) { + throw new OutOfRangeException(); + } + unset($this->array[$offset]); + + return $this; + } + + /** + * Create an immutable copy + * + * @return ImmutableArrayClass The immutable copy + */ + public function toImmutableArrayClass (): ImmutableArrayClass { + return new ImmutableArrayClass($this); + } + + /** + * @inheritDoc + */ + public function offsetSet ($offset, $value): void { + $this->set($offset, $value); + } + /** + * @inheritDoc + */ + public function offsetUnset ($offset): void { + $this->del($offset); + } +} \ No newline at end of file diff --git a/src/ArrayClasses/IArrayCast.php b/src/ArrayClasses/IArrayCast.php new file mode 100644 index 0000000..6057ed4 --- /dev/null +++ b/src/ArrayClasses/IArrayCast.php @@ -0,0 +1,15 @@ +setThrowsForNonExistentElement($throwsForNonExistentElement); + + if (is_array($initial)) { + $this->array = $initial; + } + elseif (is_null($initial)) { + $this->array = []; + } + elseif ($initial instanceof IArrayCast) { + $this->array = $initial->toArray(); + } + else { + throw new InvalidArgumentException('The initial value is not valid: null, array or ' . IArrayCast::class); + } + } + /** + * Get the value of a cell + * + * @param mixed $offset The cell offset + * + * @return mixed|null The cell value + */ + public function __invoke ($offset) { + return $this->get($offset); + } + + /** + * Check if a cell offset exists + * + * @param mixed $offset The cell offset + * + * @return bool True if the cell offset exists + */ + public function has ($offset): bool { + return array_key_exists($offset, $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 + */ + public function get ($offset, ?bool $throwsForNonExistentElement = null) { + if ($this->has($offset)) { + return $this->array[$offset]; + } + if ($throwsForNonExistentElement ?? $this->throwsForNonExistentElement()) { + throw new OutOfRangeException(); + } + return null; + } + + /** + * @inheritDoc + */ + public function toArray (): array { + return $this->array; + } + /** + * Create a mutable copy + * + * @return ArrayClass The mutable copy + */ + public function toMutableArrayClass (): ArrayClass { + return new ArrayClass($this); + } + + /** + * @inheritDoc + */ + public function count (): int { + return count($this->array); + } + + /** + * @inheritDoc + */ + public function getIterator (): Traversable { + return new ArrayIterator($this->array); + } + + /** + * @inheritDoc + */ + public function offsetExists ($offset): bool { + return $this->has($offset); + } + /** + * @inheritDoc + */ + public function offsetGet ($offset) { + return $this->get($offset); + } + /** + * @inheritDoc + */ + public function offsetSet ($offset, $value): void { + throw new ImmutableException(); + } + /** + * @inheritDoc + */ + public function offsetUnset ($offset): void { + throw new ImmutableException(); + } + + /** + * @inheritDoc + */ + public function serialize (): string { + return serialize($this->array); + } + /** + * @inheritDoc + */ + public function unserialize ($data): void { + $this->array = unserialize($data); + } + + /** + * @inheritDoc + */ + public function jsonSerialize () { + return $this->array; + } + + /** + * Throws an exception for nonexistent elements ? + * + * @return bool Throws an exception for nonexistent elements ? + */ + public function throwsForNonExistentElement (): bool { + return $this->throwsForNonExistentElement; + } + /** + * Set if throws an exception for nonexistent elements + * + * @param bool $throwsForNonExistentElement Throws an exception for nonexistent elements ? + * + * @return $this + */ + public function setThrowsForNonExistentElement (bool $throwsForNonExistentElement): self { + $this->throwsForNonExistentElement = $throwsForNonExistentElement; + return $this; + } +} \ No newline at end of file diff --git a/src/ArrayClasses/ImmutableException.php b/src/ArrayClasses/ImmutableException.php new file mode 100644 index 0000000..8f7dfb9 --- /dev/null +++ b/src/ArrayClasses/ImmutableException.php @@ -0,0 +1,11 @@ +