|
1
|
|
|
<?php |
|
2
|
|
|
namespace Narrowspark\Arr; |
|
3
|
|
|
|
|
4
|
|
|
use Narrowspark\Arr\Traits\SplitPathTrait; |
|
5
|
|
|
use Narrowspark\Arr\Traits\ValueTrait; |
|
6
|
|
|
|
|
7
|
|
|
class Transform |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* Dotted array cache. |
|
11
|
|
|
* |
|
12
|
|
|
* @var array |
|
13
|
|
|
*/ |
|
14
|
|
|
protected $dotted = []; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Swap two elements between positions. |
|
18
|
|
|
* |
|
19
|
|
|
* @param array $array array to swap |
|
20
|
|
|
* @param string $swapA |
|
21
|
|
|
* @param string $swapB |
|
22
|
|
|
* |
|
23
|
|
|
* @return array|null |
|
24
|
|
|
*/ |
|
25
|
|
|
public function swap(array $array, $swapA, $swapB) |
|
26
|
|
|
{ |
|
27
|
|
|
list($array[$swapA], $array[$swapB]) = [$array[$swapB], $array[$swapA]]; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Indexes an array depending on the values it contains. |
|
32
|
|
|
* |
|
33
|
|
|
* @param array $array |
|
34
|
|
|
* @param callable $cb Function to combine values. |
|
35
|
|
|
* @param boolean $overwrite Should duplicate keys be overwritten? |
|
36
|
|
|
* |
|
37
|
|
|
* @return array Indexed values. |
|
38
|
|
|
*/ |
|
39
|
|
|
public function combine(array $array, callable $cb, $overwrite = true) |
|
|
|
|
|
|
40
|
|
|
{ |
|
41
|
|
|
$combined = []; |
|
42
|
|
|
|
|
43
|
|
|
foreach ($array as $key => $value) { |
|
44
|
|
|
$combinator = call_user_func($cb, $value, $key); |
|
45
|
|
|
$index = $combinator->key(); |
|
46
|
|
|
|
|
47
|
|
|
if ($overwrite || !isset($combined[$index])) { |
|
48
|
|
|
$combined[$index] = $combinator->current(); |
|
49
|
|
|
} |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
return $combined; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Divide an array into two arrays. One with keys and the other with values. |
|
57
|
|
|
* |
|
58
|
|
|
* @param array $array |
|
59
|
|
|
* |
|
60
|
|
|
* @return array |
|
|
|
|
|
|
61
|
|
|
*/ |
|
62
|
|
|
public function divide($array) |
|
63
|
|
|
{ |
|
64
|
|
|
return [array_keys($array), array_values($array)]; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Reindexes a list of values. |
|
69
|
|
|
* |
|
70
|
|
|
* @param array $array |
|
71
|
|
|
* @param array $map An map of correspondances of the form |
|
72
|
|
|
* ['currentIndex' => 'newIndex']. |
|
73
|
|
|
* @param boole $keepUnmapped Whether or not to keep keys that are not |
|
|
|
|
|
|
74
|
|
|
* remapped. |
|
75
|
|
|
* |
|
76
|
|
|
* @return array |
|
77
|
|
|
*/ |
|
78
|
|
|
public function reindex(array $array, array $map, $keepUnmapped = true) |
|
79
|
|
|
{ |
|
80
|
|
|
$reindexed = $keepUnmapped |
|
81
|
|
|
? $array |
|
82
|
|
|
: []; |
|
83
|
|
|
|
|
84
|
|
|
foreach ($map as $from => $to) { |
|
85
|
|
|
if (isset($array[$from])) { |
|
86
|
|
|
$reindexed[$to] = $array[$from]; |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
return $reindexed; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Merges two arrays recursively. |
|
95
|
|
|
* |
|
96
|
|
|
* @param array $first Original data. |
|
97
|
|
|
* @param array $second Data to be merged. |
|
98
|
|
|
* |
|
99
|
|
|
* @return array |
|
100
|
|
|
*/ |
|
101
|
|
|
public function merge(array $first, array $second) |
|
102
|
|
|
{ |
|
103
|
|
|
foreach ($second as $key => $value) { |
|
104
|
|
|
$shouldBeMerged = ( |
|
105
|
|
|
isset($first[$key]) |
|
106
|
|
|
&& is_array($first[$key]) |
|
107
|
|
|
&& is_array($value) |
|
108
|
|
|
); |
|
109
|
|
|
|
|
110
|
|
|
$first[$key] = $shouldBeMerged |
|
111
|
|
|
? $this->merge($first[$key], $value) |
|
112
|
|
|
: $value; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
return $first; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Flatten a multi-dimensional associative array with dots. |
|
120
|
|
|
* |
|
121
|
|
|
* @param array $array |
|
122
|
|
|
* @param string $prepend |
|
123
|
|
|
* |
|
124
|
|
|
* @return array |
|
125
|
|
|
*/ |
|
126
|
|
|
public function dot($array, $prepend = '') |
|
127
|
|
|
{ |
|
128
|
|
|
$cache = serialize(['array' => $array, 'prepend' => $prepend]); |
|
129
|
|
|
|
|
130
|
|
|
if (array_key_exists($cache, $this->dotted)) { |
|
131
|
|
|
return $this->dotted[$cache]; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
$results = []; |
|
135
|
|
|
|
|
136
|
|
View Code Duplication |
foreach ($array as $key => $value) { |
|
|
|
|
|
|
137
|
|
|
if (is_array($value)) { |
|
138
|
|
|
$results = array_merge($results, $this->dot($value, $prepend . $key . '.')); |
|
139
|
|
|
} else { |
|
140
|
|
|
$results[$prepend . $key] = $value; |
|
141
|
|
|
} |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
return $this->dotted[$cache] = $results; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* Flatten a nested array to a separated key. |
|
149
|
|
|
* |
|
150
|
|
|
* @param array $array |
|
151
|
|
|
* @param string|null $separator |
|
152
|
|
|
* @param string $prepend |
|
153
|
|
|
* |
|
154
|
|
|
* @return array |
|
155
|
|
|
*/ |
|
156
|
|
|
public function flatten(array $array, $separator = null, $prepend = '') |
|
157
|
|
|
{ |
|
158
|
|
|
$flattened = []; |
|
159
|
|
|
|
|
160
|
|
View Code Duplication |
foreach ($array as $key => $value) { |
|
|
|
|
|
|
161
|
|
|
if (is_array($value)) { |
|
162
|
|
|
$flattened = array_merge($flattened, $this->flatten($value, $separator, $prepend . $key . $separator)); |
|
163
|
|
|
} else { |
|
164
|
|
|
$flattened[$prepend . $key] = $value; |
|
165
|
|
|
} |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
return $flattened; |
|
169
|
|
|
} |
|
170
|
|
|
} |
|
171
|
|
|
|
Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.