Skip to content

Commit

Permalink
fix: Implement count in Container
Browse files Browse the repository at this point in the history
Implement count method in Container to retrieve set values quantity

Close laminas#17

Signed-off-by: Marcos Felipe <[email protected]>
  • Loading branch information
omarkdev committed Feb 15, 2022
1 parent 928aea6 commit 0faedb9
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Laminas\Session;

use function count;

/**
* Session storage container
*
Expand Down Expand Up @@ -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]);
}
}
15 changes: 15 additions & 0 deletions test/ContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit 0faedb9

Please sign in to comment.