1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Spiral Framework. |
5
|
|
|
* |
6
|
|
|
* @license MIT |
7
|
|
|
* @author Anton Titov (Wolfy-J) |
8
|
|
|
*/ |
9
|
|
|
declare(strict_types=1); |
10
|
|
|
|
11
|
|
|
namespace Spiral\Session; |
12
|
|
|
|
13
|
|
|
use Spiral\Core\Exception\ScopeException; |
14
|
|
|
|
15
|
|
|
final class SectionScope implements SessionSectionInterface |
16
|
|
|
{ |
17
|
|
|
/** @var SessionInterface */ |
18
|
|
|
private $session; |
19
|
|
|
|
20
|
|
|
/*** @var string */ |
21
|
|
|
private $name; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param SessionScope $session |
25
|
|
|
* @param string $name |
26
|
|
|
*/ |
27
|
|
|
public function __construct(SessionScope $session, string $name) |
28
|
|
|
{ |
29
|
|
|
$this->session = $session; |
30
|
|
|
$this->name = $name; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Shortcut for get. |
35
|
|
|
* |
36
|
|
|
* @param string $name |
37
|
|
|
* @return mixed|null |
38
|
|
|
*/ |
39
|
|
|
public function __get(string $name) |
40
|
|
|
{ |
41
|
|
|
return $this->get($name); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param string $name |
46
|
|
|
* @param mixed $value |
47
|
|
|
*/ |
48
|
|
|
public function __set(string $name, $value): void |
49
|
|
|
{ |
50
|
|
|
$this->set($name, $value); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param string $name |
55
|
|
|
* @return bool |
56
|
|
|
*/ |
57
|
|
|
public function __isset(string $name) |
58
|
|
|
{ |
59
|
|
|
return $this->has($name); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param string $name |
64
|
|
|
*/ |
65
|
|
|
public function __unset(string $name): void |
66
|
|
|
{ |
67
|
|
|
$this->delete($name); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @inheritDoc |
72
|
|
|
*/ |
73
|
|
|
public function getIterator() |
74
|
|
|
{ |
75
|
|
|
return $this->getActiveSection()->getIterator(); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @inheritDoc |
80
|
|
|
*/ |
81
|
|
|
public function offsetExists($offset) |
82
|
|
|
{ |
83
|
|
|
return $this->getActiveSection()->offsetExists($offset); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @inheritDoc |
88
|
|
|
*/ |
89
|
|
|
public function offsetGet($offset) |
90
|
|
|
{ |
91
|
|
|
return $this->getActiveSection()->offsetGet($offset); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @inheritDoc |
96
|
|
|
*/ |
97
|
|
|
public function offsetSet($offset, $value): void |
98
|
|
|
{ |
99
|
|
|
$this->getActiveSection()->offsetSet($offset, $value); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @inheritDoc |
104
|
|
|
*/ |
105
|
|
|
public function offsetUnset($offset): void |
106
|
|
|
{ |
107
|
|
|
$this->getActiveSection()->offsetUnset($offset); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @inheritDoc |
112
|
|
|
*/ |
113
|
|
|
public function getName(): string |
114
|
|
|
{ |
115
|
|
|
return $this->name; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @inheritDoc |
120
|
|
|
*/ |
121
|
|
|
public function getAll(): array |
122
|
|
|
{ |
123
|
|
|
return $this->getActiveSection()->getAll(); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @inheritDoc |
128
|
|
|
*/ |
129
|
|
|
public function set(string $name, $value): void |
130
|
|
|
{ |
131
|
|
|
$this->getActiveSection()->set($name, $value); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @inheritDoc |
136
|
|
|
*/ |
137
|
|
|
public function has(string $name): bool |
138
|
|
|
{ |
139
|
|
|
return $this->getActiveSection()->has($name); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @inheritDoc |
144
|
|
|
*/ |
145
|
|
|
public function get(string $name, $default = null) |
146
|
|
|
{ |
147
|
|
|
return $this->getActiveSection()->get($name, $default); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @inheritDoc |
152
|
|
|
*/ |
153
|
|
|
public function pull(string $name, $default = null) |
154
|
|
|
{ |
155
|
|
|
return $this->getActiveSection()->pull($name, $default); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @inheritDoc |
160
|
|
|
*/ |
161
|
|
|
public function delete(string $name): void |
162
|
|
|
{ |
163
|
|
|
$this->getActiveSection()->delete($name); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @inheritDoc |
168
|
|
|
*/ |
169
|
|
|
public function clear(): void |
170
|
|
|
{ |
171
|
|
|
$this->getActiveSection()->clear(); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* @return SessionSectionInterface |
176
|
|
|
* |
177
|
|
|
* @throws ScopeException |
178
|
|
|
*/ |
179
|
|
|
private function getActiveSection(): SessionSectionInterface |
180
|
|
|
{ |
181
|
|
|
return $this->session->getActiveSession()->getSection($this->name); |
|
|
|
|
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|