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
|
|
|
|