Skip to content

Commit 723a911

Browse files
yaniYanicompwright
authored
feat: make Session iterable (#11)
* implement Iterator in Session class * make Session Iterator throw if not initialized Co-authored-by: Jonathon Hill <[email protected]> * phpstan: ignore iterator parameter errors in Session class * update iterator tests Co-authored-by: Yani <[email protected]> Co-authored-by: Jonathon Hill <[email protected]>
1 parent 4c485bd commit 723a911

File tree

3 files changed

+88
-1
lines changed

3 files changed

+88
-1
lines changed

features/access.feature

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,9 @@ Feature: Session access
2727
Scenario: Write array access data
2828
When data does not exist
2929
Then array access write succeeds
30+
Scenario: Iterate over populated session
31+
When data exists
32+
Then data is iterated
33+
Scenario: Iterate over non-populated session
34+
When data does not exist
35+
Then data is not iterated

src/Session.php

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44

55
namespace Compwright\PhpSession;
66

7+
use Iterator;
78
use Countable;
89
use ArrayAccess;
910
use RuntimeException;
1011

11-
class Session implements ArrayAccess, Countable
12+
class Session implements ArrayAccess, Iterator, Countable
1213
{
1314
protected string $name;
1415

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

227+
public function rewind(): void
228+
{
229+
if (!$this->isInitialized()) {
230+
throw new RuntimeException('Session not initialized');
231+
}
232+
233+
// @phpstan-ignore-next-line
234+
reset($this->contents);
235+
}
236+
237+
public function current(): mixed
238+
{
239+
if (!$this->isInitialized()) {
240+
throw new RuntimeException('Session not initialized');
241+
}
242+
243+
// @phpstan-ignore-next-line
244+
return current($this->contents);
245+
}
246+
247+
public function key(): mixed
248+
{
249+
if (!$this->isInitialized()) {
250+
throw new RuntimeException('Session not initialized');
251+
}
252+
253+
// @phpstan-ignore-next-line
254+
return key($this->contents);
255+
}
256+
257+
public function next(): void
258+
{
259+
if (!$this->isInitialized()) {
260+
throw new RuntimeException('Session not initialized');
261+
}
262+
263+
// @phpstan-ignore-next-line
264+
next($this->contents);
265+
}
266+
267+
public function valid(): bool
268+
{
269+
if (!$this->isInitialized()) {
270+
throw new RuntimeException('Session not initialized');
271+
}
272+
273+
// @phpstan-ignore-next-line
274+
return key($this->contents) !== null;
275+
}
276+
226277
public function count(): int
227278
{
228279
return count($this->contents ?? []);

tests/behavior/AccessContext.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,4 +169,34 @@ public function arrayAccessWriteSucceeds(): void
169169
Assert::assertCount(1, $this->session);
170170
Assert::assertTrue(isset($this->session['bar']));
171171
}
172+
173+
/**
174+
* @Then data is iterated
175+
*/
176+
public function iteratorSucceeds(): void
177+
{
178+
$counter = 0;
179+
180+
foreach ($this->session as $var => $val) {
181+
Assert::assertSame('foo', $var);
182+
Assert::assertSame('bar', $val);
183+
$counter++;
184+
}
185+
186+
Assert::assertSame(1, $counter);
187+
}
188+
189+
/**
190+
* @Then data is not iterated
191+
*/
192+
public function iteratorFails(): void
193+
{
194+
$counter = 0;
195+
196+
foreach ($this->session as $var => $val) {
197+
$counter++;
198+
}
199+
200+
Assert::assertSame(0, $counter);
201+
}
172202
}

0 commit comments

Comments
 (0)