Test Failed
Push — master ( 546388...b5cf4d )
by Mr
02:26
created

Arrays::searchMdArray()   B

Complexity

Conditions 5
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 7
nc 1
nop 2
dl 0
loc 11
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
namespace DrMVC\Helpers;
4
5
/**
6
 * Class Arrays
7
 * @package DrMVC\Helpers
8
 */
9
class Arrays
10
{
11
    /**
12
     * Order array by arguments
13
     *
14
     * @return  array
15
     */
16
    public static function orderBy(): array
17
    {
18
        $args = \func_get_args();
19
        $data = array_shift($args);
20
        foreach ($args as $n => $field) {
21
            if (\is_string($field)) {
22
                $tmp = [];
23
                foreach ($data as $key => $row) {
24
                    $tmp[$key] = $row[$field];
25
                }
26
                $args[$n] = $tmp;
27
            }
28
        }
29
        $args[] = &$data;
30
        array_multisort($args);
31
        return array_pop($args);
32
    }
33
34
    /**
35
     * Check if array is multidimensional
36
     *
37
     * @param   array $array
38
     * @return  bool
39
     */
40
    public static function isMulti($array): bool
41
    {
42
        // First we need know what value is array
43
        if (\is_array($array)) {
0 ignored issues
show
introduced by
The condition is_array($array) is always true.
Loading history...
44
            // And if some element is array then $array is multidimensional
45
            foreach ($array as $value) {
46
                if (\is_array($value)) {
47
                    return true;
48
                }
49
            }
50
        }
51
        return false;
52
    }
53
54
    /**
55
     * Check if two arrays is equal, with same keys
56
     *
57
     * @param   array $a
58
     * @param   array $b
59
     * @return  bool
60
     */
61
    public static function equal(array $a, array $b): bool
62
    {
63
        // Both arrays should be... arrays
64
        $is_arrays = (\is_array($a) && \is_array($b));
0 ignored issues
show
introduced by
The condition is_array($b) is always true.
Loading history...
65
        // Elements count should be equal
66
        $count_equal = (\count($a) === \count($b));
67
        // Between the keys should not be differences
68
        $difference = (array_diff($a, $b) === array_diff($b, $a));
69
70
        return ($is_arrays && $count_equal && $difference);
71
    }
72
73
    /**
74
     * Read any files and folder into some directory
75
     *
76
     * @param   string $path - Source directory with files
77
     * @return  array
78
     */
79
    public static function dirToArr($path): array
80
    {
81
        $result = [];
82
        $dirContent = scandir($path, null);
83
84
        foreach ($dirContent as $key => $value) {
85
            // Exclude system dots folders
86
            if (!\in_array($value, ['.', '..'])) {
87
                if (is_dir($path . DIRECTORY_SEPARATOR . $value)) {
88
                    $result[$value] = self::dirToArr($path . DIRECTORY_SEPARATOR . $value);
89
                } else {
90
                    $result[] = $value;
91
                }
92
            }
93
        }
94
95
        return $result;
96
    }
97
98
    /**
99
     * Make search in MD object
100
     *
101
     * @param   object $multi
102
     * @param   array $target
103
     * @return  array
104
     */
105
    private static function searchMdObject($multi, array $target): array
106
    {
107
        return array_map(
108
            function($element) use ($target) {
109
                $exist = true;
110
                foreach ($target as $skey => $svalue) {
111
                    $exist = $exist && isset($element->$skey) && ($element->$skey === $svalue);
112
                }
113
                return $exist ? $element : null;
114
            },
115
            (array) $multi
116
        );
117
    }
118
119
    /**
120
     * Make search in MD array
121
     *
122
     * @param   array $multi
123
     * @param   array $target
124
     * @return  array
125
     */
126
    private static function searchMdArray(array $multi, array $target): array
127
    {
128
        return array_map(
129
            function($element) use ($target) {
130
                $exist = true;
131
                foreach ($target as $skey => $svalue) {
132
                    $exist = $exist && isset($element[$skey]) && ($element[$skey] === $svalue);
133
                }
134
                return $exist ? $element : null;
135
            },
136
            $multi
137
        );
138
    }
139
140
    /**
141
     * Find nested array inside two-dimensional array
142
     *
143
     * @param   array|object $multi where need to make search
144
     * @param   array $target what we want to find
145
     * @param   bool $isObject if multidimensional array is object
146
     * @return  bool|array
147
     */
148
    public static function searchMd($multi, $target, $isObject = true)
149
    {
150
        if (empty($target) || empty($multi)) {
151
            return false;
152
        }
153
154
        $output = $isObject
155
            ? self::searchMdObject($multi, $target)
0 ignored issues
show
Bug introduced by
It seems like $multi can also be of type array; however, parameter $multi of DrMVC\Helpers\Arrays::searchMdObject() does only seem to accept object, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

155
            ? self::searchMdObject(/** @scrutinizer ignore-type */ $multi, $target)
Loading history...
156
            : self::searchMdArray($multi, $target);
0 ignored issues
show
Bug introduced by
It seems like $multi can also be of type object; however, parameter $multi of DrMVC\Helpers\Arrays::searchMdArray() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

156
            : self::searchMdArray(/** @scrutinizer ignore-type */ $multi, $target);
Loading history...
157
158
        $output = array_values(array_filter($output));
159
160
        // If output is not empty, false by default
161
        return !empty($output) ? $output : false;
162
    }
163
164
}
165