Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions src/SessionStore.php
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
<?php
namespace Gt\Session;

use ArrayIterator;
use Countable;
use Gt\TypeSafeGetter\NullableTypeSafeGetter;
use Gt\TypeSafeGetter\TypeSafeGetter;

/** @SuppressWarnings(PHPMD.TooManyPublicMethods) */
class SessionStore implements SessionContainer, TypeSafeGetter, Countable {
/**
* @extends ArrayIterator<string, mixed>
* @SuppressWarnings(PHPMD.TooManyPublicMethods)
*/
class SessionStore
extends ArrayIterator
implements SessionContainer, TypeSafeGetter, Countable {
use NullableTypeSafeGetter;

protected string $name;
protected Session $session;
/** @var array<SessionStore> */
protected array $stores;
/** @var array<string, mixed> */
protected array $data;
protected ?SessionStore $parentStore;

public function __construct(
Expand All @@ -26,31 +30,27 @@ 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 {
return isset($this->stores[$key]);
}

public function removeData(string $key):void {
unset($this->data[$key]);
$this->offsetUnset($key);
}

public function removeStore(string $key):void {
Expand Down
21 changes: 21 additions & 0 deletions test/phpunit/SessionStoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}