1
|
|
|
<?php
|
2
|
|
|
/**
|
3
|
|
|
* @author Sergey Glagolev <[email protected]>, Alexey Tatarinov <[email protected]>
|
4
|
|
|
* @link https://github.com/shogodev/argilla/
|
5
|
|
|
* @copyright Copyright © 2003-2014 Shogo
|
6
|
|
|
* @license http://argilla.ru/LICENSE
|
7
|
|
|
* @package frontend.share.helpers
|
8
|
|
|
*/
|
9
|
|
|
class Arr
|
10
|
|
|
{
|
11
|
|
|
/**
|
12
|
|
|
* @param mixed $array
|
13
|
|
|
* @param mixed $key
|
14
|
|
|
* @param mixed $default
|
15
|
|
|
* @param mixed $strict_default_check
|
16
|
|
|
*
|
17
|
|
|
* @return mixed
|
18
|
|
|
*/
|
19
|
109 |
|
public static function get($array, $key, $default = null, $strict_default_check = false)
|
20
|
|
|
{
|
21
|
|
|
if( $strict_default_check )
|
22
|
109 |
|
{
|
23
|
1 |
|
return !empty($array[$key]) ? $array[$key] : $default;
|
24
|
|
|
}
|
25
|
|
|
else
|
26
|
|
|
{
|
27
|
109 |
|
return isset($array[$key]) ? $array[$key] : $default;
|
28
|
|
|
}
|
29
|
|
|
}
|
30
|
|
|
|
31
|
|
|
/**
|
32
|
|
|
* Вырезает ключ из массива
|
33
|
|
|
* @static
|
34
|
|
|
*
|
35
|
|
|
* @param $array
|
36
|
|
|
* @param $key
|
37
|
|
|
* @param null $default
|
38
|
|
|
* @param bool $strict_default_check
|
39
|
|
|
*
|
40
|
|
|
* @return null
|
41
|
|
|
*/
|
42
|
18 |
|
public static function cut(&$array, $key, $default = null, $strict_default_check = false)
|
43
|
|
|
{
|
44
|
|
|
if( $strict_default_check )
|
45
|
18 |
|
{
|
46
|
1 |
|
$value = !empty($array[$key]) ? $array[$key] : $default;
|
47
|
1 |
|
}
|
48
|
|
|
else
|
49
|
|
|
{
|
50
|
18 |
|
$value = isset($array[$key]) ? $array[$key] : $default;
|
51
|
|
|
}
|
52
|
|
|
|
53
|
18 |
|
if( isset($array[$key]) )
|
54
|
18 |
|
unset($array[$key]);
|
55
|
|
|
|
56
|
18 |
|
return $value;
|
57
|
|
|
}
|
58
|
|
|
|
59
|
|
|
/**
|
60
|
|
|
* Retrieves multiple keys from an array. If the key does not exist in the
|
61
|
|
|
* array, the default value will be added instead.
|
62
|
|
|
* Get the values "username", "password" from $_POST
|
63
|
|
|
* $auth = Arr::extract($_POST, array('username', 'password'));
|
64
|
|
|
* $param = Arr::extract($_POST, $param);
|
65
|
|
|
*
|
66
|
|
|
* @param array array to extract keys from
|
67
|
|
|
* @param mixed key or list of key names
|
68
|
|
|
* @param mixed default value
|
69
|
|
|
*
|
70
|
|
|
* @return mixed
|
71
|
|
|
*/
|
72
|
2 |
|
public static function extract($array, $keys, $default = null)
|
73
|
|
|
{
|
74
|
2 |
|
$found = array();
|
75
|
|
|
|
76
|
2 |
|
if( is_array($keys) )
|
77
|
2 |
|
{
|
78
|
2 |
|
foreach($keys as $key)
|
79
|
|
|
{
|
80
|
2 |
|
$found[$key] = isset($array[$key]) ? $array[$key] : $default;
|
81
|
2 |
|
}
|
82
|
2 |
|
}
|
83
|
|
|
else
|
84
|
|
|
{
|
85
|
1 |
|
$found = isset($array[$keys]) ? $array[$keys] : $default;
|
86
|
|
|
}
|
87
|
|
|
|
88
|
2 |
|
return $found;
|
89
|
|
|
}
|
90
|
|
|
|
91
|
|
|
/**
|
92
|
|
|
* Возвращает первый элемент массива
|
93
|
|
|
*
|
94
|
|
|
* @param mixed $array
|
95
|
|
|
*
|
96
|
|
|
* @return mixed $first_value
|
97
|
|
|
*/
|
98
|
34 |
|
public static function reset(array $array)
|
99
|
|
|
{
|
100
|
34 |
|
$item = reset($array);
|
101
|
|
|
|
102
|
34 |
|
return $item;
|
103
|
|
|
}
|
104
|
|
|
|
105
|
|
|
/**
|
106
|
|
|
* Возвращает последний элемент массива
|
107
|
|
|
*
|
108
|
|
|
* @param mixed $array
|
109
|
|
|
*
|
110
|
|
|
* @return mixed $last_value
|
111
|
|
|
*/
|
112
|
13 |
|
public static function end(array $array)
|
113
|
|
|
{
|
114
|
13 |
|
$item = end($array);
|
115
|
|
|
|
116
|
13 |
|
return $item;
|
117
|
|
|
}
|
118
|
|
|
|
119
|
|
|
/**
|
120
|
|
|
* Проверка массива на наличие ключей
|
121
|
|
|
*
|
122
|
|
|
* @param array $search
|
123
|
|
|
* @param mixed $keys
|
124
|
|
|
*
|
125
|
|
|
* @return bool
|
126
|
|
|
*/
|
127
|
1 |
|
public static function keysExists(array $search, $keys)
|
128
|
|
|
{
|
129
|
1 |
|
$result = true;
|
130
|
|
|
|
131
|
1 |
|
if( is_array($keys) )
|
132
|
1 |
|
{
|
133
|
1 |
|
foreach($keys as $value)
|
134
|
|
|
{
|
135
|
1 |
|
if( !array_key_exists($value, $search) )
|
136
|
1 |
|
{
|
137
|
1 |
|
return false;
|
138
|
|
|
}
|
139
|
1 |
|
}
|
140
|
1 |
|
}
|
141
|
|
|
else
|
142
|
|
|
{
|
143
|
1 |
|
$result = array_key_exists($keys, $search);
|
144
|
|
|
}
|
145
|
|
|
|
146
|
1 |
|
return $result;
|
147
|
|
|
}
|
148
|
|
|
|
149
|
|
|
/**
|
150
|
|
|
* Преобразует объект данных в ассоциативный массив
|
151
|
|
|
*
|
152
|
|
|
* @param $obj
|
153
|
|
|
*
|
154
|
|
|
* @return array
|
155
|
|
|
*/
|
156
|
1 |
|
public static function fromObj($obj)
|
157
|
|
|
{
|
158
|
1 |
|
if( is_object($obj) )
|
159
|
1 |
|
{
|
160
|
1 |
|
return get_object_vars($obj);
|
161
|
|
|
}
|
162
|
1 |
|
else if( is_array($obj) )
|
163
|
1 |
|
{
|
164
|
1 |
|
foreach($obj as $key => $value)
|
165
|
|
|
{
|
166
|
1 |
|
$obj[$key] = call_user_func(__METHOD__, $value);
|
167
|
1 |
|
}
|
168
|
|
|
|
169
|
1 |
|
return $obj;
|
170
|
|
|
}
|
171
|
|
|
else
|
172
|
|
|
{
|
173
|
1 |
|
return $obj;
|
174
|
|
|
}
|
175
|
|
|
}
|
176
|
|
|
|
177
|
|
|
/**
|
178
|
|
|
* Проверим, пересекаются ли два массива
|
179
|
|
|
*
|
180
|
|
|
* @param mixed $arr1
|
181
|
|
|
* @param mixed $arr2
|
182
|
|
|
*
|
183
|
|
|
* @return mixed
|
184
|
|
|
*/
|
185
|
1 |
|
public static function isIntersec(array $arr1 = array(), array $arr2 = array())
|
186
|
|
|
{
|
187
|
1 |
|
$intersec = array_intersect($arr1, $arr2);
|
188
|
|
|
|
189
|
1 |
|
return !empty($intersec);
|
190
|
|
|
}
|
191
|
|
|
|
192
|
|
|
/**
|
193
|
|
|
* Выполняет trim над всеми элементами массива
|
194
|
|
|
*
|
195
|
|
|
* @param array $array
|
196
|
|
|
* @param string $charlist
|
197
|
|
|
*
|
198
|
|
|
* @return array $array
|
199
|
|
|
*/
|
200
|
8 |
|
public static function trim($array, $charlist = '')
|
201
|
|
|
{
|
202
|
8 |
|
if( is_array($array) )
|
203
|
8 |
|
{
|
204
|
8 |
|
foreach($array as $key => $value)
|
205
|
|
|
{
|
206
|
7 |
|
if( !empty($charlist) )
|
207
|
7 |
|
{
|
208
|
1 |
|
$array[$key] = self::trim($value, $charlist);
|
209
|
1 |
|
}
|
210
|
|
|
else
|
211
|
|
|
{
|
212
|
7 |
|
$array[$key] = self::trim($value);
|
213
|
|
|
}
|
214
|
8 |
|
}
|
215
|
8 |
|
}
|
216
|
7 |
|
elseif( is_string($array) )
|
217
|
|
|
{
|
218
|
7 |
|
if( !empty($charlist) )
|
219
|
7 |
|
{
|
220
|
1 |
|
$array = trim($array, $charlist);
|
221
|
1 |
|
}
|
222
|
|
|
else
|
223
|
|
|
{
|
224
|
7 |
|
$array = trim($array);
|
225
|
|
|
}
|
226
|
7 |
|
}
|
227
|
|
|
|
228
|
8 |
|
return $array;
|
229
|
|
|
}
|
230
|
|
|
|
231
|
|
|
/**
|
232
|
|
|
* @static
|
233
|
|
|
*
|
234
|
|
|
* @param $glue
|
235
|
|
|
* @param array $array
|
236
|
|
|
*
|
237
|
|
|
* @return mixed
|
238
|
|
|
*/
|
239
|
1 |
|
public static function implode(array $array, $glue)
|
240
|
|
|
{
|
241
|
1 |
|
foreach($array as $key => $value)
|
242
|
|
|
{
|
243
|
1 |
|
if( empty($value) )
|
244
|
1 |
|
{
|
245
|
1 |
|
unset($array[$key]);
|
246
|
1 |
|
}
|
247
|
1 |
|
}
|
248
|
|
|
|
249
|
1 |
|
return preg_replace("/\s+/", " ", implode($glue, $array));
|
250
|
|
|
}
|
251
|
|
|
|
252
|
16 |
|
public static function reflect($array)
|
253
|
|
|
{
|
254
|
16 |
|
return array_combine($array, $array);
|
255
|
|
|
}
|
256
|
|
|
|
257
|
|
|
/**
|
258
|
|
|
* @param mixed $data
|
259
|
|
|
*
|
260
|
|
|
* @return mixed
|
261
|
|
|
*/
|
262
|
19 |
|
public static function reduce($data)
|
263
|
|
|
{
|
264
|
19 |
|
if( is_array($data) )
|
265
|
19 |
|
{
|
266
|
18 |
|
return self::reset($data);
|
267
|
|
|
}
|
268
|
|
|
else
|
269
|
|
|
{
|
270
|
2 |
|
return $data;
|
271
|
|
|
}
|
272
|
|
|
}
|
273
|
|
|
|
274
|
|
|
/**
|
275
|
|
|
* Объединение ассоциативных массивов.
|
276
|
|
|
* В отличие от CMap::mergeArray перекрывает целочисленные ключи, а не увеличивает индекс элементов
|
277
|
|
|
*
|
278
|
|
|
* @param array $a
|
279
|
|
|
* @param array $b
|
280
|
|
|
*
|
281
|
|
|
* @return array
|
282
|
|
|
*/
|
283
|
2 |
|
public static function mergeAssoc(array $a, array $b)
|
|
|
|
|
284
|
|
|
{
|
285
|
2 |
|
$args = func_get_args();
|
286
|
2 |
|
$res = array_shift($args);
|
287
|
|
|
|
288
|
2 |
|
while(!empty($args))
|
289
|
|
|
{
|
290
|
2 |
|
$next = array_shift($args);
|
291
|
2 |
|
foreach($next as $k => $v)
|
292
|
|
|
{
|
293
|
2 |
|
if( is_array($v) && isset($res[$k]) && is_array($res[$k]) )
|
294
|
2 |
|
$res[$k] = self::mergeAssoc($res[$k], $v);
|
295
|
|
|
else
|
296
|
2 |
|
$res[$k] = $v;
|
297
|
2 |
|
}
|
298
|
2 |
|
}
|
299
|
|
|
|
300
|
2 |
|
return $res;
|
301
|
|
|
}
|
302
|
|
|
|
303
|
|
|
/**
|
304
|
|
|
* Делит массив на $countOfParts колонок
|
305
|
|
|
* @param array $array
|
306
|
|
|
* @param int $countOfParts
|
307
|
|
|
* @param bool $resetIndex - не использовать ассоциативные ключи, по умолчанию true
|
308
|
|
|
* @param bool $flipSort - сортировка элементов по горизонтали, по умолчанию false
|
309
|
|
|
*
|
310
|
|
|
* @return array
|
311
|
|
|
*/
|
312
|
7 |
|
public static function divide(array $array, $countOfParts = 2, $resetIndex = true, $flipSort = false)
|
313
|
|
|
{
|
314
|
7 |
|
if( !count($array) )
|
315
|
7 |
|
return array();
|
316
|
|
|
|
317
|
6 |
|
$matrix = self::createMatrix($countOfParts, $array);
|
318
|
6 |
|
$outArray = self::createArrayByMatrix($array, $matrix, $resetIndex, $flipSort);
|
319
|
|
|
|
320
|
6 |
|
return $outArray;
|
321
|
|
|
}
|
322
|
|
|
|
323
|
|
|
/**
|
324
|
|
|
* @param $array
|
325
|
|
|
* @param $itemKey
|
326
|
|
|
* @param $item
|
327
|
|
|
*/
|
328
|
1 |
|
public static function push(&$array, $itemKey, $item)
|
329
|
|
|
{
|
330
|
1 |
|
if( !isset($array[$itemKey]) )
|
331
|
1 |
|
{
|
332
|
1 |
|
$array[$itemKey] = array();
|
333
|
1 |
|
}
|
334
|
|
|
|
335
|
1 |
|
$array[$itemKey][] = $item;
|
336
|
1 |
|
}
|
337
|
|
|
|
338
|
|
|
/**
|
339
|
|
|
* @param $array
|
340
|
|
|
* @param $after
|
341
|
|
|
* @param $item
|
342
|
|
|
* @param $itemKey
|
343
|
|
|
*/
|
344
|
1 |
|
public static function insertAfter(&$array, $itemKey, $item, $after)
|
345
|
|
|
{
|
346
|
1 |
|
$counter = 1;
|
347
|
1 |
|
foreach($array as $key => $value)
|
348
|
|
|
{
|
349
|
1 |
|
if( $key === $after )
|
350
|
1 |
|
{
|
351
|
1 |
|
break;
|
352
|
|
|
}
|
353
|
|
|
|
354
|
1 |
|
$counter++;
|
355
|
1 |
|
}
|
356
|
|
|
|
357
|
1 |
|
$array_head = array_slice($array, 0, $counter, true);
|
358
|
1 |
|
$array_tail = array_slice($array, $counter, null, true);
|
359
|
1 |
|
$array = self::mergeAssoc($array_head, array($itemKey => $item));
|
360
|
1 |
|
$array = self::mergeAssoc($array, $array_tail);
|
361
|
1 |
|
}
|
362
|
|
|
|
363
|
|
|
/**
|
364
|
|
|
* @param $array - массив элементы которого нужно отфильтровать
|
365
|
|
|
* @param string|array $keys - ключ или массив ключей элементов $array которые нужно сравнить с $value
|
366
|
|
|
* @param $value
|
367
|
|
|
* @param $condition - OR или AND условие сравнения нескольких ключей
|
368
|
|
|
*
|
369
|
|
|
* @return array
|
370
|
|
|
*/
|
371
|
1 |
|
public static function filter($array, $keys, $value, $condition = 'OR')
|
372
|
|
|
{
|
373
|
1 |
|
$keys = !is_array($keys) ? array($keys) : $keys;
|
374
|
1 |
|
$condition = strtolower($condition);
|
375
|
|
|
|
376
|
|
|
return array_filter($array, function ($element) use ($keys, $value, $condition)
|
377
|
|
|
{
|
378
|
1 |
|
foreach($keys as $key)
|
379
|
|
|
{
|
380
|
1 |
|
if( !isset($element[$key]) )
|
381
|
1 |
|
continue;
|
382
|
|
|
|
383
|
1 |
|
$result = $element[$key] === $value ? true : false;
|
384
|
|
|
|
385
|
1 |
|
if( $condition == 'or' && $result )
|
386
|
1 |
|
return true;
|
387
|
|
|
|
388
|
|
|
if( $condition == 'and' && !$result )
|
389
|
|
|
return false;
|
390
|
|
|
}
|
391
|
|
|
|
392
|
|
|
return false;
|
393
|
1 |
|
});
|
394
|
|
|
}
|
395
|
|
|
|
396
|
|
|
/**
|
397
|
|
|
* @param array $array
|
398
|
|
|
*
|
399
|
|
|
* @return array
|
400
|
|
|
*/
|
401
|
|
|
public static function flatten(array $array)
|
402
|
|
|
{
|
403
|
|
|
$result = array();
|
404
|
|
|
array_walk_recursive($array, function ($value, $key) use (&$result)
|
405
|
|
|
{
|
406
|
|
|
$result[$key] = $value;
|
407
|
|
|
});
|
408
|
|
|
|
409
|
|
|
return $result;
|
410
|
|
|
}
|
411
|
|
|
|
412
|
|
|
/**
|
413
|
|
|
* Следующий после $current элемент массива
|
414
|
|
|
*
|
415
|
|
|
* @param $array
|
416
|
|
|
* @param $current
|
417
|
|
|
* @param bool $cycle - по кругу
|
418
|
|
|
*
|
419
|
|
|
* @return mixed|null
|
420
|
|
|
*/
|
421
|
|
|
public static function next($array, $current, $cycle = false)
|
422
|
|
|
{
|
423
|
|
|
if( empty($array) || count($array) == 1 )
|
424
|
|
|
return null;
|
425
|
|
|
|
426
|
|
|
$element = reset($array);
|
427
|
|
|
|
428
|
|
|
while( $element )
|
429
|
|
|
{
|
430
|
|
|
if( Utils::compareObjects($element, $current) )
|
431
|
|
|
{
|
432
|
|
|
$next = next($array);
|
433
|
|
|
return $cycle && !$next ? reset($array) : $next;
|
434
|
|
|
}
|
435
|
|
|
|
436
|
|
|
$element = next($array);
|
437
|
|
|
}
|
438
|
|
|
|
439
|
|
|
return null;
|
440
|
|
|
}
|
441
|
|
|
|
442
|
|
|
/**
|
443
|
|
|
* Предыдущий перед $current элемент массива
|
444
|
|
|
* @param $array
|
445
|
|
|
* @param $current
|
446
|
|
|
* @param bool $cycle - по кругу
|
447
|
|
|
*
|
448
|
|
|
* @return mixed|null
|
449
|
|
|
*/
|
450
|
|
|
public static function prev($array, $current, $cycle = false)
|
451
|
|
|
{
|
452
|
|
|
$revers = array_reverse($array);
|
453
|
|
|
|
454
|
|
|
return self::next($revers, $current, $cycle);
|
455
|
|
|
}
|
456
|
|
|
|
457
|
6 |
|
private static function createMatrix($countColumns, $array)
|
458
|
|
|
{
|
459
|
6 |
|
$matrix = array();
|
460
|
6 |
|
$countElements = count($array);
|
461
|
6 |
|
$rowsCount = ceil($countElements / $countColumns);
|
462
|
|
|
|
463
|
6 |
|
for($rowIndex = 0; $rowIndex < $rowsCount; $rowIndex++)
|
464
|
|
|
{
|
465
|
6 |
|
for($columnIndex = 0; $columnIndex < $countColumns; $columnIndex++)
|
466
|
|
|
{
|
467
|
6 |
|
$value = array_shift($array) ? 1 : 0;
|
468
|
6 |
|
$matrix[$columnIndex][$rowIndex] = $value;
|
469
|
6 |
|
}
|
470
|
6 |
|
}
|
471
|
|
|
|
472
|
6 |
|
return $matrix;
|
473
|
|
|
}
|
474
|
|
|
|
475
|
6 |
|
private static function createArrayByMatrix($array, $matrix, $resetKeys = true, $flipSort = false)
|
476
|
|
|
{
|
477
|
6 |
|
if( empty($matrix) || empty($array) )
|
478
|
6 |
|
return array();
|
479
|
|
|
|
480
|
6 |
|
$countColumns = count($matrix);
|
481
|
6 |
|
$rowsCount = count($matrix[0]);
|
482
|
|
|
|
483
|
6 |
|
$outArray = array();
|
484
|
|
|
|
485
|
6 |
|
$arrayProcess = function($columnIndex, $rowIndex) use($matrix, $resetKeys, &$outArray, &$array) {
|
486
|
6 |
|
if( $matrix[$columnIndex][$rowIndex] == 1)
|
487
|
6 |
|
{
|
488
|
6 |
|
reset($array);
|
489
|
6 |
|
$key = key($array);
|
490
|
6 |
|
$value = array_shift($array);
|
491
|
6 |
|
$outArray[$columnIndex][$resetKeys ? $rowIndex : $key] = $value;
|
492
|
6 |
|
}
|
493
|
6 |
|
};
|
494
|
|
|
|
495
|
6 |
|
if( !$flipSort )
|
496
|
6 |
|
{
|
497
|
5 |
View Code Duplication |
for($columnIndex = 0; $columnIndex < $countColumns; $columnIndex++)
|
|
|
|
|
498
|
|
|
{
|
499
|
5 |
|
for($rowIndex = 0; $rowIndex < $rowsCount; $rowIndex++)
|
500
|
|
|
{
|
501
|
5 |
|
$arrayProcess($columnIndex, $rowIndex);
|
502
|
5 |
|
}
|
503
|
5 |
|
}
|
504
|
5 |
|
}
|
505
|
|
|
else
|
506
|
|
|
{
|
507
|
1 |
View Code Duplication |
for($rowIndex = 0; $rowIndex < $rowsCount; $rowIndex++)
|
|
|
|
|
508
|
|
|
{
|
509
|
1 |
|
for($columnIndex = 0; $columnIndex < $countColumns; $columnIndex++)
|
510
|
|
|
{
|
511
|
1 |
|
$arrayProcess($columnIndex, $rowIndex);
|
512
|
1 |
|
}
|
513
|
1 |
|
}
|
514
|
|
|
}
|
515
|
|
|
|
516
|
6 |
|
return $outArray;
|
517
|
|
|
}
|
518
|
|
|
|
519
|
|
|
/**
|
520
|
|
|
* Возвращает случайный элемент из массива $elements
|
521
|
|
|
* @param array $elements
|
522
|
|
|
*
|
523
|
|
|
* @return mixed
|
524
|
|
|
*/
|
525
|
|
|
public static function random(array $elements)
|
526
|
|
|
{
|
527
|
|
|
shuffle($elements);
|
528
|
|
|
return current($elements);
|
529
|
|
|
}
|
530
|
|
|
} |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.