@@ -21,46 +21,10 @@ class DotArray implements
21
21
/* Traits. */
22
22
use DotFilteringTrait;
23
23
24
- /**
25
- * Internal Dot Path Config.
26
- *
27
- * @var array
28
- */
29
- protected static $ dotPathConfig = [
30
- 'template ' => '#(?|(?|[<token-start>](.*?)[<token-end>])|(.*?))(?:$|\.+)#i ' ,
31
- 'wrapKey ' => '{%s} ' ,
32
- 'wildcards ' => [
33
- '<token-start> ' => ['\'' , '\" ' , '\[ ' , '\( ' , '\{ ' ],
34
- '<token-end> ' => ['\'' , '\" ' , '\] ' , '\) ' , '\} ' ],
35
- ],
36
- ];
37
-
38
- /**
39
- * The cached pattern that allow to match the JSON paths that use the dot notation.
40
- *
41
- * Allowed tokens for more complex paths: '', "", [], (), {}
42
- * Examples:
43
- *
44
- * - foo.bar
45
- * - foo.'bar'
46
- * - foo."bar"
47
- * - foo.[bar]
48
- * - foo.(bar)
49
- * - foo.{bar}
50
- *
51
- * Or more complex:
52
- * - foo.{bar}.[component].{version.1.0}
53
- *
54
- * @var string
55
- */
56
- protected static $ dotPathPattern ;
57
-
58
- /**
59
- * Unique object identifier.
60
- *
61
- * @var string
62
- */
63
- protected $ uniqueIdentifier ;
24
+ private const TEMPLATE_PATTERN = '#(?|(?|[%s](.*?)[%s])|(.*?))(?:$|\.+)#i ' ;
25
+ private const WRAP_KEY = '{%s} ' ;
26
+ private const TOKEN_START = ['\'' , '\" ' , '\[ ' , '\( ' , '\{ ' ];
27
+ private const TOKEN_END = ['\'' , '\" ' , '\] ' , '\) ' , '\} ' ];
64
28
65
29
/**
66
30
* Stores the original data.
@@ -92,23 +56,34 @@ public static function createFromJson($json)
92
56
}
93
57
94
58
/**
95
- * Getting the dot path pattern.
59
+ * Getting the path pattern.
60
+ *
61
+ * Allowed tokens for more complex paths: '', "", [], (), {}
62
+ * Examples:
63
+ *
64
+ * - foo.bar
65
+ * - foo.'bar'
66
+ * - foo."bar"
67
+ * - foo.[bar]
68
+ * - foo.(bar)
69
+ * - foo.{bar}
70
+ *
71
+ * Or more complex:
72
+ * - foo.{bar}.[component].{version.1.0}
96
73
*
97
74
* @return string
98
75
*/
99
- protected static function dotPathPattern ()
76
+ protected static function pathPattern ()
100
77
{
101
- if (empty (self ::$ dotPathPattern )) {
102
- $ path = self ::$ dotPathConfig ['template ' ];
103
-
104
- foreach (self ::$ dotPathConfig ['wildcards ' ] as $ wildcard => $ tokens ) {
105
- $ path = \str_replace ($ wildcard , \implode ('' , $ tokens ), $ path );
106
- }
107
-
108
- self ::$ dotPathPattern = $ path ;
109
- }
110
-
111
- return self ::$ dotPathPattern ;
78
+ return (
79
+ vsprintf (
80
+ self ::TEMPLATE_PATTERN ,
81
+ [
82
+ \implode ('' , self ::TOKEN_START ),
83
+ \implode ('' , self ::TOKEN_END ),
84
+ ]
85
+ )
86
+ );
112
87
}
113
88
114
89
/**
@@ -118,7 +93,7 @@ protected static function dotPathPattern()
118
93
*
119
94
* @return array
120
95
*/
121
- protected static function pathToSegments ($ path )
96
+ final protected static function pathToSegments ($ path )
122
97
{
123
98
$ path = \trim ($ path , "\t\n\r0円\x0B\. " );
124
99
$ segments = [];
@@ -128,7 +103,7 @@ protected static function pathToSegments($path)
128
103
return [];
129
104
}
130
105
131
- \preg_match_all (static :: dotPathPattern (), $ path , $ matches );
106
+ \preg_match_all (self :: pathPattern (), $ path , $ matches );
132
107
133
108
if (!empty ($ matches [1 ])) {
134
109
$ matches = $ matches [1 ];
@@ -153,28 +128,28 @@ function ($match) {
153
128
*
154
129
* @return string
155
130
*/
156
- protected static function wrapSegmentKey ($ key )
131
+ final protected static function wrapSegmentKey ($ key )
157
132
{
158
- return vsprintf (static :: $ dotPathConfig [ ' wrapKey ' ] , [$ key ]);
133
+ return vsprintf (self :: WRAP_KEY , [$ key ]);
159
134
}
160
135
161
136
/**
162
137
* @param array $segments
163
138
*
164
139
* @return string
165
140
*/
166
- protected static function segmentsToKey (array $ segments )
141
+ final protected static function segmentsToKey (array $ segments )
167
142
{
168
143
return (
169
- \implode (
170
- '. ' ,
171
- \array_map (
172
- function ($ segment ) {
173
- return static ::wrapSegmentKey ($ segment );
174
- },
175
- $ segments
144
+ \implode (
145
+ '. ' ,
146
+ \array_map (
147
+ function ($ segment ) {
148
+ return self ::wrapSegmentKey ($ segment );
149
+ },
150
+ $ segments
151
+ )
176
152
)
177
- )
178
153
);
179
154
}
180
155
@@ -187,15 +162,15 @@ function ($segment) {
187
162
*
188
163
* @return array
189
164
*/
190
- protected static function flatten (array $ items , $ prepend = [])
165
+ final protected static function flatten (array $ items , $ prepend = [])
191
166
{
192
167
$ flatten = [];
193
168
194
169
foreach ($ items as $ key => $ value ) {
195
170
if (\is_array ($ value ) && !empty ($ value )) {
196
171
$ flatten = array_merge (
197
172
$ flatten ,
198
- static ::flatten (
173
+ self ::flatten (
199
174
$ value ,
200
175
array_merge ($ prepend , [$ key ])
201
176
)
@@ -204,7 +179,7 @@ protected static function flatten(array $items, $prepend = [])
204
179
continue ;
205
180
}
206
181
207
- $ segmentsToKey = static ::segmentsToKey (array_merge ($ prepend , [$ key ]));
182
+ $ segmentsToKey = self ::segmentsToKey (array_merge ($ prepend , [$ key ]));
208
183
209
184
$ flatten [$ segmentsToKey ] = $ value ;
210
185
}
@@ -219,7 +194,7 @@ protected static function flatten(array $items, $prepend = [])
219
194
*
220
195
* @return array
221
196
*/
222
- protected static function normalize ($ items )
197
+ final protected static function normalize ($ items )
223
198
{
224
199
if ($ items instanceof self) {
225
200
$ items = $ items ->toArray ();
@@ -228,7 +203,7 @@ protected static function normalize($items)
228
203
if (\is_array ($ items )) {
229
204
foreach ($ items as $ k => $ v ) {
230
205
if (\is_array ($ v ) || $ v instanceof self) {
231
- $ v = static ::normalize ($ v );
206
+ $ v = self ::normalize ($ v );
232
207
}
233
208
$ items [$ k ] = $ v ;
234
209
}
@@ -243,9 +218,9 @@ protected static function normalize($items)
243
218
*
244
219
* @return array
245
220
*/
246
- protected static function mergeRecursive ($ array1 , $ array2 = null )
221
+ final protected static function mergeRecursive ($ array1 , $ array2 = null )
247
222
{
248
- $ args = static ::normalize (\func_get_args ());
223
+ $ args = self ::normalize (\func_get_args ());
249
224
$ res = \array_shift ($ args );
250
225
251
226
while (!empty ($ args )) {
@@ -256,7 +231,7 @@ protected static function mergeRecursive($array1, $array2 = null)
256
231
}
257
232
258
233
if (\is_array ($ v ) && isset ($ res [$ k ]) && \is_array ($ res [$ k ])) {
259
- $ v = static ::mergeRecursive ($ res [$ k ], $ v );
234
+ $ v = self ::mergeRecursive ($ res [$ k ], $ v );
260
235
}
261
236
262
237
$ res [$ k ] = $ v ;
@@ -273,17 +248,14 @@ protected static function mergeRecursive($array1, $array2 = null)
273
248
*/
274
249
public function __construct ($ items = [])
275
250
{
276
- $ this ->items = static ::normalize ($ items );
277
-
278
- $ this ->uniqueIdentifier ();
251
+ $ this ->items = self ::normalize ($ items );
279
252
}
280
253
281
254
/**
282
255
* DotArray Destructor.
283
256
*/
284
257
public function __destruct ()
285
258
{
286
- unset($ this ->uniqueIdentifier );
287
259
unset($ this ->items );
288
260
}
289
261
@@ -299,24 +271,6 @@ public function __invoke($key = null)
299
271
return $ this ->get ($ key );
300
272
}
301
273
302
- /**
303
- * @return string
304
- */
305
- public function uniqueIdentifier ()
306
- {
307
- if (empty ($ this ->uniqueIdentifier )) {
308
- $ this ->uniqueIdentifier = static ::segmentsToKey (
309
- [
310
- static ::class,
311
- \uniqid ('' , true ),
312
- \microtime (true ),
313
- ]
314
- );
315
- }
316
-
317
- return $ this ->uniqueIdentifier ;
318
- }
319
-
320
274
/**
321
275
* Merges one or more arrays into master recursively.
322
276
* If each array has an element with the same string key value, the latter
@@ -356,7 +310,7 @@ public function merge($array)
356
310
*/
357
311
protected function &read ($ key = null , $ default = null )
358
312
{
359
- $ segments = static ::pathToSegments ($ key );
313
+ $ segments = self ::pathToSegments ($ key );
360
314
$ items = &$ this ->items ;
361
315
362
316
foreach ($ segments as $ segment ) {
@@ -383,7 +337,7 @@ protected function &read($key = null, $default = null)
383
337
*/
384
338
protected function write ($ key , $ value )
385
339
{
386
- $ segments = static ::pathToSegments ($ key );
340
+ $ segments = self ::pathToSegments ($ key );
387
341
$ count = \count ($ segments );
388
342
$ items = &$ this ->items ;
389
343
@@ -401,13 +355,13 @@ protected function write($key, $value)
401
355
}
402
356
403
357
if (\is_array ($ value ) || $ value instanceof self) {
404
- $ value = static ::normalize ($ value );
358
+ $ value = self ::normalize ($ value );
405
359
}
406
360
407
361
$ items = $ value ;
408
362
409
363
if (!\is_array ($ this ->items )) {
410
- $ this ->items = static ::normalize ($ this ->items );
364
+ $ this ->items = self ::normalize ($ this ->items );
411
365
}
412
366
}
413
367
@@ -420,7 +374,7 @@ protected function write($key, $value)
420
374
*/
421
375
protected function remove ($ key )
422
376
{
423
- $ segments = static ::pathToSegments ($ key );
377
+ $ segments = self ::pathToSegments ($ key );
424
378
$ count = \count ($ segments );
425
379
$ items = &$ this ->items ;
426
380
@@ -449,7 +403,7 @@ protected function remove($key)
449
403
*/
450
404
public function has ($ key )
451
405
{
452
- $ identifier = $ this -> uniqueIdentifier ( );
406
+ $ identifier = \uniqid ( static ::class, true );
453
407
454
408
return ($ identifier !== $ this ->read ($ key , $ identifier ));
455
409
}
@@ -729,7 +683,7 @@ public function toJson($options = 0)
729
683
*/
730
684
public function toFlat ()
731
685
{
732
- return static ::flatten ($ this ->items );
686
+ return self ::flatten ($ this ->items );
733
687
}
734
688
735
689
}
0 commit comments