1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TraderInteractive\Filter; |
4
|
|
|
|
5
|
|
|
use TraderInteractive\Filterer; |
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
|
|
|
* The return value is the $value, as expected by the \TraderInteractive\Filterer class. |
16
|
|
|
* |
17
|
|
|
* @param mixed $value the value to filter |
18
|
|
|
* @param int $minCount the minimum allowed count in the array |
19
|
|
|
* @param int $maxCount the maximum allowed count in the array |
20
|
|
|
* |
21
|
|
|
* @return mixed The passed in value |
22
|
|
|
* |
23
|
|
|
* @throws \InvalidArgumentException if $minCount was not an int |
24
|
|
|
* @throws \InvalidArgumentException if $maxCount was not an int |
25
|
|
|
* @throws Exception if $value is not an array |
26
|
|
|
* @throws Exception if $value count is less than $minCount |
27
|
|
|
* @throws Exception if $value count is greater than $maxCount |
28
|
|
|
*/ |
29
|
|
|
public static function filter($value, int $minCount = 1, int $maxCount = PHP_INT_MAX) |
30
|
|
|
{ |
31
|
|
|
if (!is_int($minCount)) { |
32
|
|
|
throw new \InvalidArgumentException('$minCount was not an int'); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
if (!is_int($maxCount)) { |
36
|
|
|
throw new \InvalidArgumentException('$maxCount was not an int'); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
if (!is_array($value)) { |
40
|
|
|
throw new Exception("Value '" . trim(var_export($value, true), "'") . "' is not an array"); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
//optimization for default case |
44
|
|
|
if ($minCount === 1 && empty($value)) { |
45
|
|
|
throw new Exception('$value count of 0 is less than 1'); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
$count = count($value); |
49
|
|
|
|
50
|
|
|
if ($count < $minCount) { |
51
|
|
|
throw new Exception("\$value count of {$count} is less than {$minCount}"); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
if ($count > $maxCount) { |
55
|
|
|
throw new Exception("\$value count of {$count} is greater than {$maxCount}"); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return $value; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Filter an array by throwing if $value is not in $haystack adhering to $strict. |
63
|
|
|
* |
64
|
|
|
* The return value is the $value, as expected by the \TraderInteractive\Filterer class. |
65
|
|
|
* |
66
|
|
|
* @param mixed $value value to search for |
67
|
|
|
* @param array $haystack array to search in |
68
|
|
|
* @param bool $strict to compare strictly or not. @see in_array() |
69
|
|
|
* |
70
|
|
|
* @return mixed The passed in value |
71
|
|
|
* |
72
|
|
|
* @see in_array() |
73
|
|
|
* |
74
|
|
|
* @throws Exception if $value is not in array $haystack |
75
|
|
|
*/ |
76
|
|
|
public static function in($value, array $haystack, bool $strict = true) |
77
|
|
|
{ |
78
|
|
|
if (!in_array($value, $haystack, $strict)) { |
79
|
|
|
throw new Exception( |
80
|
|
|
"Value '" . trim(var_export($value, true), "'") . "' is not in array " . var_export($haystack, true) |
81
|
|
|
); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return $value; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Given a multi-dimensional array, flatten the array to a single level. |
89
|
|
|
* |
90
|
|
|
* The order of the values will be maintained, but the keys will not. |
91
|
|
|
* |
92
|
|
|
* For example, given the array [[1, 2], [3, [4, 5]]], this would result in the array [1, 2, 3, 4, 5]. |
93
|
|
|
* |
94
|
|
|
* @param array $value The array to flatten. |
95
|
|
|
* |
96
|
|
|
* @return array The single-dimension array. |
97
|
|
|
*/ |
98
|
|
|
public static function flatten(array $value) : array |
99
|
|
|
{ |
100
|
|
|
$result = []; |
101
|
|
|
|
102
|
|
|
array_walk_recursive( |
103
|
|
|
$value, |
104
|
|
|
function ($item) use (&$result) { |
105
|
|
|
$result[] = $item; |
106
|
|
|
} |
107
|
|
|
); |
108
|
|
|
|
109
|
|
|
return $result; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|