Skip to content

Commit 620f804

Browse files
authored
Merge pull request #3 from WyriHaximus/to-from-array
Convert a session to an from array for (JSON) encoding/decoding
2 parents f194886 + 4507d45 commit 620f804

File tree

3 files changed

+73
-0
lines changed

3 files changed

+73
-0
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,17 @@ Using this middleware together with [`wyrihaximus/react-http-middleware-response
5757
please take a look at [`wyrihaximus/react-http-middleware-response-cache-session-cache-configuration`](https://github.com/WyriHaximus/reactphp-http-middleware-response-cache-session-cache-configuration) to
5858
ensure you don't cache responses from users with active sessions.
5959

60+
## To/From array
61+
62+
In case you need to pass a session into a child process it has `toArray` and `fromArray` methods:
63+
64+
```php
65+
$array = $session->toArray();
66+
// Transfer to child process
67+
$session = (new Session('', [], new RandomBytes()))->fromArray($array);
68+
// The same can be done transferring changes back to the parent
69+
```
70+
6071
# License
6172

6273
The MIT License (MIT)

src/Session.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,4 +122,34 @@ public function regenerate(): bool
122122

123123
return true;
124124
}
125+
126+
public function toArray(): array
127+
{
128+
return [
129+
'id' => $this->id,
130+
'contents' => $this->contents,
131+
'oldIds' => $this->oldIds,
132+
'status' => $this->status,
133+
];
134+
}
135+
136+
/**
137+
* @param array $session
138+
* @throws \InvalidArgumentException
139+
* @return Session
140+
*/
141+
public function fromArray(array $session): self
142+
{
143+
if (!isset($session['id']) || !isset($session['contents']) || !isset($session['oldIds']) || !isset($session['oldIds'])) {
144+
throw new \InvalidArgumentException('Session array most contain "id", "contents", "oldIds", and "status".');
145+
}
146+
147+
$clone = clone $this;
148+
$clone->id = $session['id'];
149+
$clone->contents = $session['contents'];
150+
$clone->oldIds = $session['oldIds'];
151+
$clone->status = $session['status'];
152+
153+
return $clone;
154+
}
125155
}

tests/SessionTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,36 @@ public function testState()
7474
], $session->getOldIds());
7575
self::assertSame([], $session->getContents());
7676
}
77+
78+
public function testToFromArray()
79+
{
80+
$session = new Session('', [], new RandomBytes());
81+
82+
self::assertSame(
83+
[
84+
'id' => '',
85+
'contents' => [],
86+
'oldIds' => [],
87+
'status' => 1,
88+
],
89+
$session->toArray()
90+
);
91+
92+
$session->begin();
93+
94+
self::assertSame(
95+
[
96+
'id' => $session->getId(),
97+
'contents' => [],
98+
'oldIds' => [],
99+
'status' => 2,
100+
],
101+
$session->toArray()
102+
);
103+
104+
$array = $session->toArray();
105+
$newSession = $session->fromArray($array);
106+
self::assertNotSame($session, $newSession);
107+
self::assertSame($array, $newSession->toArray());
108+
}
77109
}

0 commit comments

Comments
 (0)