|
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 extractOrderedItems(array $items, array $targets) |
|
11
|
|
|
{ |
|
12
|
|
|
$targets = array_flip($targets); |
|
13
|
|
|
|
|
14
|
|
|
return array_replace( |
|
15
|
|
|
array_intersect_key($targets, $items), |
|
16
|
|
|
array_intersect_key($items, $targets) |
|
17
|
|
|
); |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
public function prefixArrayValues(array $data, $prefix) |
|
21
|
|
|
{ |
|
22
|
|
|
return array_map( |
|
23
|
|
|
function ($value) use ($prefix) { |
|
24
|
|
|
return $prefix . $value; |
|
25
|
|
|
}, |
|
26
|
|
|
$data |
|
27
|
|
|
); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
public function extractItems(array &$data, array $keys) |
|
31
|
|
|
{ |
|
32
|
|
|
$subset = array_intersect($data, $keys); |
|
33
|
|
|
$data = array_diff($data, $keys); |
|
34
|
|
|
|
|
35
|
|
|
return $subset; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function extractValue(array $data, $key, $default = null) |
|
39
|
|
|
{ |
|
40
|
|
|
return isset($data[$key]) |
|
41
|
|
|
? $data[$key] |
|
42
|
|
|
: $default; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function removeKeysByPrefix(array $data, $prefix) |
|
46
|
|
|
{ |
|
47
|
|
|
$whitelist = array_filter( |
|
48
|
|
|
array_keys($data), |
|
49
|
|
|
function ($key) use ($prefix) { |
|
50
|
|
|
return strpos($key, $prefix) !== 0; |
|
51
|
|
|
} |
|
52
|
|
|
); |
|
53
|
|
|
|
|
54
|
|
|
return array_intersect_key( |
|
55
|
|
|
$data, |
|
56
|
|
|
array_flip($whitelist) |
|
57
|
|
|
); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function walkArrayNodes(array $list, \Closure $callback) |
|
61
|
|
|
{ |
|
62
|
|
|
$list = $callback($list); |
|
63
|
|
|
|
|
64
|
|
|
foreach ($list as $key => $value) { |
|
65
|
|
|
if (!is_array($value)) { |
|
66
|
|
|
continue; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
$list[$key] = $this->walkArrayNodes($value, $callback); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
return $list; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
public function getNodeReferencesByPaths(array &$data, array $paths) |
|
76
|
|
|
{ |
|
77
|
|
|
$stack = array(); |
|
78
|
|
|
|
|
79
|
|
|
foreach ($paths as $path) { |
|
80
|
|
|
$stack[] = array(array(&$data), explode('/', $path)); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
$result = array(); |
|
84
|
|
|
|
|
85
|
|
|
while ($item = array_shift($stack)) { |
|
86
|
|
|
$segment = array_shift($item[1]); |
|
87
|
|
|
|
|
88
|
|
|
foreach ($item[0] as &$node) { |
|
89
|
|
|
if ($segment === null) { |
|
90
|
|
|
$result[] = &$node; |
|
91
|
|
|
} elseif ($segment === '*') { |
|
92
|
|
|
$stack[] = array(&$node, $item[1]); |
|
93
|
|
|
} elseif (isset($node[$segment])) { |
|
94
|
|
|
$stack[] = array(array(&$node[$segment]), $item[1]); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
unset($node); |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
return $result; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
public function getValueByPath(array $data, $path, $default = null) |
|
105
|
|
|
{ |
|
106
|
|
|
if (!is_array($path)) { |
|
107
|
|
|
$path = explode('/', $path); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
foreach ($path as $key) { |
|
111
|
|
|
if (is_array($data) && array_key_exists($key, $data)) { |
|
112
|
|
|
$data = $data[$key]; |
|
113
|
|
|
|
|
114
|
|
|
continue; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
return $default; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
return $data; |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|