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
|
10 |
|
foreach ($array as $value) { |
79
|
10 |
|
if (call_user_func($callback, $value)) { |
80
|
6 |
|
$result[] = $value; |
81
|
6 |
|
} |
82
|
10 |
|
} |
83
|
10 |
|
} |
84
|
|
|
|
85
|
15 |
|
return $result; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Filter empty() values from array |
90
|
|
|
* |
91
|
|
|
* @param $array |
92
|
|
|
* |
93
|
|
|
* @return array |
94
|
|
|
*/ |
95
|
8 |
|
public static function filterEmpty($array) { |
96
|
8 |
|
return static::filter($array, 'Validators::isNotEmpty'); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param string $string |
101
|
|
|
* @param string $delimiter |
102
|
|
|
* |
103
|
|
|
* @return array |
104
|
|
|
*/ |
105
|
1 |
|
public static function stringToArrayFilterEmpty($string, $delimiter = ',') { |
106
|
1 |
|
return static::filterEmpty(static::stringToArray($string, $delimiter)); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param mixed|array &$arrayOld |
111
|
|
|
* @param mixed|array $arrayNew |
112
|
|
|
* @param int $mergeStrategy - default is HelperArray::MERGE_OVERWRITE |
113
|
|
|
*/ |
114
|
1 |
|
public static function merge(&$arrayOld, $arrayNew = array(), $mergeStrategy = HelperArray::MERGE_OVERWRITE) { |
115
|
1 |
|
static::makeArrayRef($arrayNew); |
116
|
1 |
|
static::makeArrayRef($arrayOld); |
117
|
|
|
|
118
|
|
|
switch ($mergeStrategy) { |
119
|
1 |
|
case HelperArray::MERGE_PHP: |
120
|
1 |
|
$arrayOld = array_merge($arrayOld, $arrayNew); |
121
|
1 |
|
break; |
122
|
|
|
|
123
|
1 |
|
default: |
124
|
1 |
|
$arrayOld = $arrayNew; |
125
|
1 |
|
break; |
126
|
1 |
|
} |
127
|
1 |
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Checks if key exists in array. If yes - return value on key otherwise return default value |
131
|
|
|
* |
132
|
|
|
* @param array &$array |
133
|
|
|
* @param mixed $key |
134
|
|
|
* @param mixed $default |
135
|
|
|
* |
136
|
|
|
* @return mixed |
137
|
|
|
*/ |
138
|
1 |
|
public static function keyExistsOr(&$array, $key, $default) { |
139
|
1 |
|
return array_key_exists($key, $array) ? $array[$key] : $default; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Clone objects in array |
144
|
|
|
* |
145
|
|
|
* @param array &$array - Any dimensional array with presumed objects in there |
146
|
|
|
* @param int $deep - HelperArray::CLONE_ARRAY_xxx constants |
147
|
|
|
*/ |
148
|
3 |
|
public static function cloneDeep(&$array, $deep = HelperArray::CLONE_ARRAY_RECURSIVE) { |
149
|
3 |
|
if ($deep == HelperArray::CLONE_ARRAY_NONE) { |
150
|
1 |
|
return; |
151
|
|
|
} |
152
|
|
|
|
153
|
2 |
|
foreach ($array as &$value) { |
154
|
2 |
|
if (is_object($value)) { |
155
|
2 |
|
$value = clone $value; |
156
|
2 |
|
} elseif (is_array($value) && $deep == HelperArray::CLONE_ARRAY_RECURSIVE) { |
157
|
1 |
|
static::cloneDeep($value, $deep); |
158
|
1 |
|
} |
159
|
2 |
|
} |
160
|
2 |
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Repacking array to provided level, removing null elements |
164
|
|
|
* |
165
|
|
|
* @param array &$array |
166
|
|
|
* @param int $level |
167
|
|
|
*/ |
168
|
|
|
public static function array_repack(&$array, $level = 0) { |
169
|
|
|
// TODO $lock_table не нужна тут |
170
|
|
|
if (!is_array($array)) { |
171
|
|
|
return; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
foreach ($array as $key => &$value) { |
175
|
|
|
if ($value === null) { |
176
|
|
|
unset($array[$key]); |
177
|
|
|
} elseif ($level > 0 && is_array($value)) { |
178
|
|
|
static::array_repack($value, $level - 1); |
179
|
|
|
if (empty($value)) { |
180
|
|
|
unset($array[$key]); |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
} |
187
|
|
|
|