1 | <?php |
||
13 | final class Arrays |
||
14 | { |
||
15 | /** |
||
16 | * Filter an array by throwing if not an array or count not in the min/max range. |
||
17 | * |
||
18 | * The return value is the $value, as expected by the \DominionEnterprises\Filterer class. |
||
19 | * |
||
20 | * @param mixed $value the value to filter |
||
21 | * @param int $minCount the minimum allowed count in the array |
||
22 | * @param int $maxCount the maximum allowed count in the array |
||
23 | * |
||
24 | * @return the passed in value |
||
25 | * |
||
26 | * @throws \InvalidArgumentException if $minCount was not an int |
||
27 | * @throws \InvalidArgumentException if $maxCount was not an int |
||
28 | * @throws \Exception if $value is not an array |
||
29 | * @throws \Exception if $value count is less than $minCount |
||
30 | * @throws \Exception if $value count is greater than $maxCount |
||
31 | */ |
||
32 | public static function filter($value, $minCount = 1, $maxCount = PHP_INT_MAX) |
||
63 | |||
64 | /** |
||
65 | * Filter an array by throwing if $value is not in $haystack adhering to $strict. |
||
66 | * |
||
67 | * The return value is the $value, as expected by the \DominionEnterprises\Filterer class. |
||
68 | * |
||
69 | * @param mixed $value value to search for |
||
70 | * @param array $haystack array to search in |
||
71 | * @param bool $strict to compare strictly or not. @see in_array() |
||
72 | * |
||
73 | * @return the passed in value |
||
74 | * |
||
75 | * @see in_array() |
||
76 | * @throws \InvalidArgumentException if $strict was not a bool |
||
77 | * @throws \Exception if $value is not in array $haystack |
||
78 | */ |
||
79 | public static function in($value, array $haystack, $strict = true) |
||
93 | |||
94 | /** |
||
95 | * Filter an array by applying filters to each member |
||
96 | * |
||
97 | * @param array $values an array to be filtered. Use the Arrays::filter() before this method to ensure counts when |
||
98 | * you pass into Filterer |
||
99 | * @param array $filters filters with each specified the same as in @see Filterer::filter. |
||
100 | * Eg [['string', false, 2], ['uint']] |
||
101 | * |
||
102 | * @return array the filtered $values |
||
103 | * |
||
104 | * @throws \Exception if any member of $values fails filtering |
||
105 | */ |
||
106 | public static function ofScalars(array $values, array $filters) |
||
120 | |||
121 | /** |
||
122 | * Filter an array by applying filters to each member |
||
123 | * |
||
124 | * @param array $values as array to be filtered. Use the Arrays::filter() before this method to ensure counts when |
||
125 | * you pass into Filterer |
||
126 | * @param array $spec spec to apply to each $values member, specified the same as in @see Filterer::filter. |
||
127 | * Eg ['key' => ['required' => true, ['string', false], ['unit']], 'key2' => ...] |
||
128 | * |
||
129 | * @return array the filtered $values |
||
130 | * |
||
131 | * @throws \Exception if any member of $values fails filtering |
||
132 | */ |
||
133 | public static function ofArrays(array $values, array $spec) |
||
158 | |||
159 | /** |
||
160 | * Filter $value by using a Filterer $spec and Filterer's default options. |
||
161 | * |
||
162 | * @param array $value array to be filtered. Use the Arrays::filter() before this method to ensure counts when you |
||
163 | * pass into Filterer |
||
164 | * @param array $spec spec to apply to $value, specified the same as in @see Filterer::filter. |
||
165 | * Eg ['key' => ['required' => true, ['string', false], ['unit']], 'key2' => ...] |
||
166 | * |
||
167 | * @return array the filtered $value |
||
168 | * |
||
169 | * @throws \Exception if $value fails filtering |
||
170 | */ |
||
171 | public static function ofArray(array $value, array $spec) |
||
180 | |||
181 | /** |
||
182 | * Given a multi-dimensional array, flatten the array to a single level. |
||
183 | * |
||
184 | * The order of the values will be maintained, but the keys will not. |
||
185 | * |
||
186 | * For example, given the array [[1, 2], [3, [4, 5]]], this would result in the array [1, 2, 3, 4, 5]. |
||
187 | * |
||
188 | * @param array $value The array to flatten. |
||
189 | * |
||
190 | * @return array The single-dimension array. |
||
191 | */ |
||
192 | public static function flatten(array $value) |
||
205 | } |
||
206 |