|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Caridea |
|
4
|
|
|
* |
|
5
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may not |
|
6
|
|
|
* use this file except in compliance with the License. You may obtain a copy of |
|
7
|
|
|
* the License at |
|
8
|
|
|
* |
|
9
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
10
|
|
|
* |
|
11
|
|
|
* Unless required by applicable law or agreed to in writing, software |
|
12
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
|
13
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
|
14
|
|
|
* License for the specific language governing permissions and limitations under |
|
15
|
|
|
* the License. |
|
16
|
|
|
* |
|
17
|
|
|
* @copyright 2015-2018 LibreWorks contributors |
|
18
|
|
|
* @license Apache-2.0 |
|
19
|
|
|
*/ |
|
20
|
|
|
namespace Caridea\Session; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Session value namespace |
|
24
|
|
|
* |
|
25
|
|
|
* @copyright 2015-2018 LibreWorks contributors |
|
26
|
|
|
* @license Apache-2.0 |
|
27
|
|
|
*/ |
|
28
|
|
|
class Values implements Map |
|
29
|
|
|
{ |
|
30
|
|
|
/** |
|
31
|
|
|
* @var Session |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $session; |
|
34
|
|
|
/** |
|
35
|
|
|
* @var string |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $name; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Creates a new Session value namespace. |
|
41
|
|
|
* |
|
42
|
|
|
* @param \Caridea\Session\Session $session The session utility |
|
43
|
|
|
* @param string $name The session value namespace |
|
44
|
|
|
*/ |
|
45
|
8 |
|
public function __construct(Session $session, string $name) |
|
46
|
|
|
{ |
|
47
|
8 |
|
if ($name === null || !strlen(trim($name))) { |
|
48
|
|
|
throw new \InvalidArgumentException('Session namespace cannot be blank'); |
|
49
|
|
|
} |
|
50
|
8 |
|
$this->name = $name; |
|
51
|
8 |
|
$this->session = $session; |
|
52
|
8 |
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* {@inheritDoc} |
|
56
|
|
|
*/ |
|
57
|
1 |
|
public function clear(): void |
|
58
|
|
|
{ |
|
59
|
1 |
|
if ($this->resume()) { |
|
60
|
1 |
|
$_SESSION[$this->name] = []; |
|
61
|
|
|
} |
|
62
|
1 |
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* {@inheritDoc} |
|
66
|
|
|
*/ |
|
67
|
2 |
|
public function count(): int |
|
68
|
|
|
{ |
|
69
|
2 |
|
$this->resume(); |
|
70
|
2 |
|
return count($_SESSION[$this->name]); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* {@inheritDoc} |
|
75
|
|
|
*/ |
|
76
|
1 |
|
public function get(string $offset, $alt = null) |
|
77
|
|
|
{ |
|
78
|
1 |
|
$this->resume(); |
|
79
|
1 |
|
return isset($_SESSION[$this->name][$offset]) ? |
|
80
|
1 |
|
$_SESSION[$this->name][$offset] : $alt; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* {@inheritDoc} |
|
85
|
|
|
*/ |
|
86
|
1 |
|
public function getIterator(): \Iterator |
|
87
|
|
|
{ |
|
88
|
1 |
|
$this->resume(); |
|
89
|
1 |
|
return new \ArrayIterator($_SESSION[$this->name]); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Gets the values namespace in the session. |
|
94
|
|
|
* |
|
95
|
|
|
* @return string The values namespace |
|
96
|
|
|
*/ |
|
97
|
1 |
|
public function getNamespace(): string |
|
98
|
|
|
{ |
|
99
|
1 |
|
return $this->name; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* {@inheritDoc} |
|
104
|
|
|
*/ |
|
105
|
1 |
|
public function merge(\Caridea\Session\Map $values): void |
|
106
|
|
|
{ |
|
107
|
1 |
|
$this->start(); |
|
108
|
1 |
|
if ($values instanceof Values) { |
|
109
|
1 |
|
$values->resume(); |
|
110
|
1 |
|
$_SESSION[$this->name] = array_merge($_SESSION[$this->name], $_SESSION[$values->name]); |
|
111
|
|
|
} else { |
|
112
|
1 |
|
foreach ($values as $k => $v) { |
|
113
|
1 |
|
$_SESSION[$this->name][$k] = $v; |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
1 |
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* {@inheritDoc} |
|
120
|
|
|
*/ |
|
121
|
1 |
|
public function offsetExists($offset): bool |
|
122
|
|
|
{ |
|
123
|
1 |
|
$this->resume(); |
|
124
|
1 |
|
return isset($_SESSION[$this->name][$offset]); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* {@inheritDoc} |
|
129
|
|
|
*/ |
|
130
|
1 |
|
public function offsetGet($offset) |
|
131
|
|
|
{ |
|
132
|
1 |
|
$this->resume(); |
|
133
|
1 |
|
return $this->get($offset); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* {@inheritDoc} |
|
138
|
|
|
*/ |
|
139
|
3 |
|
public function offsetSet($offset, $value) |
|
140
|
|
|
{ |
|
141
|
3 |
|
$this->start(); |
|
142
|
3 |
|
$_SESSION[$this->name][$offset] = $value; |
|
143
|
3 |
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* {@inheritDoc} |
|
147
|
|
|
*/ |
|
148
|
1 |
|
public function offsetUnset($offset) |
|
149
|
|
|
{ |
|
150
|
1 |
|
if ($this->resume()) { |
|
151
|
1 |
|
unset($_SESSION[$this->name][$offset]); |
|
152
|
|
|
} |
|
153
|
1 |
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* Resumes the session. |
|
157
|
|
|
* |
|
158
|
|
|
* @return bool Whether the session resumed successfully |
|
159
|
|
|
*/ |
|
160
|
2 |
|
protected function resume(): bool |
|
161
|
|
|
{ |
|
162
|
2 |
|
return $this->session->resume() && $this->init(); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* Starts the session, trying to resume it first. |
|
167
|
|
|
* |
|
168
|
|
|
* @return bool Whether the session started successfully |
|
169
|
|
|
*/ |
|
170
|
1 |
|
protected function start(): bool |
|
171
|
|
|
{ |
|
172
|
1 |
|
return $this->resume() || ($this->session->start() && $this->init()); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* Initializes the session. |
|
177
|
|
|
* |
|
178
|
|
|
* @return bool Always `true` |
|
179
|
|
|
*/ |
|
180
|
1 |
|
protected function init(): bool |
|
181
|
|
|
{ |
|
182
|
1 |
|
if (!isset($_SESSION[$this->name])) { |
|
183
|
1 |
|
$_SESSION[$this->name] = []; |
|
184
|
|
|
} |
|
185
|
1 |
|
return true; |
|
186
|
|
|
} |
|
187
|
|
|
} |
|
188
|
|
|
|