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