Skip to content

feat: make Session iterable #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Dec 28, 2022
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
6 changes: 6 additions & 0 deletions features/access.feature
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,9 @@ Feature: Session access
Scenario: Write array access data
When data does not exist
Then array access write succeeds
Scenario: Iterate over populated session
When data exists
Then data is iterated
Scenario: Iterate over non-populated session
When data does not exist
Then data is not iterated
53 changes: 52 additions & 1 deletion src/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@

namespace Compwright\PhpSession;

use Iterator;
use Countable;
use ArrayAccess;
use RuntimeException;

class Session implements ArrayAccess, Countable
class Session implements ArrayAccess, Iterator, Countable
{
protected string $name;

Expand Down Expand Up @@ -223,6 +224,56 @@ public function toArray(): array
return $this->contents ?? [];
}

public function rewind(): void
{
if (!$this->isInitialized()) {
throw new RuntimeException('Session not initialized');
}

// @phpstan-ignore-next-line
reset($this->contents);
}

public function current(): mixed
{
if (!$this->isInitialized()) {
throw new RuntimeException('Session not initialized');
}

// @phpstan-ignore-next-line
return current($this->contents);
}

public function key(): mixed
{
if (!$this->isInitialized()) {
throw new RuntimeException('Session not initialized');
}

// @phpstan-ignore-next-line
return key($this->contents);
}

public function next(): void
{
if (!$this->isInitialized()) {
throw new RuntimeException('Session not initialized');
}

// @phpstan-ignore-next-line
next($this->contents);
}

public function valid(): bool
{
if (!$this->isInitialized()) {
throw new RuntimeException('Session not initialized');
}

// @phpstan-ignore-next-line
return key($this->contents) !== null;
}

public function count(): int
{
return count($this->contents ?? []);
Expand Down
30 changes: 30 additions & 0 deletions tests/behavior/AccessContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,4 +169,34 @@ public function arrayAccessWriteSucceeds(): void
Assert::assertCount(1, $this->session);
Assert::assertTrue(isset($this->session['bar']));
}

/**
* @Then data is iterated
*/
public function iteratorSucceeds(): void
{
$counter = 0;

foreach ($this->session as $var => $val) {
Assert::assertSame('foo', $var);
Assert::assertSame('bar', $val);
$counter++;
}

Assert::assertSame(1, $counter);
}

/**
* @Then data is not iterated
*/
public function iteratorFails(): void
{
$counter = 0;

foreach ($this->session as $var => $val) {
$counter++;
}

Assert::assertSame(0, $counter);
}
}