-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDotPathTrait.php
More file actions
175 lines (145 loc) · 3.87 KB
/
Copy pathDotPathTrait.php
File metadata and controls
175 lines (145 loc) · 3.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<?php
namespace BinaryCube\DotArray;
/**
* DotPathTrait
*
* @package BinaryCube\DotArray
* @author Banciu N. Cristian Mihai <banciu.n.cristian.mihai@gmail.com>
* @license https://github.com/binary-cube/dot-array/blob/master/LICENSE <MIT License>
* @link https://github.com/binary-cube/dot-array
*/
trait DotPathTrait
{
/**
* Internal Dot Path Config.
*
* @var array
*/
protected static $dotPathConfig = [
'template' => '#(?|(?|[<token-start>](.*?)[<token-end>])|(.*?))(?:$|\.+)#i',
'wrapKey' => '{{%s}}',
'wildcards' => [
'<token-start>' => ['\'', '\"', '\[', '\(', '\{'],
'<token-end>' => ['\'', '\"', '\]', '\)', '\}'],
],
];
/**
* The cached pattern that allow to match the JSON paths that use the dot notation.
*
* Allowed tokens for more complex paths: '', "", [], (), {}
* Examples:
*
* - foo.bar
* - foo.'bar'
* - foo."bar"
* - foo.[bar]
* - foo.(bar)
* - foo.{bar}
*
* Or more complex:
* - foo.{bar}.[component].{version.1.0}
*
* @var string
*/
protected static $dotPathPattern;
/**
* Getting the dot path pattern.
*
* @return string
*/
protected static function dotPathPattern()
{
if (empty(self::$dotPathPattern)) {
$path = self::$dotPathConfig['template'];
foreach (self::$dotPathConfig['wildcards'] as $wildcard => $tokens) {
$path = \str_replace($wildcard, \implode('', $tokens), $path);
}
self::$dotPathPattern = $path;
}
return self::$dotPathPattern;
}
/**
* Converts dot string path to segments.
*
* @param string $path
*
* @return array
*/
protected static function pathToSegments($path)
{
$path = \trim($path, " \t\n\r\0\x0B\.");
$segments = [];
$matches = [];
if (\mb_strlen($path, 'UTF-8') === 0) {
return [];
}
\preg_match_all(static::dotPathPattern(), $path, $matches);
if (!empty($matches[1])) {
$matches = $matches[1];
$segments = \array_filter(
$matches,
function ($match) {
return (\mb_strlen($match, 'UTF-8') > 0);
}
);
}
unset($path, $matches);
return $segments;
}
/**
* Wrap a given string into special characters.
*
* @param string $key
*
* @return string
*/
protected static function wrapSegmentKey($key)
{
return vsprintf(static::$dotPathConfig['wrapKey'], [$key]);
}
/**
* @param array $segments
*
* @return string
*/
protected static function segmentsToKey(array $segments)
{
return (
\implode(
'',
\array_map(
[static::class, 'wrapSegmentKey'],
$segments
)
)
);
}
/**
* Flatten the internal array using the dot delimiter,
* also the keys are wrapped inside {{key}} (2 x curly braces).
*
* @param array $items
* @param string $prepend
*
* @return array
*/
protected static function flatten(array $items, $prepend = '')
{
$flatten = [];
foreach ($items as $key => $value) {
$wrapKey = static::wrapSegmentKey($key);
if (\is_array($value) && !empty($value)) {
$flatten = array_merge(
$flatten,
static::flatten(
$value,
($prepend . $wrapKey . '.')
)
);
continue;
}
$flatten[$prepend . $wrapKey] = $value;
}
return $flatten;
}
}