1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright © Vaimo Group. All rights reserved. |
4
|
|
|
* See LICENSE_VAIMO.txt for license details. |
5
|
|
|
*/ |
6
|
|
|
namespace Vaimo\ComposerPatches\Utils; |
7
|
|
|
|
8
|
|
|
class DataUtils |
9
|
|
|
{ |
10
|
|
|
public function listToGroups(array $items, $keyMatcher) |
11
|
|
|
{ |
12
|
|
|
$result = array(); |
13
|
|
|
|
14
|
|
|
$key = ''; |
15
|
|
|
|
16
|
|
|
foreach ($items as $item) { |
17
|
|
|
$matches = array(); |
18
|
|
|
if (preg_match($keyMatcher, $item, $matches)) { |
19
|
|
|
$key = isset($matches['match']) |
20
|
|
|
? $matches['match'] |
21
|
|
|
: $matches[1]; |
22
|
|
|
|
23
|
|
|
continue; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
if (!isset($result[$key])) { |
27
|
|
|
$result[$key] = array(); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
$result[$key][] = $item; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
return $result; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function embedGroupKeyToItems(array $groups, $template = '%s:%s') |
37
|
|
|
{ |
38
|
|
|
$result = array(); |
39
|
|
|
|
40
|
|
|
foreach ($groups as $key => $items) { |
41
|
|
|
$result[$key] = $this->prefixArrayValues($items, $key, $template); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
return $result; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function extractOrderedItems(array $items, array $targets) |
48
|
|
|
{ |
49
|
|
|
$targets = array_flip($targets); |
50
|
|
|
|
51
|
|
|
return array_replace( |
52
|
|
|
array_intersect_key($targets, $items), |
53
|
|
|
array_intersect_key($items, $targets) |
54
|
|
|
); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function prefixArrayValues(array $data, $prefix, $template = '%s%s') |
58
|
|
|
{ |
59
|
|
|
return array_map( |
60
|
|
|
function ($value) use ($prefix, $template) { |
61
|
|
|
return sprintf($template, $prefix, $value); |
62
|
|
|
}, |
63
|
|
|
$data |
64
|
|
|
); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function extractItems(array &$data, array $keys) |
68
|
|
|
{ |
69
|
|
|
$subset = array_intersect($data, $keys); |
70
|
|
|
$data = array_diff($data, $keys); |
71
|
|
|
|
72
|
|
|
return $subset; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function extractValue(array $data, $key, $default = null) |
76
|
|
|
{ |
77
|
|
|
return isset($data[$key]) |
78
|
|
|
? $data[$key] |
79
|
|
|
: $default; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function removeKeysByPrefix(array $data, $prefix) |
83
|
|
|
{ |
84
|
|
|
$whitelist = array_filter( |
85
|
|
|
array_keys($data), |
86
|
|
|
function ($key) use ($prefix) { |
87
|
|
|
return strpos($key, $prefix) !== 0; |
88
|
|
|
} |
89
|
|
|
); |
90
|
|
|
|
91
|
|
|
return array_intersect_key( |
92
|
|
|
$data, |
93
|
|
|
array_flip($whitelist) |
94
|
|
|
); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function walkArrayNodes(array $list, \Closure $callback) |
98
|
|
|
{ |
99
|
|
|
$list = $callback($list); |
100
|
|
|
|
101
|
|
|
foreach ($list as $key => $value) { |
102
|
|
|
if (!is_array($value)) { |
103
|
|
|
continue; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
$list[$key] = $this->walkArrayNodes($value, $callback); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return $list; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function getNodeReferencesByPaths(array &$data, array $paths) |
113
|
|
|
{ |
114
|
|
|
$stack = array(); |
115
|
|
|
|
116
|
|
|
foreach ($paths as $path) { |
117
|
|
|
$stack[] = array(array(&$data), explode('/', $path)); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
$result = array(); |
121
|
|
|
|
122
|
|
|
while ($item = array_shift($stack)) { |
123
|
|
|
$segment = array_shift($item[1]); |
124
|
|
|
|
125
|
|
|
foreach ($item[0] as &$node) { |
126
|
|
|
if ($segment === null) { |
127
|
|
|
$result[] = &$node; |
128
|
|
|
} elseif ($segment === '*') { |
129
|
|
|
$stack[] = array(&$node, $item[1]); |
130
|
|
|
} elseif (isset($node[$segment])) { |
131
|
|
|
$stack[] = array(array(&$node[$segment]), $item[1]); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
unset($node); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
return $result; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
public function getValueByPath(array $data, $path, $default = null) |
142
|
|
|
{ |
143
|
|
|
if (!is_array($path)) { |
144
|
|
|
$path = explode('/', $path); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
foreach ($path as $key) { |
148
|
|
|
if (is_array($data) && array_key_exists($key, $data)) { |
149
|
|
|
$data = $data[$key]; |
150
|
|
|
|
151
|
|
|
continue; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
return $default; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
return $data; |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|