Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
33 / 33 |
|
100.00% |
13 / 13 |
CRAP | |
100.00% |
1 / 1 |
Util | |
100.00% |
33 / 33 |
|
100.00% |
13 / 13 |
25 | |
100.00% |
1 / 1 |
stringToList | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
3 | |||
cleanString | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
cleanList | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
clean | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
join | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
split | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
flatten | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
merge | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
removeEmpty | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
match | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
isInt | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
rekey | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
format | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
8 |
1 | <?php |
2 | |
3 | declare(strict_types=1); |
4 | |
5 | namespace Projom\Storage; |
6 | |
7 | class Util |
8 | { |
9 | public static function stringToList(string|array $subject, string $delimeter = ','): array |
10 | { |
11 | if (is_array($subject)) |
12 | return $subject; |
13 | |
14 | $subject = static::cleanString($subject); |
15 | |
16 | $list = [$subject]; |
17 | if (strpos($subject, $delimeter) !== false) |
18 | $list = explode($delimeter, $subject); |
19 | |
20 | return $list; |
21 | } |
22 | |
23 | public static function cleanString(string $subject): string |
24 | { |
25 | return str_replace(' ', '', trim($subject)); |
26 | } |
27 | |
28 | public static function cleanList(array $list): array |
29 | { |
30 | return array_map([self::class, 'cleanString'], $list); |
31 | } |
32 | |
33 | public static function clean(string|array $subject): array|string |
34 | { |
35 | if (is_array($subject)) |
36 | return static::cleanList($subject); |
37 | return static::cleanString($subject); |
38 | } |
39 | |
40 | public static function join(array $list, string $delimeter = ','): string |
41 | { |
42 | return implode($delimeter, $list); |
43 | } |
44 | |
45 | public static function split(string $subject, string $delimeter = ','): array |
46 | { |
47 | return explode($delimeter, $subject); |
48 | } |
49 | |
50 | public static function flatten(array $list): array |
51 | { |
52 | return array_merge(...$list); |
53 | } |
54 | |
55 | public static function merge(array ...$lists): array |
56 | { |
57 | return array_merge(...$lists); |
58 | } |
59 | |
60 | public static function removeEmpty(array $list): array |
61 | { |
62 | return array_filter($list); |
63 | } |
64 | |
65 | public static function match(string $pattern, string $subject): array |
66 | { |
67 | if (preg_match($pattern, $subject, $matches) === 1) |
68 | return $matches; |
69 | |
70 | return []; |
71 | } |
72 | |
73 | public static function isInt(int|float|string $subject): bool |
74 | { |
75 | $subject = (string) $subject; |
76 | return is_numeric($subject) && strpos($subject, '.') === false; |
77 | } |
78 | |
79 | public static function rekey(array $records, string $field): array |
80 | { |
81 | return array_column($records, null, $field); |
82 | } |
83 | |
84 | public static function format(mixed $value, string $type): mixed |
85 | { |
86 | $type = strtolower($type); |
87 | return match ($type) { |
88 | 'int' => (int) $value, |
89 | 'float' => (float) $value, |
90 | 'bool' => (bool) $value, |
91 | 'date' => date('Y-m-d', strtotime((string) $value)), |
92 | 'datetime' => date('Y-m-d H:i:s', strtotime((string) $value)), |
93 | 'string' => (string) $value, |
94 | default => $value, |
95 | }; |
96 | } |
97 | } |