Passed
Pull Request — master (#4)
by Chad
02:16
created

Arrays::extract()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 7
nc 4
nop 3
dl 0
loc 15
rs 9.6111
c 0
b 0
f 0
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
     * Filter an array by throwing if not an array or count not in the min/max range.
15
     *
16
     * @param mixed   $value    The value to filter.
17
     * @param integer $minCount The minimum allowed count in the array.
18
     * @param integer $maxCount The maximum allowed count in the array.
19
     *
20
     * @return array
21
     *
22
     * @throws FilterException if $value is not an array
23
     * @throws FilterException if $value count is less than $minCount
24
     * @throws FilterException if $value count is greater than $maxCount
25
     */
26
    public static function filter($value, int $minCount = 1, int $maxCount = PHP_INT_MAX) : array
27
    {
28
        if (!is_array($value)) {
29
            throw new FilterException("Value '" . trim(var_export($value, true), "'") . "' is not an array");
30
        }
31
32
        $count = count($value);
33
        if ($count < $minCount) {
34
            throw new FilterException("\$value count of {$count} is less than {$minCount}");
35
        }
36
37
        if ($count > $maxCount) {
38
            throw new FilterException("\$value count of {$count} is greater than {$maxCount}");
39
        }
40
41
        return $value;
42
    }
43
44
    /**
45
     * Filter an array by throwing if $value is not in $haystack adhering to $strict.
46
     *
47
     * @param mixed $value    The searched value.
48
     * @param array $haystack The array to be searched.
49
     * @param bool  $strict   Flag to compare strictly or not. @see in_array()
50
     *
51
     * @return mixed The passed in value
52
     *
53
     * @throws FilterException if $value is not in array $haystack
54
     */
55
    public static function in($value, array $haystack, bool $strict = true)
56
    {
57
        if (!in_array($value, $haystack, $strict)) {
58
            throw new FilterException(
59
                "Value '" . trim(var_export($value, true), "'") . "' is not in array " . var_export($haystack, true)
60
            );
61
        }
62
63
        return $value;
64
    }
65
66
    /**
67
     * Given a multi-dimensional array, flatten the array to a single level.
68
     *
69
     * The order of the values will be maintained, but the keys will not.
70
     *
71
     * For example, given the array [[1, 2], [3, [4, 5]]], this would result in the array [1, 2, 3, 4, 5].
72
     *
73
     * @param array $value The array to flatten.
74
     *
75
     * @return array The single-dimension array.
76
     */
77
    public static function flatten(array $value) : array
78
    {
79
        $result = [];
80
81
        $callable = function ($item) use (&$result) {
82
            $result[] = $item;
83
        };
84
85
        array_walk_recursive($value, $callable);
86
        return $result;
87
    }
88
89
    /**
90
     * Converts any non-array value to a single element array.
91
     *
92
     * @param mixed $value The value to convert.
93
     *
94
     * @return array The coverted array or the original value.
95
     */
96
    public static function arrayize($value) : array
97
    {
98
        if ($value === null) {
99
            return [];
100
        }
101
102
        if (!is_array($value)) {
103
            return [$value];
104
        }
105
106
        return $value;
107
    }
108
109
    /**
110
     * Filter to extract a single element from a given array by key.
111
     *
112
     * @param array      $input    The array containing the element to extract.
113
     * @param int|string $key      The index of the element to extract.
114
     * @param bool       $required Flag to throw exception if the requested key does not exist.
115
     *
116
     * @return mixed
117
     *
118
     * @throws FilterException Thrown if element cannot be extracted from the array
119
     * @throws InvalidArgumentException Thrown if $key is not a valid array index.
120
     */
121
    public static function extract(array $input, $key, bool $required = true)
122
    {
123
        if (!is_string($key) && !is_int($key)) {
0 ignored issues
show
introduced by
The condition is_int($key) is always true.
Loading history...
124
            throw new InvalidArgumentException('$key was not a string or integer');
125
        }
126
127
        if (array_key_exists($key, $input)) {
128
            return $input[$key];
129
        }
130
131
        if ($required) {
132
            throw new FilterException("Array did not contain element at index '{$key}'");
133
        }
134
135
        return null;
136
    }
137
}
138