diff --git a/CHANGELOG.md b/CHANGELOG.md index 8ac7ff2..876294a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +3.2.3 +===== + +* (improvement) Add `ArrayAccess` support to `ArgumentBag`. + + 3.2.2 ===== diff --git a/src/Exception/Structure/ImmutableDataException.php b/src/Exception/Structure/ImmutableDataException.php new file mode 100644 index 0000000..df236dd --- /dev/null +++ b/src/Exception/Structure/ImmutableDataException.php @@ -0,0 +1,12 @@ + + * @implements \ArrayAccess */ -final readonly class ArgumentBag implements \IteratorAggregate, \Countable +final readonly class ArgumentBag implements \IteratorAggregate, \Countable, \ArrayAccess { /** * @param array $arguments @@ -296,6 +298,7 @@ public function has (string $key) : bool return \array_key_exists($key, $this->arguments); } + // region IteratorAggregate implementation /** * */ @@ -303,7 +306,9 @@ public function getIterator () : \Traversable { return new \ArrayIterator($this->arguments); } + // endregion + // region Countable implementation /** * */ @@ -311,4 +316,39 @@ public function count () : int { return \count($this->arguments); } + // endregion + + // region ArrayAccess implementation + /** + * + */ + public function offsetExists (mixed $offset) : bool + { + return isset($this->arguments[$offset]); + } + + /** + * + */ + public function offsetGet (mixed $offset) : mixed + { + return $this->arguments[$offset] ?? null; + } + + /** + * + */ + public function offsetSet (mixed $offset, mixed $value) : void + { + throw new ImmutableDataException("The argument bag is read-only"); + } + + /** + * + */ + public function offsetUnset (mixed $offset) : void + { + throw new ImmutableDataException("The argument bag is read-only"); + } + // endregion } diff --git a/tests/Structure/ArgumentBagTest.php b/tests/Structure/ArgumentBagTest.php index cb2326e..5ee42c8 100644 --- a/tests/Structure/ArgumentBagTest.php +++ b/tests/Structure/ArgumentBagTest.php @@ -6,6 +6,7 @@ use Tests\Torr\Rad\Fixtures\ExampleBackedEnum; use Tests\Torr\Rad\Fixtures\ExampleEntity; use Tests\Torr\Rad\Fixtures\ExampleEnum; +use Torr\Rad\Exception\Structure\ImmutableDataException; use Torr\Rad\Exception\Structure\InvalidArgumentTypeException; use Torr\Rad\Exception\Structure\MissingArgumentException; use Torr\Rad\Structure\ArgumentBag; @@ -105,6 +106,39 @@ public function testInvalidGetters (callable $getter, string $key) : void $getter($key); } + /** + * + */ + public function testArrayAccess () : void + { + $bag = self::createBag(); + + self::assertSame($bag["object"], $bag->get("object")); + self::assertSame(ExampleEnum::Test, $bag["enum"]); + } + + /** + * + */ + public function testInvalidArraySet () : void + { + $this->expectException(ImmutableDataException::class); + $bag = self::createBag(); + + $bag["nope"] = "nope"; + } + + /** + * + */ + public function testInvalidArrayUnset () : void + { + $this->expectException(ImmutableDataException::class); + $bag = self::createBag(); + + unset($bag["nope"]); + } + /** * */