Failed Conditions
Pull Request — master (#1)
by Chad
02:53
created

Arrays::in()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 3
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
1
<?php
2
3
namespace TraderInteractive\Filter;
4
5
use TraderInteractive\Exceptions\FilterException;
6
use TraderInteractive\Filterer;
0 ignored issues
show
Bug introduced by
The type TraderInteractive\Filterer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

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