|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
class HelperArray { |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Overwrites old array with new |
|
7
|
|
|
*/ |
|
8
|
|
|
const MERGE_OVERWRITE = 0; |
|
9
|
|
|
/** |
|
10
|
|
|
* Merges old array with new with array_merge() |
|
11
|
|
|
* String keys replaced, numeric keys renumbered |
|
12
|
|
|
*/ |
|
13
|
|
|
const MERGE_PHP = 1; |
|
14
|
|
|
|
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* No array cloning - just stay with same objects |
|
18
|
|
|
*/ |
|
19
|
|
|
const CLONE_ARRAY_NONE = 0; |
|
20
|
|
|
/** |
|
21
|
|
|
* Clone objects on first level of array |
|
22
|
|
|
*/ |
|
23
|
|
|
const CLONE_ARRAY_SHALLOW = 1; |
|
24
|
|
|
/** |
|
25
|
|
|
* Clone objects recursive on any array level |
|
26
|
|
|
*/ |
|
27
|
|
|
const CLONE_ARRAY_RECURSIVE = 2; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Convert $delimiter delimited string to array |
|
31
|
|
|
* |
|
32
|
|
|
* @param mixed $string |
|
33
|
|
|
* @param string $delimiter |
|
34
|
|
|
* |
|
35
|
|
|
* @return array |
|
36
|
|
|
*/ |
|
37
|
6 |
|
public static function stringToArray($string, $delimiter = ',') { |
|
38
|
6 |
|
return is_string($string) && !empty($string) ? explode($delimiter, $string) : array(); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Convert single value to array by reference |
|
43
|
|
|
* |
|
44
|
|
|
* @param mixed &$value |
|
45
|
|
|
*/ |
|
46
|
3 |
|
public static function makeArrayRef(&$value, $index = 0) { |
|
47
|
3 |
|
if (!is_array($value)) { |
|
48
|
2 |
|
$value = array($index => $value); |
|
49
|
2 |
|
} |
|
50
|
3 |
|
} |
|
51
|
|
|
|
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Convert single value to array |
|
55
|
|
|
* |
|
56
|
|
|
* @param mixed $value |
|
57
|
|
|
* |
|
58
|
|
|
* @return array |
|
59
|
|
|
*/ |
|
60
|
3 |
|
public static function makeArray($value, $index = 0) { |
|
61
|
3 |
|
static::makeArrayRef($value, $index); |
|
62
|
|
|
|
|
63
|
3 |
|
return $value; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Filters array by callback |
|
68
|
|
|
* |
|
69
|
|
|
* @param mixed $array |
|
70
|
|
|
* @param callable $callback |
|
71
|
|
|
* |
|
72
|
|
|
* @return array |
|
73
|
|
|
*/ |
|
74
|
15 |
|
public static function filter(&$array, $callback) { |
|
75
|
15 |
|
$result = array(); |
|
76
|
|
|
|
|
77
|
15 |
|
if (is_array($array) && !empty($array)) { |
|
78
|
|
|
// $result = array_filter($array, $callback); |
|
|
|
|
|
|
79
|
10 |
|
foreach ($array as $value) { |
|
80
|
10 |
|
if (call_user_func($callback, $value)) { |
|
81
|
6 |
|
$result[] = $value; |
|
82
|
|
|
// TODO - array_filter |
|
83
|
6 |
|
} |
|
84
|
10 |
|
} |
|
85
|
10 |
|
} |
|
86
|
|
|
|
|
87
|
15 |
|
return $result; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Filters array by callback |
|
92
|
|
|
* |
|
93
|
|
|
* @param mixed $array |
|
94
|
|
|
* @param callable $callback |
|
95
|
|
|
* @param bool $withKeys |
|
96
|
|
|
* |
|
97
|
|
|
* @return array |
|
98
|
|
|
*/ |
|
99
|
|
|
public static function map(&$array, $callback, $withKeys = false) { |
|
100
|
|
|
$result = array(); |
|
101
|
|
|
|
|
102
|
|
|
// var_dump('entering map'); |
|
|
|
|
|
|
103
|
|
|
// var_dump('$withKeys'); |
|
104
|
|
|
// var_dump($withKeys); |
|
105
|
|
|
|
|
106
|
|
|
if (is_array($array) && !empty($array)) { |
|
107
|
|
|
|
|
108
|
|
|
|
|
109
|
|
|
if ($withKeys) { |
|
110
|
|
|
$result = array_map($callback, $array, array_keys($array)); |
|
111
|
|
|
} else { |
|
112
|
|
|
$result = array_map($callback, $array); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
// $result = $withKeys |
|
|
|
|
|
|
116
|
|
|
// ? array_map($callback, $array) |
|
117
|
|
|
// : array_map($callback, $array, array_keys($array)) |
|
118
|
|
|
// ; |
|
119
|
|
|
|
|
120
|
|
|
// var_dump($array); |
|
|
|
|
|
|
121
|
|
|
// if ($withKeys) { |
|
122
|
|
|
// var_dump('$array'); |
|
123
|
|
|
// var_dump($array); |
|
124
|
|
|
//// var_dump(array_keys($array)); |
|
125
|
|
|
// die(); |
|
126
|
|
|
// } |
|
127
|
|
|
|
|
128
|
|
|
// foreach ($array as $key => $value) { |
|
|
|
|
|
|
129
|
|
|
// if (call_user_func($callback, $value)) { |
|
130
|
|
|
// $result[$key] = $value; |
|
131
|
|
|
// } |
|
132
|
|
|
// } |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
return $result; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Filter empty() values from array |
|
140
|
|
|
* |
|
141
|
|
|
* @param $array |
|
142
|
|
|
* |
|
143
|
|
|
* @return array |
|
144
|
|
|
*/ |
|
145
|
8 |
|
public static function filterEmpty($array) { |
|
146
|
8 |
|
return static::filter($array, 'Validators::isNotEmpty'); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* @param string $string |
|
151
|
|
|
* @param string $delimiter |
|
152
|
|
|
* |
|
153
|
|
|
* @return array |
|
154
|
|
|
*/ |
|
155
|
1 |
|
public static function stringToArrayFilterEmpty($string, $delimiter = ',') { |
|
156
|
1 |
|
return static::filterEmpty(static::stringToArray($string, $delimiter)); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* @param mixed|array &$arrayOld |
|
161
|
|
|
* @param mixed|array $arrayNew |
|
162
|
|
|
* @param int $mergeStrategy - default is HelperArray::MERGE_OVERWRITE |
|
163
|
|
|
*/ |
|
164
|
1 |
|
public static function merge(&$arrayOld, $arrayNew = array(), $mergeStrategy = HelperArray::MERGE_OVERWRITE) { |
|
165
|
1 |
|
static::makeArrayRef($arrayNew); |
|
166
|
1 |
|
static::makeArrayRef($arrayOld); |
|
167
|
|
|
|
|
168
|
|
|
switch ($mergeStrategy) { |
|
169
|
1 |
|
case HelperArray::MERGE_PHP: |
|
170
|
1 |
|
$arrayOld = array_merge($arrayOld, $arrayNew); |
|
171
|
1 |
|
break; |
|
172
|
|
|
|
|
173
|
1 |
|
default: |
|
174
|
1 |
|
$arrayOld = $arrayNew; |
|
175
|
1 |
|
break; |
|
176
|
1 |
|
} |
|
177
|
1 |
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* Checks if key exists in array. If yes - return value on key otherwise return default value |
|
181
|
|
|
* |
|
182
|
|
|
* @param array &$array |
|
183
|
|
|
* @param mixed $key |
|
184
|
|
|
* @param mixed $default |
|
185
|
|
|
* |
|
186
|
|
|
* @return mixed |
|
187
|
|
|
*/ |
|
188
|
1 |
|
public static function keyExistsOr(&$array, $key, $default) { |
|
189
|
1 |
|
return array_key_exists($key, $array) ? $array[$key] : $default; |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* Clone objects in array |
|
194
|
|
|
* |
|
195
|
|
|
* @param array &$array - Any dimensional array with presumed objects in there |
|
196
|
|
|
* @param int $deep - HelperArray::CLONE_ARRAY_xxx constants |
|
197
|
|
|
*/ |
|
198
|
3 |
|
public static function cloneDeep(&$array, $deep = HelperArray::CLONE_ARRAY_RECURSIVE) { |
|
199
|
3 |
|
if ($deep == HelperArray::CLONE_ARRAY_NONE) { |
|
200
|
1 |
|
return; |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
2 |
|
foreach ($array as &$value) { |
|
204
|
2 |
|
if (is_object($value)) { |
|
205
|
2 |
|
$value = clone $value; |
|
206
|
2 |
|
} elseif (is_array($value) && $deep == HelperArray::CLONE_ARRAY_RECURSIVE) { |
|
207
|
1 |
|
static::cloneDeep($value, $deep); |
|
208
|
1 |
|
} |
|
209
|
2 |
|
} |
|
210
|
2 |
|
} |
|
211
|
|
|
|
|
212
|
|
|
/** |
|
213
|
|
|
* Repacking array to provided level, removing null elements |
|
214
|
|
|
* |
|
215
|
|
|
* @param array &$array |
|
216
|
|
|
* @param int $level |
|
217
|
|
|
*/ |
|
218
|
|
|
public static function array_repack(&$array, $level = 0) { |
|
219
|
|
|
// TODO $lock_table не нужна тут |
|
220
|
|
|
if (!is_array($array)) { |
|
221
|
|
|
return; |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
foreach ($array as $key => &$value) { |
|
225
|
|
|
if ($value === null) { |
|
226
|
|
|
unset($array[$key]); |
|
227
|
|
|
} elseif ($level > 0 && is_array($value)) { |
|
228
|
|
|
static::array_repack($value, $level - 1); |
|
229
|
|
|
if (empty($value)) { |
|
230
|
|
|
unset($array[$key]); |
|
231
|
|
|
} |
|
232
|
|
|
} |
|
233
|
|
|
} |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
} |
|
237
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.