Completed
Push — master ( 9ba778...870b98 )
by Mauro
01:54
created

ConsistencyChecker::isValid()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
rs 9.6666
cc 3
eloc 4
nc 3
nop 1
1
<?php
2
/**
3
 * This file is part of the ArrayQuery package.
4
 *
5
 * (c) Mauro Cassani<https://github.com/mauretto78>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace ArrayQuery\Helpers;
12
13
class ConsistencyChecker
14
{
15
    /**
16
     * @param $element
17
     * @param $array
18
     * @return bool
19
     */
20
    public static function isElementValid($element, $array)
21
    {
22
        foreach ($array as $key => $item) {
23
            if (is_object($item)) {
24
                $array[$key] = ArrayConverter::convertToPlainArray($item);
25
            }
26
        }
27
28
        if (is_object($element)) {
29
            $element = ArrayConverter::convertToPlainArray($element);
30
        }
31
32
        $FirstItemKeyMap = array_keys(current($array));
33
        $ItemKeyMap = array_keys($element);
34
35
        if (count(array_diff($FirstItemKeyMap, $ItemKeyMap)) > 0 || count(array_diff($ItemKeyMap, $FirstItemKeyMap)) > 0) {
36
            return false;
37
        }
38
39
        return true;
40
    }
41
42
    /**
43
     * @param $array
44
     * @return bool
45
     */
46
    public static function isValid($array)
47
    {
48
        foreach ($array as $element) {
49
            if (false === self::isElementValid($element, $array)) {
50
                return false;
51
            }
52
        }
53
54
        return true;
55
    }
56
}
57