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 ArrayHelper |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @param $firstArray |
17
|
|
|
* @param $secondArray |
18
|
|
|
* @return bool |
19
|
|
|
*/ |
20
|
|
|
public static function checkIfTwoArraysAreConsistent($firstArray, $secondArray) |
21
|
|
|
{ |
22
|
|
|
if (count(array_diff($firstArray, $secondArray)) > 0 || count(array_diff($secondArray, $firstArray)) > 0) { |
23
|
|
|
return false; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
return true; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param $itemToCompare |
31
|
|
|
* @param $element |
32
|
|
|
* @return bool |
33
|
|
|
*/ |
34
|
|
|
public static function compareElementToItemKeyMap($itemToCompare, $element) |
35
|
|
|
{ |
36
|
|
|
foreach ($itemToCompare as $key => $item){ |
37
|
|
|
if(is_array($item) && ArrayHelper::isAnAssociativeArray($item)){ |
38
|
|
|
if (false === ArrayHelper::checkIfTwoArraysAreConsistent(array_keys($item), array_keys($element[$key]))) { |
39
|
|
|
return false; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
return self::compareElementToItemKeyMap($item, $element[$key]); |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param $arrayElement |
49
|
|
|
* @return array |
50
|
|
|
*/ |
51
|
|
|
public static function convertObjectToArray($arrayElement) |
52
|
|
|
{ |
53
|
|
|
$convertedArray = []; |
54
|
|
|
|
55
|
|
|
foreach (self::convertToPlainArray($arrayElement) as $key => $element) { |
56
|
|
|
$key = explode("\\", $key); |
57
|
|
|
$key = end($key); |
58
|
|
|
$key = explode("\000", $key); |
59
|
|
|
|
60
|
|
|
$convertedArray[end($key)] = $element; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return $convertedArray; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param $array |
68
|
|
|
* @return mixed |
69
|
|
|
*/ |
70
|
|
|
public static function convertToObjectArray($array) |
71
|
|
|
{ |
72
|
|
|
return json_decode(json_encode($array)); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param $array |
77
|
|
|
* @return mixed |
78
|
|
|
*/ |
79
|
|
|
public static function convertToPlainArray($array) |
80
|
|
|
{ |
81
|
|
|
return json_decode(json_encode($array), true); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param $key |
86
|
|
|
* @param $array |
87
|
|
|
* @return mixed |
88
|
|
|
*/ |
89
|
|
|
public static function getValueFromNestedArray(array $key, array $array) |
90
|
|
|
{ |
91
|
|
|
array_shift($key); |
92
|
|
|
$value = $array[$key[0]]; |
93
|
|
|
|
94
|
|
|
if(is_array($value)){ |
95
|
|
|
return self::getValueFromNestedArray($key, $value); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return $value; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param array $array |
103
|
|
|
* @return bool |
104
|
|
|
*/ |
105
|
|
|
public static function isAnAssociativeArray(array $array) |
106
|
|
|
{ |
107
|
|
|
if ([] === $array) return false; |
108
|
|
|
|
109
|
|
|
return array_keys($array) !== range(0, count($array) - 1); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|