1 | <?php declare(strict_types=1); |
||
5 | final class Session |
||
6 | { |
||
7 | /** |
||
8 | * @var string |
||
9 | */ |
||
10 | private $id; |
||
11 | |||
12 | /** |
||
13 | * @var array |
||
14 | */ |
||
15 | private $contents; |
||
16 | |||
17 | /** |
||
18 | * @var SessionIdInterface |
||
19 | */ |
||
20 | private $sessionId; |
||
21 | |||
22 | /** |
||
23 | * @var string[] |
||
24 | */ |
||
25 | private $oldIds = []; |
||
26 | |||
27 | /** |
||
28 | * @var int |
||
29 | */ |
||
30 | private $status = \PHP_SESSION_NONE; |
||
31 | |||
32 | /** |
||
33 | * @param string $id |
||
34 | * @param array $contents |
||
35 | * @param SessionIdInterface $sessionId |
||
36 | */ |
||
37 | 28 | public function __construct(string $id, array $contents, SessionIdInterface $sessionId) |
|
47 | |||
48 | /** |
||
49 | * @return string |
||
50 | */ |
||
51 | 21 | public function getId(): string |
|
55 | |||
56 | /** |
||
57 | * @param array $contents |
||
58 | */ |
||
59 | 4 | public function setContents(array $contents): void |
|
63 | |||
64 | /** |
||
65 | * @return array |
||
66 | */ |
||
67 | 18 | public function getContents(): array |
|
71 | |||
72 | /** |
||
73 | * @return string[] |
||
74 | */ |
||
75 | 19 | public function getOldIds(): array |
|
79 | |||
80 | 14 | public function begin(): void |
|
81 | { |
||
82 | 14 | if ($this->status === \PHP_SESSION_ACTIVE) { |
|
83 | return; |
||
84 | } |
||
85 | |||
86 | 14 | $this->status = \PHP_SESSION_ACTIVE; |
|
87 | |||
88 | 14 | if ($this->id === '') { |
|
89 | 14 | $this->id = $this->sessionId->generate(); |
|
90 | } |
||
91 | 14 | } |
|
92 | |||
93 | 2 | public function end(): void |
|
104 | |||
105 | /** |
||
106 | * @return bool |
||
107 | */ |
||
108 | 20 | public function isActive(): bool |
|
112 | |||
113 | 3 | public function regenerate(): bool |
|
114 | { |
||
115 | // Can only regenerate active sessions |
||
116 | 3 | if ($this->status !== \PHP_SESSION_ACTIVE) { |
|
117 | 1 | return false; |
|
118 | } |
||
119 | |||
120 | 3 | $this->oldIds[] = $this->id; |
|
121 | 3 | $this->id = $this->sessionId->generate(); |
|
122 | |||
123 | 3 | return true; |
|
124 | } |
||
125 | |||
126 | 2 | public function toArray(): array |
|
135 | |||
136 | /** |
||
137 | * @param array $session |
||
138 | * @param bool $clone |
||
139 | * @throws \InvalidArgumentException |
||
140 | * @return Session |
||
141 | */ |
||
142 | 6 | public function fromArray(array $session, bool $clone = true): self |
|
159 | } |
||
160 |