Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 7d13b9d

Browse files
yaniYani
and
Yani
authored
fix: add ArrayAccess to Session class (#7)
Resolves #4 Co-authored-by: Yani <yani@xenokore.com>
1 parent 80fea20 commit 7d13b9d

File tree

3 files changed

+122
-2
lines changed

3 files changed

+122
-2
lines changed

‎features/access.feature

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,28 @@ Feature: Session access
22
Scenario: Check data does not exist
33
When data does not exist
44
Then property check returns false
5+
And array access check returns false
56
Scenario: Check data exist
67
When data exists
78
Then property check returns true
9+
And array access check returns true
810
Scenario: Read data that exists
911
When data exists
1012
Then property read returns data
13+
And array access read returns data
1114
Scenario: Read data that does not exist
1215
When data does not exist
1316
Then property read triggers error
1417
And property read returns null
18+
And array access read triggers error
19+
And array access read returns null
1520
Scenario: Null coalesce when data does not exist
1621
When data does not exist
1722
Then property read with null coalesce returns null
18-
Scenario: Write data
23+
And array access read with null coalesce returns null
24+
Scenario: Write property data
1925
When data does not exist
2026
Then property write succeeds
27+
Scenario: Write array access data
28+
When data does not exist
29+
Then array access write succeeds

‎src/Session.php

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
namespace Compwright\PhpSession;
66

77
use Countable;
8+
use ArrayAccess;
89
use RuntimeException;
910

10-
class Session implements Countable
11+
class Session implements ArrayAccess, Countable
1112
{
1213
protected string $name;
1314

@@ -94,6 +95,48 @@ public function __unset(string $name): void
9495
unset($this->contents[$name]);
9596
}
9697

98+
public function offsetSet($name, $value): void
99+
{
100+
if (!$this->isInitialized()) {
101+
throw new RuntimeException('Session not initialized');
102+
}
103+
104+
if (!$this->writeable) {
105+
throw new RuntimeException('Cannot alter session after it is closed');
106+
}
107+
108+
$this->modified = true;
109+
$this->contents[$name] = $value;
110+
}
111+
112+
public function offsetExists($name): bool
113+
{
114+
if (!$this->isInitialized()) {
115+
throw new RuntimeException('Session not initialized');
116+
}
117+
118+
return isset($this->contents[$name]);
119+
}
120+
121+
public function offsetUnset($name): void
122+
{
123+
if (!$this->isInitialized()) {
124+
throw new RuntimeException('Session not initialized');
125+
}
126+
127+
if (!$this->writeable) {
128+
throw new RuntimeException('Cannot alter session after it is closed');
129+
}
130+
131+
$this->modified = true;
132+
unset($this->contents[$name]);
133+
}
134+
135+
public function offsetGet($name): mixed
136+
{
137+
return $this->contents[$name];
138+
}
139+
97140
/**
98141
* @param ?array<string, mixed> $contents
99142
*/

‎tests/behavior/AccessContext.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,4 +101,72 @@ public function propertyWriteSucceeds(): void
101101
Assert::assertCount(1, $this->session);
102102
Assert::assertTrue(isset($this->session->bar));
103103
}
104+
105+
/**
106+
* @Then array access check returns true
107+
*/
108+
public function arrayAccessCheckReturnsTrue(): void
109+
{
110+
Assert::assertTrue(isset($this->session['foo']));
111+
}
112+
113+
/**
114+
* @Then array access check returns false
115+
*/
116+
public function arrayAccessCheckReturnsFalse(): void
117+
{
118+
Assert::assertFalse(isset($this->session['foo']));
119+
}
120+
121+
/**
122+
* @Then array access read returns data
123+
*/
124+
public function arrayAccessReadReturnsData(): void
125+
{
126+
Assert::assertEquals('bar', $this->session['foo']);
127+
}
128+
129+
/**
130+
* @Then array access read triggers error
131+
*/
132+
public function arrayAccessReadTriggersNoticeError(): void
133+
{
134+
try {
135+
$errorThrown = false;
136+
$bar = $this->session['bar'];
137+
// @phpstan-ignore-next-line
138+
} catch (Throwable $e) {
139+
$errorThrown = true;
140+
} finally {
141+
Assert::assertTrue($errorThrown);
142+
}
143+
}
144+
145+
/**
146+
* @Then array access read returns null
147+
*/
148+
public function arrayAccessReadReturnsNull(): void
149+
{
150+
$bar = @$this->session['foo'];
151+
Assert::assertSame(null, $bar);
152+
}
153+
154+
/**
155+
* @Then array access read with null coalesce returns null
156+
*/
157+
public function arrayAccessReadWithNullCoalesceReturnsNull(): void
158+
{
159+
$bar = $this->session['foo'] ?? null;
160+
Assert::assertSame(null, $bar);
161+
}
162+
163+
/**
164+
* @Then array access write succeeds
165+
*/
166+
public function arrayAccessWriteSucceeds(): void
167+
{
168+
$this->session['bar'] = 'baz';
169+
Assert::assertCount(1, $this->session);
170+
Assert::assertTrue(isset($this->session['bar']));
171+
}
104172
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /