Skip to content

Commit

Permalink
Add array access support to ArgumentBag
Browse files Browse the repository at this point in the history
  • Loading branch information
apfelbox committed Sep 23, 2024
1 parent dc0b315 commit 636278f
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 1 deletion.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
3.2.3
=====

* (improvement) Add `ArrayAccess` support to `ArgumentBag`.


3.2.2
=====

Expand Down
12 changes: 12 additions & 0 deletions src/Exception/Structure/ImmutableDataException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php declare(strict_types=1);

namespace Torr\Rad\Exception\Structure;

use Torr\Rad\Exception\RadException;

/**
* @final
*/
class ImmutableDataException extends \BadMethodCallException implements RadException
{
}
45 changes: 44 additions & 1 deletion src/Structure/ArgumentBag.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Torr\Rad\Structure;

use Symfony\Component\HttpFoundation\ParameterBag;
use Torr\Rad\Exception\Structure\ImmutableDataException;
use Torr\Rad\Exception\Structure\InvalidArgumentTypeException;
use Torr\Rad\Exception\Structure\MissingArgumentException;

Expand All @@ -11,7 +12,7 @@
*
* @implements \IteratorAggregate<string, array|bool|string|int|float|object>
*/
final readonly class ArgumentBag implements \IteratorAggregate, \Countable
final readonly class ArgumentBag implements \IteratorAggregate, \Countable, \ArrayAccess
{
/**
* @param array<string, array|bool|string|int|float|object> $arguments
Expand Down Expand Up @@ -296,19 +297,61 @@ public function has (string $key) : bool
return \array_key_exists($key, $this->arguments);
}

//region IteratorAggregate implementation
/**
*
*/
public function getIterator () : \Traversable
{
return new \ArrayIterator($this->arguments);
}
//endregion


//region Countable implementation
/**
*
*/
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


}
35 changes: 35 additions & 0 deletions tests/Structure/ArgumentBagTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -105,6 +106,40 @@ 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($bag["enum"], ExampleEnum::Test);
}

/**
*
*/
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"]);
}


/**
*
*/
Expand Down

0 comments on commit 636278f

Please sign in to comment.