diff --git a/src/SessionStore.php b/src/SessionStore.php index e1a32f3..07b9788 100644 --- a/src/SessionStore.php +++ b/src/SessionStore.php @@ -1,20 +1,24 @@ + * @SuppressWarnings(PHPMD.TooManyPublicMethods) + */ +class SessionStore + extends ArrayIterator + implements SessionContainer, TypeSafeGetter, Countable { use NullableTypeSafeGetter; protected string $name; protected Session $session; /** @var array */ protected array $stores; - /** @var array */ - protected array $data; protected ?SessionStore $parentStore; public function __construct( @@ -26,23 +30,19 @@ public function __construct( $this->session = $session; $this->parentStore = $parentStore; $this->stores = []; - $this->data = []; - } - - public function count():int { - return count($this->data); + parent::__construct(); } public function setData(string $key, mixed $value):void { - $this->data[$key] = $value; + $this->offsetSet($key, $value); } public function getData(string $key):mixed { - return $this->data[$key] ?? null; + return $this->offsetGet($key) ?? null; } public function containsData(string $key):bool { - return isset($this->data[$key]); + return $this->offsetExists($key); } public function containsStore(string $key):bool { @@ -50,7 +50,7 @@ public function containsStore(string $key):bool { } public function removeData(string $key):void { - unset($this->data[$key]); + $this->offsetUnset($key); } public function removeStore(string $key):void { diff --git a/test/phpunit/SessionStoreTest.php b/test/phpunit/SessionStoreTest.php index 27417b0..2124cb3 100644 --- a/test/phpunit/SessionStoreTest.php +++ b/test/phpunit/SessionStoreTest.php @@ -151,4 +151,25 @@ public function testCount():void { self::assertCount(3, $sut); } + + + public function testSessionStoreIsIterable():void { + $session = $this->createMock(Session::class); + $sut = new SessionStore("test", $session); + + $rawData = [ + "key1" => "value1", + "key2" => "value2", + "key3" => "value3", + ]; + + foreach($rawData as $key => $value) { + $sut->set($key, $value); + } + + foreach($sut as $key => $value) { + self::assertArrayHasKey($key, $rawData); + self::assertSame($rawData[$key], $value); + } + } }