|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of the Phalcon Framework. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Phalcon Team <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE.txt |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
declare(strict_types=1); |
|
13
|
|
|
|
|
14
|
|
|
namespace Phalcon; |
|
15
|
|
|
|
|
16
|
|
|
use Traversable; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Phalcon\Registry |
|
20
|
|
|
* |
|
21
|
|
|
* A registry is a container for storing objects and values in the application |
|
22
|
|
|
* space. By storing the value in a registry, the same object is always |
|
23
|
|
|
* available throughout your application. |
|
24
|
|
|
* |
|
25
|
|
|
*```php |
|
26
|
|
|
* $registry = new \Phalcon\Registry(); |
|
27
|
|
|
* |
|
28
|
|
|
* // Set value |
|
29
|
|
|
* $registry->something = "something"; |
|
30
|
|
|
* // or |
|
31
|
|
|
* $registry["something"] = "something"; |
|
32
|
|
|
* |
|
33
|
|
|
* // Get value |
|
34
|
|
|
* $value = $registry->something; |
|
35
|
|
|
* // or |
|
36
|
|
|
* $value = $registry["something"]; |
|
37
|
|
|
* |
|
38
|
|
|
* // Check if the key exists |
|
39
|
|
|
* $exists = isset($registry->something); |
|
40
|
|
|
* // or |
|
41
|
|
|
* $exists = isset($registry["something"]); |
|
42
|
|
|
* |
|
43
|
|
|
* // Unset |
|
44
|
|
|
* unset($registry->something); |
|
45
|
|
|
* // or |
|
46
|
|
|
* unset($registry["something"]); |
|
47
|
|
|
*``` |
|
48
|
|
|
* |
|
49
|
|
|
* In addition to ArrayAccess, Phalcon\Registry also implements Countable |
|
50
|
|
|
* (count($registry) will return the number of elements in the registry), |
|
51
|
|
|
* Serializable and Iterator (you can iterate over the registry using a foreach |
|
52
|
|
|
* loop) interfaces. For PHP 5.4 and higher, JsonSerializable interface is |
|
53
|
|
|
* implemented. |
|
54
|
|
|
* |
|
55
|
|
|
* Phalcon\Registry is very fast (it is typically faster than any userspace |
|
56
|
|
|
* implementation of the registry); however, this comes at a price: |
|
57
|
|
|
* Phalcon\Registry is a final class and cannot be inherited from. |
|
58
|
|
|
* |
|
59
|
|
|
* Though Phalcon\Registry exposes methods like __get(), offsetGet(), count() |
|
60
|
|
|
* etc, it is not recommended to invoke them manually (these methods exist |
|
61
|
|
|
* mainly to match the interfaces the registry implements): |
|
62
|
|
|
* $registry->__get("property") is several times slower than |
|
63
|
|
|
* $registry->property. |
|
64
|
|
|
* |
|
65
|
|
|
* Internally all the magic methods (and interfaces except JsonSerializable) |
|
66
|
|
|
* are implemented using object handlers or similar techniques: this allows to |
|
67
|
|
|
* bypass relatively slow method calls. |
|
68
|
|
|
*/ |
|
69
|
|
|
final class Registry extends Collection |
|
70
|
|
|
{ |
|
71
|
|
|
/** |
|
72
|
|
|
* Registry constructor. |
|
73
|
|
|
* |
|
74
|
|
|
* @param array|null $data |
|
75
|
|
|
*/ |
|
76
|
|
|
final public function __construct(array $data = []) |
|
77
|
|
|
{ |
|
78
|
|
|
parent::__construct($data); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Magic getter to get an element from the collection |
|
83
|
|
|
* |
|
84
|
|
|
* @param string $element |
|
85
|
|
|
* |
|
86
|
|
|
* @return mixed|null |
|
87
|
|
|
*/ |
|
88
|
|
|
final public function __get(string $element) |
|
89
|
|
|
{ |
|
90
|
|
|
return parent::get($element); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Magic isset to check whether an element exists or not |
|
95
|
|
|
* |
|
96
|
|
|
* @param string $element |
|
97
|
|
|
* |
|
98
|
|
|
* @return bool |
|
99
|
|
|
*/ |
|
100
|
|
|
final public function __isset(string $element): bool |
|
101
|
|
|
{ |
|
102
|
|
|
return parent::has($element); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Magic setter to assign values to an element |
|
107
|
|
|
* |
|
108
|
|
|
* @param string $element |
|
109
|
|
|
* @param mixed $value |
|
110
|
|
|
*/ |
|
111
|
|
|
final public function __set(string $element, $value): void |
|
112
|
|
|
{ |
|
113
|
|
|
parent::set($element, $value); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Magic unset to remove an element from the collection |
|
118
|
|
|
* |
|
119
|
|
|
* @param string $element |
|
120
|
|
|
*/ |
|
121
|
|
|
final public function __unset(string $element): void |
|
122
|
|
|
{ |
|
123
|
|
|
parent::remove($element); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Clears the internal collection |
|
128
|
|
|
*/ |
|
129
|
|
|
final public function clear(): void |
|
130
|
|
|
{ |
|
131
|
|
|
parent::clear(); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* Count elements of an object |
|
136
|
|
|
* |
|
137
|
|
|
* @link https://php.net/manual/en/countable.count.php |
|
138
|
|
|
*/ |
|
139
|
|
|
final public function count(): int |
|
140
|
|
|
{ |
|
141
|
|
|
return parent::count(); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Get the element from the collection |
|
146
|
|
|
* |
|
147
|
|
|
* @param string $element |
|
148
|
|
|
* @param mixed|null $defaultValue |
|
149
|
|
|
* @param string|null $cast |
|
150
|
|
|
* |
|
151
|
|
|
* @return mixed|null |
|
152
|
|
|
*/ |
|
153
|
|
|
final public function get( |
|
154
|
|
|
string $element, |
|
155
|
|
|
$defaultValue = null, |
|
156
|
|
|
string $cast = null |
|
157
|
|
|
) { |
|
158
|
|
|
return parent::get($element, $defaultValue, $cast); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* Returns the iterator of the class |
|
163
|
|
|
*/ |
|
164
|
|
|
final public function getIterator(): Traversable |
|
165
|
|
|
{ |
|
166
|
|
|
return parent::getIterator(); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* Get the element from the collection |
|
171
|
|
|
* |
|
172
|
|
|
* @param string $element |
|
173
|
|
|
* |
|
174
|
|
|
* @return bool |
|
175
|
|
|
*/ |
|
176
|
|
|
final public function has(string $element): bool |
|
177
|
|
|
{ |
|
178
|
|
|
return parent::has($element); |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* Initialize internal array |
|
183
|
|
|
* |
|
184
|
|
|
* @param array $data |
|
185
|
|
|
*/ |
|
186
|
|
|
final public function init(array $data = []): void |
|
187
|
|
|
{ |
|
188
|
|
|
parent::init($data); |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* Specify data which should be serialized to JSON |
|
193
|
|
|
* |
|
194
|
|
|
* @link https://php.net/manual/en/jsonserializable.jsonserialize.php |
|
195
|
|
|
*/ |
|
196
|
|
|
final public function jsonSerialize(): array |
|
197
|
|
|
{ |
|
198
|
|
|
return parent::jsonSerialize(); |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* Whether a offset exists |
|
203
|
|
|
* |
|
204
|
|
|
* @param mixed $element |
|
205
|
|
|
* |
|
206
|
|
|
* @return bool |
|
207
|
|
|
*/ |
|
208
|
|
|
final public function offsetExists($element): bool |
|
209
|
|
|
{ |
|
210
|
|
|
return parent::has($element); |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
/** |
|
214
|
|
|
* Offset to retrieve |
|
215
|
|
|
* |
|
216
|
|
|
* @param mixed $element |
|
217
|
|
|
* |
|
218
|
|
|
* @return mixed|null |
|
219
|
|
|
*/ |
|
220
|
|
|
final public function offsetGet($element) |
|
221
|
|
|
{ |
|
222
|
|
|
return parent::get($element); |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
/** |
|
226
|
|
|
* Offset to set |
|
227
|
|
|
* |
|
228
|
|
|
* @param mixed $element |
|
229
|
|
|
* @param mixed $value |
|
230
|
|
|
*/ |
|
231
|
|
|
final public function offsetSet($element, $value): void |
|
232
|
|
|
{ |
|
233
|
|
|
parent::set($element, $value); |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
/** |
|
237
|
|
|
* Offset to unset |
|
238
|
|
|
* |
|
239
|
|
|
* @param mixed $element |
|
240
|
|
|
*/ |
|
241
|
|
|
final public function offsetUnset($element): void |
|
242
|
|
|
{ |
|
243
|
|
|
parent::remove($element); |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
|
|
/** |
|
247
|
|
|
* Delete the element from the collection |
|
248
|
|
|
* |
|
249
|
|
|
* @param string $element |
|
250
|
|
|
*/ |
|
251
|
|
|
final public function remove(string $element): void |
|
252
|
|
|
{ |
|
253
|
|
|
parent::remove($element); |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
/** |
|
257
|
|
|
* String representation of object |
|
258
|
|
|
* |
|
259
|
|
|
* @link https://php.net/manual/en/serializable.serialize.php |
|
260
|
|
|
*/ |
|
261
|
|
|
final public function serialize(): string |
|
262
|
|
|
{ |
|
263
|
|
|
return parent::serialize(); |
|
264
|
|
|
} |
|
265
|
|
|
|
|
266
|
|
|
/** |
|
267
|
|
|
* Set an element in the collection |
|
268
|
|
|
* |
|
269
|
|
|
* @param string $element |
|
270
|
|
|
* @param mixed $value |
|
271
|
|
|
*/ |
|
272
|
|
|
final public function set(string $element, $value): void |
|
273
|
|
|
{ |
|
274
|
|
|
parent::set($element, $value); |
|
275
|
|
|
} |
|
276
|
|
|
|
|
277
|
|
|
/** |
|
278
|
|
|
* Returns the object in an array format |
|
279
|
|
|
*/ |
|
280
|
|
|
final public function toArray(): array |
|
281
|
|
|
{ |
|
282
|
|
|
return parent::toArray(); |
|
283
|
|
|
} |
|
284
|
|
|
|
|
285
|
|
|
/** |
|
286
|
|
|
* Returns the object in a JSON format |
|
287
|
|
|
* |
|
288
|
|
|
* The default string uses the following options for json_encode |
|
289
|
|
|
* |
|
290
|
|
|
* JSON_HEX_TAG, JSON_HEX_APOS, JSON_HEX_AMP, JSON_HEX_QUOT, |
|
291
|
|
|
* JSON_UNESCAPED_SLASHES |
|
292
|
|
|
* |
|
293
|
|
|
* @see https://www.ietf.org/rfc/rfc4627.txt |
|
294
|
|
|
* |
|
295
|
|
|
* @param int $options |
|
296
|
|
|
* |
|
297
|
|
|
* @return string |
|
298
|
|
|
*/ |
|
299
|
|
|
final public function toJson(int $options = 79): string |
|
300
|
|
|
{ |
|
301
|
|
|
return parent::toJson($options); |
|
302
|
|
|
} |
|
303
|
|
|
|
|
304
|
|
|
/** |
|
305
|
|
|
* Constructs the object |
|
306
|
|
|
* |
|
307
|
|
|
* @param string $serialized |
|
308
|
|
|
*/ |
|
309
|
|
|
final public function unserialize($serialized): void |
|
310
|
|
|
{ |
|
311
|
|
|
parent::unserialize($serialized); |
|
312
|
|
|
} |
|
313
|
|
|
} |
|
314
|
|
|
|