Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit af13b74

Browse files
[*]: Minor refactoring in code [+]: More tests
1 parent d832137 commit af13b74

File tree

2 files changed

+126
-104
lines changed

2 files changed

+126
-104
lines changed

‎src/DotArray.php

Lines changed: 57 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -21,46 +21,10 @@ class DotArray implements
2121
/* Traits. */
2222
use DotFilteringTrait;
2323

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 = ['\'', '\"', '\]', '\)', '\}'];
6428

6529
/**
6630
* Stores the original data.
@@ -92,23 +56,34 @@ public static function createFromJson($json)
9256
}
9357

9458
/**
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}
9673
*
9774
* @return string
9875
*/
99-
protected static function dotPathPattern()
76+
protected static function pathPattern()
10077
{
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+
);
11287
}
11388

11489
/**
@@ -118,7 +93,7 @@ protected static function dotPathPattern()
11893
*
11994
* @return array
12095
*/
121-
protected static function pathToSegments($path)
96+
finalprotected static function pathToSegments($path)
12297
{
12398
$path = \trim($path, "\t\n\r0円\x0B\.");
12499
$segments = [];
@@ -128,7 +103,7 @@ protected static function pathToSegments($path)
128103
return [];
129104
}
130105

131-
\preg_match_all(static::dotPathPattern(), $path, $matches);
106+
\preg_match_all(self::pathPattern(), $path, $matches);
132107

133108
if (!empty($matches[1])) {
134109
$matches = $matches[1];
@@ -153,28 +128,28 @@ function ($match) {
153128
*
154129
* @return string
155130
*/
156-
protected static function wrapSegmentKey($key)
131+
finalprotected static function wrapSegmentKey($key)
157132
{
158-
return vsprintf(static::$dotPathConfig['wrapKey'], [$key]);
133+
return vsprintf(self::WRAP_KEY, [$key]);
159134
}
160135

161136
/**
162137
* @param array $segments
163138
*
164139
* @return string
165140
*/
166-
protected static function segmentsToKey(array $segments)
141+
finalprotected static function segmentsToKey(array $segments)
167142
{
168143
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+
)
176152
)
177-
)
178153
);
179154
}
180155

@@ -187,15 +162,15 @@ function ($segment) {
187162
*
188163
* @return array
189164
*/
190-
protected static function flatten(array $items, $prepend = [])
165+
finalprotected static function flatten(array $items, $prepend = [])
191166
{
192167
$flatten = [];
193168

194169
foreach ($items as $key => $value) {
195170
if (\is_array($value) && !empty($value)) {
196171
$flatten = array_merge(
197172
$flatten,
198-
static::flatten(
173+
self::flatten(
199174
$value,
200175
array_merge($prepend, [$key])
201176
)
@@ -204,7 +179,7 @@ protected static function flatten(array $items, $prepend = [])
204179
continue;
205180
}
206181

207-
$segmentsToKey = static::segmentsToKey(array_merge($prepend, [$key]));
182+
$segmentsToKey = self::segmentsToKey(array_merge($prepend, [$key]));
208183

209184
$flatten[$segmentsToKey] = $value;
210185
}
@@ -219,7 +194,7 @@ protected static function flatten(array $items, $prepend = [])
219194
*
220195
* @return array
221196
*/
222-
protected static function normalize($items)
197+
finalprotected static function normalize($items)
223198
{
224199
if ($items instanceof self) {
225200
$items = $items->toArray();
@@ -228,7 +203,7 @@ protected static function normalize($items)
228203
if (\is_array($items)) {
229204
foreach ($items as $k => $v) {
230205
if (\is_array($v) || $v instanceof self) {
231-
$v = static::normalize($v);
206+
$v = self::normalize($v);
232207
}
233208
$items[$k] = $v;
234209
}
@@ -243,9 +218,9 @@ protected static function normalize($items)
243218
*
244219
* @return array
245220
*/
246-
protected static function mergeRecursive($array1, $array2 = null)
221+
finalprotected static function mergeRecursive($array1, $array2 = null)
247222
{
248-
$args = static::normalize(\func_get_args());
223+
$args = self::normalize(\func_get_args());
249224
$res = \array_shift($args);
250225

251226
while (!empty($args)) {
@@ -256,7 +231,7 @@ protected static function mergeRecursive($array1, $array2 = null)
256231
}
257232

258233
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);
260235
}
261236

262237
$res[$k] = $v;
@@ -273,17 +248,14 @@ protected static function mergeRecursive($array1, $array2 = null)
273248
*/
274249
public function __construct($items = [])
275250
{
276-
$this->items = static::normalize($items);
277-
278-
$this->uniqueIdentifier();
251+
$this->items = self::normalize($items);
279252
}
280253

281254
/**
282255
* DotArray Destructor.
283256
*/
284257
public function __destruct()
285258
{
286-
unset($this->uniqueIdentifier);
287259
unset($this->items);
288260
}
289261

@@ -299,24 +271,6 @@ public function __invoke($key = null)
299271
return $this->get($key);
300272
}
301273

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-
320274
/**
321275
* Merges one or more arrays into master recursively.
322276
* If each array has an element with the same string key value, the latter
@@ -356,7 +310,7 @@ public function merge($array)
356310
*/
357311
protected function &read($key = null, $default = null)
358312
{
359-
$segments = static::pathToSegments($key);
313+
$segments = self::pathToSegments($key);
360314
$items = &$this->items;
361315

362316
foreach ($segments as $segment) {
@@ -383,7 +337,7 @@ protected function &read($key = null, $default = null)
383337
*/
384338
protected function write($key, $value)
385339
{
386-
$segments = static::pathToSegments($key);
340+
$segments = self::pathToSegments($key);
387341
$count = \count($segments);
388342
$items = &$this->items;
389343

@@ -401,13 +355,13 @@ protected function write($key, $value)
401355
}
402356

403357
if (\is_array($value) || $value instanceof self) {
404-
$value = static::normalize($value);
358+
$value = self::normalize($value);
405359
}
406360

407361
$items = $value;
408362

409363
if (!\is_array($this->items)) {
410-
$this->items = static::normalize($this->items);
364+
$this->items = self::normalize($this->items);
411365
}
412366
}
413367

@@ -420,7 +374,7 @@ protected function write($key, $value)
420374
*/
421375
protected function remove($key)
422376
{
423-
$segments = static::pathToSegments($key);
377+
$segments = self::pathToSegments($key);
424378
$count = \count($segments);
425379
$items = &$this->items;
426380

@@ -449,7 +403,7 @@ protected function remove($key)
449403
*/
450404
public function has($key)
451405
{
452-
$identifier = $this->uniqueIdentifier();
406+
$identifier = \uniqid(static::class, true);
453407

454408
return ($identifier !== $this->read($key, $identifier));
455409
}
@@ -729,7 +683,7 @@ public function toJson($options = 0)
729683
*/
730684
public function toFlat()
731685
{
732-
return static::flatten($this->items);
686+
return self::flatten($this->items);
733687
}
734688

735689
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /