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