1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace WebServCo\Framework\Utils; |
6
|
|
|
|
7
|
|
|
final class Arrays |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @param array<mixed> $array |
11
|
|
|
* @param mixed $value |
12
|
|
|
*/ |
13
|
|
|
public static function has(?array $array, $value): bool |
14
|
|
|
{ |
15
|
|
|
if (!\is_array($array)) { |
|
|
|
|
16
|
|
|
return false; |
17
|
|
|
} |
18
|
|
|
return \in_array($value, $array, true); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Get a value from an array if it exists, otherwise a specified default value. |
23
|
|
|
* For multi dimensional arrays please see ArrayStorage. |
24
|
|
|
* |
25
|
|
|
* @param array<mixed> $array |
26
|
|
|
* @param mixed $key |
27
|
|
|
* @param mixed $defaultValue |
28
|
|
|
* @return mixed |
29
|
|
|
*/ |
30
|
|
|
public static function get(array $array, $key, $defaultValue = null) |
31
|
|
|
{ |
32
|
|
|
return \array_key_exists($key, $array) |
33
|
|
|
? $array[$key] |
34
|
|
|
: $defaultValue; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param array<mixed> $array |
39
|
|
|
*/ |
40
|
|
|
public static function isMultidimensional(array $array): bool |
41
|
|
|
{ |
42
|
|
|
if (!$array) { |
|
|
|
|
43
|
|
|
return false; |
44
|
|
|
} |
45
|
|
|
return \is_array($array[\key($array)]); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param array<mixed> $array |
50
|
|
|
* @return array<mixed> |
51
|
|
|
*/ |
52
|
|
|
public static function nullToEmptyString(array $array): array |
53
|
|
|
{ |
54
|
|
|
foreach ($array as $key => $value) { |
55
|
|
|
if (!\is_null($value)) { |
56
|
|
|
continue; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$array[$key] = ''; |
60
|
|
|
} |
61
|
|
|
return $array; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Array power set (all element combinations) |
66
|
|
|
* |
67
|
|
|
* Original function credit: |
68
|
|
|
* "PHP Cookbook by David Sklar, Adam Trachtenberg" |
69
|
|
|
* 4.24. Finding All Element Combinations of an Array |
70
|
|
|
* https://www.oreilly.com/library/view/php-cookbook/1565926811/ch04s25.html |
71
|
|
|
* |
72
|
|
|
* @param array<mixed> $array |
73
|
|
|
* @return array<mixed> |
74
|
|
|
*/ |
75
|
|
|
public static function powerSet(array $array): array |
76
|
|
|
{ |
77
|
|
|
$results = [[]]; //initialize by adding the empty set |
78
|
|
|
foreach ($array as $element) { |
79
|
|
|
foreach ($results as $combination) { |
80
|
|
|
\array_push($results, \array_merge([$element], $combination)); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
// sort by number of elements descending |
84
|
|
|
$array1 = \array_map('count', $results); |
85
|
|
|
\array_multisort($array1, \SORT_DESC, $results); |
|
|
|
|
86
|
|
|
|
87
|
|
|
return \array_filter($results); // removes empty elements |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param array<int|string,int|float|string> $array |
92
|
|
|
*/ |
93
|
|
|
public static function toUrlQueryString(array $array): ?string |
94
|
|
|
{ |
95
|
|
|
if (!$array) { |
96
|
|
|
return null; |
97
|
|
|
} |
98
|
|
|
$queries = []; |
99
|
|
|
foreach ($array as $k => $v) { |
100
|
|
|
$queries[] = \sprintf('%s=%s', $k, $v); |
101
|
|
|
} |
102
|
|
|
return '?' . \implode('&', $queries); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|