1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TraderInteractive\Filter; |
4
|
|
|
|
5
|
|
|
use TraderInteractive\Exceptions\FilterException; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* A collection of filters for arrays. |
9
|
|
|
*/ |
10
|
|
|
final class Arrays |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Filter an array by throwing if not an array or count not in the min/max range. |
14
|
|
|
* |
15
|
|
|
* @param mixed $value The value to filter. |
16
|
|
|
* @param integer $minCount The minimum allowed count in the array. |
17
|
|
|
* @param integer $maxCount The maximum allowed count in the array. |
18
|
|
|
* |
19
|
|
|
* @return array |
20
|
|
|
* |
21
|
|
|
* @throws FilterException if $value is not an array |
22
|
|
|
* @throws FilterException if $value count is less than $minCount |
23
|
|
|
* @throws FilterException if $value count is greater than $maxCount |
24
|
|
|
*/ |
25
|
|
|
public static function filter($value, int $minCount = 1, int $maxCount = PHP_INT_MAX) : array |
26
|
|
|
{ |
27
|
|
|
if (!is_array($value)) { |
28
|
|
|
throw new FilterException("Value '" . trim(var_export($value, true), "'") . "' is not an array"); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
$count = count($value); |
32
|
|
|
if ($count < $minCount) { |
33
|
|
|
throw new FilterException("\$value count of {$count} is less than {$minCount}"); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
if ($count > $maxCount) { |
37
|
|
|
throw new FilterException("\$value count of {$count} is greater than {$maxCount}"); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
return $value; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Filter an array by throwing if $value is not in $haystack adhering to $strict. |
45
|
|
|
* |
46
|
|
|
* @param mixed $value The searched value. |
47
|
|
|
* @param array $haystack The array to be searched. |
48
|
|
|
* @param bool $strict Flag to compare strictly or not. @see in_array() |
49
|
|
|
* |
50
|
|
|
* @return mixed The passed in value |
51
|
|
|
* |
52
|
|
|
* @throws FilterException if $value is not in array $haystack |
53
|
|
|
*/ |
54
|
|
|
public static function in($value, array $haystack, bool $strict = true) |
55
|
|
|
{ |
56
|
|
|
if (!in_array($value, $haystack, $strict)) { |
57
|
|
|
throw new FilterException( |
58
|
|
|
"Value '" . trim(var_export($value, true), "'") . "' is not in array " . var_export($haystack, true) |
59
|
|
|
); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return $value; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Given a multi-dimensional array, flatten the array to a single level. |
67
|
|
|
* |
68
|
|
|
* The order of the values will be maintained, but the keys will not. |
69
|
|
|
* |
70
|
|
|
* For example, given the array [[1, 2], [3, [4, 5]]], this would result in the array [1, 2, 3, 4, 5]. |
71
|
|
|
* |
72
|
|
|
* @param array $value The array to flatten. |
73
|
|
|
* |
74
|
|
|
* @return array The single-dimension array. |
75
|
|
|
*/ |
76
|
|
|
public static function flatten(array $value) : array |
77
|
|
|
{ |
78
|
|
|
$result = []; |
79
|
|
|
|
80
|
|
|
array_walk_recursive( |
81
|
|
|
$value, |
82
|
|
|
function ($item) use (&$result) { |
83
|
|
|
$result[] = $item; |
84
|
|
|
} |
85
|
|
|
); |
86
|
|
|
|
87
|
|
|
return $result; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Converts any non-array value to a single element array. |
92
|
|
|
* |
93
|
|
|
* @param mixed $value The value to convert. |
94
|
|
|
* |
95
|
|
|
* @return array The coverted array or the original value. |
96
|
|
|
*/ |
97
|
|
|
public static function arrayize($value) : array |
98
|
|
|
{ |
99
|
|
|
if ($value === null) { |
100
|
|
|
return []; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
if (!is_array($value)) { |
104
|
|
|
return [$value]; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return $value; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Copies values from the $source array into a new array using the $keyMap for destination keys. |
113
|
|
|
* |
114
|
|
|
* @param array[] $source The arrays with values to be copied. |
115
|
|
|
* @param array $keyMap mapping of dest keys to source keys. If $keyMap is associative, the keys will be the |
116
|
|
|
* destination keys. If numeric the values will be the destination keys |
117
|
|
|
* |
118
|
|
|
* @return array |
119
|
|
|
*/ |
120
|
|
|
public static function copyEach(array $source, array $keyMap) : array |
121
|
|
|
{ |
122
|
|
|
$result = []; |
123
|
|
|
foreach ($source as $sourceArray) { |
124
|
|
|
$result[] = self::copy($sourceArray, $keyMap); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return $result; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Copies values from the $source array into a new array using the $keyMap for destination keys. |
132
|
|
|
* |
133
|
|
|
* @param array $source The array with values to be copied. |
134
|
|
|
* @param array $keyMap mapping of dest keys to source keys. If $keyMap is associative, the keys will be the |
135
|
|
|
* destination keys. If numeric the values will be the destination keys |
136
|
|
|
* |
137
|
|
|
* @return array |
138
|
|
|
*/ |
139
|
|
|
public static function copy(array $source, array $keyMap) : array |
140
|
|
|
{ |
141
|
|
|
$result = []; |
142
|
|
|
foreach ($keyMap as $destinationKey => $sourceKey) { |
143
|
|
|
if (is_int($destinationKey)) { |
144
|
|
|
$destinationKey = $sourceKey; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
if (array_key_exists($sourceKey, $source)) { |
148
|
|
|
$result[$destinationKey] = $source[$sourceKey]; |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
return $result; |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|