Checkers::isStructureSame()   B
last analyzed

Complexity

Conditions 6
Paths 4

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 8.8571
c 0
b 0
f 0
cc 6
eloc 8
nc 4
nop 2
1
<?php
2
3
namespace SimpleArrayLibrary\Categories;
4
5
use InvalidArgumentException;
6
7
trait Checkers
8
{
9
    /**
10
     * Checks if all elements of the array have same value
11
     *
12
     * @param array $haystack
13
     * @param mixed $needle
14
     *
15
     * @return boolean
16
     */
17
    public static function allElementsEqual(array $haystack, $needle = null)
18
    {
19
        $return = true;
20
        // if both arguments have been passed, use value argument (regardless of whether it is null or not
21
        if (func_num_args() == 2) {
22
            $compareAgainst = $needle;
23
        } // only one argument has been passed, so compare elements only to each other
24
        else {
25
            $compareAgainst = reset($haystack);
26
        }
27
        foreach ($haystack as $element) {
28
            if ($compareAgainst != $element) {
29
                $return = false;
30
                break;
31
            }
32
        }
33
34
        return $return;
35
    }
36
37
    /**
38
     * Checks whether array is associative or numeric
39
     *
40
     * @param array $array
41
     *
42
     * @return bool
43
     */
44
    public static function isAssociative(array $array)
45
    {
46
        return (bool)count(array_filter(array_keys($array), 'is_string'));
47
    }
48
49
    /**
50
     * Checks whether array is numeric
51
     *
52
     * @param array $array
53
     *
54
     * @return bool
55
     */
56
    public static function isNumeric(array $array)
57
    {
58
        return array_keys($array) == range(0, count($array) - 1);
59
    }
60
61
    /**
62
     * @param mixed $input1
63
     * @param mixed $input2
64
     *
65
     * @return bool
66
     */
67
    public static function isStructureSame($input1, $input2)
68
    {
69
        $return = true;
70
        if (is_array($input1) && is_array($input2)) {
71
            if (!self::compareArrays($input1, $input2) || !self::compareArrays($input2, $input1)) {
72
                $return = false;
73
            }
74
        } else {
75
            $return = !is_array($input1) && !is_array($input2);
76
        }
77
78
        return $return;
79
    }
80
81
    /**
82
     * @param array $input1
83
     * @param array $input2
84
     *
85
     * @return bool
86
     */
87
    private static function compareArrays(array $input1, array $input2)
88
    {
89
        foreach ($input1 as $key => $value) {
90
            if (!array_key_exists($key, $input2)) {
91
                return false;
92
            } else {
93
                if (!self::isStructureSame($value, $input2[$key])) {
94
                    return false;
95
                }
96
            }
97
        }
98
99
        return true;
100
    }
101
102
    /**
103
     * Checks whether $subArray is contained in $array
104
     *
105
     * @param array $array
106
     * @param array $subArray
107
     * @param bool  $strictComparison
108
     *
109
     * @return bool
110
     * @throws InvalidArgumentException
111
     */
112
    public static function isSubArray(array $array, array $subArray, $strictComparison = true)
113
    {
114
        if (!is_bool($strictComparison)) {
115
            throw new InvalidArgumentException('Strict comparison parameter must be a boolean');
116
        }
117
118
        $return = true;
119
        foreach ($subArray as $key => $value) {
120
            if (isset($array[$key]) || array_key_exists($key, $array)) {
121
                $check = $strictComparison ? $array[$key] !== $subArray[$key] : $array[$key] != $subArray[$key];
122
                if ($check) {
123
                    $return = false;
124
                    break;
125
                }
126
            } else {
127
                $return = false;
128
                break;
129
            }
130
        }
131
132
        return $return;
133
    }
134
}