1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* User: alec |
4
|
|
|
* Date: 12.10.18 |
5
|
|
|
* Time: 15:53 |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
if (!function_exists('formatted_array')) { |
9
|
|
|
/** |
10
|
|
|
* @param array $data |
11
|
|
|
* @param int|null $columns |
12
|
|
|
* @param callable|null $callback |
13
|
|
|
* @param int $pad |
14
|
|
|
* @return iterable |
15
|
|
|
*/ |
16
|
|
|
function formatted_array(array $data, int $columns = 10, ?callable $callback = null, int $pad = STR_PAD_RIGHT): iterable |
17
|
|
|
{ |
18
|
1 |
|
$result = []; |
19
|
1 |
|
if ($callback) { |
20
|
1 |
|
\array_walk($data, $callback); |
21
|
|
|
} |
22
|
1 |
|
$maxLength = arr_el_max_length($data); |
23
|
|
|
|
24
|
1 |
|
while ($element = \array_shift($data)) { |
25
|
1 |
|
$tmp[] = \str_pad($element, $maxLength, ' ', $pad); |
26
|
1 |
|
if (\count($tmp) >= $columns) { |
27
|
1 |
|
$result[] = \implode(' ', $tmp); |
28
|
1 |
|
$tmp = []; |
29
|
|
|
} |
30
|
|
|
} |
31
|
1 |
|
if (!empty($tmp)) { |
32
|
1 |
|
$result[] = \implode(' ', $tmp); |
33
|
|
|
} |
34
|
1 |
|
return $result; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
if (!function_exists('arr_el_max_length')) { |
40
|
|
|
/** |
41
|
|
|
* @param array $data |
42
|
|
|
* @return int |
43
|
|
|
*/ |
44
|
|
|
function arr_el_max_length(array $data): int |
45
|
|
|
{ |
46
|
10 |
|
$maxLength = 0; |
47
|
10 |
|
foreach ($data as $value) { |
48
|
10 |
|
$len = \strlen((string)$value); |
49
|
10 |
|
$maxLength = $maxLength < $len ? $len : $maxLength; |
50
|
|
|
} |
51
|
10 |
|
return $maxLength; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
if (!function_exists('unset_first')) { |
57
|
|
|
function unset_first(array $data): iterable |
58
|
|
|
{ |
59
|
9 |
|
$key = array_key_first($data); |
60
|
9 |
|
unset($data[$key]); |
61
|
9 |
|
return $data; |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
if (!function_exists('array_key_first')) { |
66
|
|
|
/** |
67
|
|
|
* @param array $data |
68
|
|
|
* @return int|null|string |
69
|
|
|
*/ |
70
|
|
|
function array_key_first(array $data) |
71
|
|
|
{ |
72
|
12 |
|
reset($data); |
73
|
12 |
|
return key($data); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
if (!function_exists('array_key_last')) { |
78
|
|
|
/** |
79
|
|
|
* @param array $data |
80
|
|
|
* @return int|null|string |
81
|
|
|
*/ |
82
|
|
|
function array_key_last(array $data) |
83
|
|
|
{ |
84
|
3 |
|
end($data); |
85
|
3 |
|
return key($data); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|