diff --git a/src/Container.php b/src/Container.php index e3c0c990..98f3e91e 100644 --- a/src/Container.php +++ b/src/Container.php @@ -2,6 +2,8 @@ namespace Laminas\Session; +use function count; + /** * Session storage container * @@ -30,4 +32,17 @@ public function &offsetGet($key) return $ret; } + + /** + * Get the number of set values in container. + * + * @return int + */ + public function count() + { + $storage = $this->getStorage(); + $name = $this->getName(); + + return count($storage[$name]); + } } diff --git a/test/ContainerTest.php b/test/ContainerTest.php index 56b11b7e..df0dc6a1 100644 --- a/test/ContainerTest.php +++ b/test/ContainerTest.php @@ -560,4 +560,19 @@ public function testGetArrayCopyAfterExchangeArray(): void self::assertIsArray($contents); self::assertArrayHasKey('foo', $contents, "'getArrayCopy' doesn't return exchanged array"); } + + /** + * Test count method is retrieving + * set values quantity. + * + * @test + */ + public function testCountIsRetrievingSetValuesQuantity(): void + { + $this->container->foo = ['bar' => 'baz']; + $this->container->bar = ['foo' => 'baz']; + $this->container->baz = ['bar' => 'foo']; + + self::assertCount(3, $this->container); + } }