1 | <?php |
||||
2 | |||||
3 | /** |
||||
4 | * @file ArrayHelper.php |
||||
5 | * @brief This file contains the ArrayHelper class. |
||||
6 | * @details |
||||
7 | * @author Filippo F. Fadda |
||||
8 | */ |
||||
9 | |||||
10 | |||||
11 | namespace ToolBag\Helper; |
||||
12 | |||||
13 | |||||
14 | use ToolBag\Exception\JSONErrorException; |
||||
15 | |||||
16 | |||||
17 | /** |
||||
18 | * @brief Helper with common array methods. |
||||
19 | * @nosubgrouping |
||||
20 | */ |
||||
21 | class ArrayHelper { |
||||
22 | |||||
23 | |||||
24 | /** |
||||
25 | * @brief Checks if the array is associative. |
||||
26 | * @param array $array The array. |
||||
27 | * @return bool |
||||
28 | */ |
||||
29 | public static function isAssociative(array $array) { |
||||
30 | return (0 !== count(array_diff_key($array, array_keys(array_keys($array)))) || count($array) == 0); |
||||
31 | } |
||||
32 | |||||
33 | |||||
34 | /** |
||||
35 | * @brief Converts the array to an object. |
||||
36 | * @param array $array The array to be converted. |
||||
37 | * @return object |
||||
38 | */ |
||||
39 | public static function toObject(array $array) { |
||||
40 | return is_array($array) ? (object)array_map(__METHOD__, $array) : $array; |
||||
0 ignored issues
–
show
introduced
by
![]() |
|||||
41 | } |
||||
42 | |||||
43 | |||||
44 | /** |
||||
45 | * @brief Converts the given JSON into an array. |
||||
46 | * @param string $json A JSON object. |
||||
47 | * @param bool $assoc When `true`, returned objects will be converted into associative arrays. |
||||
48 | * @return array |
||||
49 | */ |
||||
50 | public static function fromJson($json, $assoc) { |
||||
51 | $data = json_decode((string)$json, $assoc); |
||||
52 | |||||
53 | if (is_null($data)) |
||||
54 | switch (json_last_error()) { |
||||
55 | case JSON_ERROR_DEPTH: |
||||
56 | throw new JSONErrorException("Unable to parse the given JSON, the maximum stack depth has been exceeded."); |
||||
57 | break; |
||||
58 | case JSON_ERROR_STATE_MISMATCH: |
||||
59 | throw new JSONErrorException("Unable to parse the given JSON, invalid or malformed JSON."); |
||||
60 | break; |
||||
61 | case JSON_ERROR_CTRL_CHAR: |
||||
62 | throw new JSONErrorException("Unable to parse the given JSON, control character error, possibly incorrectly encoded."); |
||||
63 | break; |
||||
64 | case JSON_ERROR_SYNTAX: |
||||
65 | throw new JSONErrorException("Unable to parse the given JSON, syntax error."); |
||||
66 | break; |
||||
67 | case JSON_ERROR_UTF8: |
||||
68 | throw new JSONErrorException("Unable to parse the given JSON, malformed UTF-8 characters, possibly incorrectly encoded."); |
||||
69 | break; |
||||
70 | } |
||||
71 | |||||
72 | return $data; |
||||
73 | } |
||||
74 | |||||
75 | |||||
76 | /** |
||||
77 | * @brief Returns a portion of the array. |
||||
78 | * @param array $array The original array. |
||||
79 | * @param int $number (optional) The number of elements from left to right. |
||||
80 | * @return array |
||||
81 | */ |
||||
82 | public static function slice(array $array, $number = NULL) { |
||||
83 | return array_slice($array, 0, $number, TRUE); |
||||
84 | } |
||||
85 | |||||
86 | |||||
87 | /** |
||||
88 | * @brief Given a key, returns its related value. |
||||
89 | * @param mixed $key A key. |
||||
90 | * @param array $array The array to be searched. |
||||
91 | * @return mixed The value or `false` in case the value doesn't exist. |
||||
92 | */ |
||||
93 | public static function value($key, array $array) { |
||||
94 | |||||
95 | if (array_key_exists($key, $array)) |
||||
96 | return $array[$key]; |
||||
97 | else |
||||
98 | return FALSE; |
||||
99 | } |
||||
100 | |||||
101 | |||||
102 | /** |
||||
103 | * @brief Given a key, returns it only if exists otherwise return `false`. |
||||
104 | * @param mixed $key A key. |
||||
105 | * @param array $array The array to be searched. |
||||
106 | * @return mixed The key or `false` in case the key doesn't exist. |
||||
107 | */ |
||||
108 | public static function key($key, array $array) { |
||||
109 | |||||
110 | if (array_key_exists($key, $array)) |
||||
111 | return $key; |
||||
112 | else |
||||
113 | return FALSE; |
||||
114 | } |
||||
115 | |||||
116 | |||||
117 | /** |
||||
118 | * @brief Modifies the specified array, depriving each ID of its related version. |
||||
119 | * @param[in,out] array $ids An array of IDs. |
||||
120 | */ |
||||
121 | public static function unversion(array &$ids) { |
||||
122 | array_walk($ids, function(&$value, $key) { |
||||
0 ignored issues
–
show
The parameter
$key is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. ![]() |
|||||
123 | $value = strtok($value, TextHelper::SEPARATOR); |
||||
124 | } |
||||
125 | ); |
||||
126 | } |
||||
127 | |||||
128 | |||||
129 | /** |
||||
130 | * @brief Merge the two given arrays. |
||||
131 | * @details The returned array doesn't contain duplicate values. |
||||
132 | * @param array $array1 The first array. |
||||
133 | * @param array $array2 The first array. |
||||
134 | * @return array |
||||
135 | */ |
||||
136 | public static function merge(array $array1, array $array2) { |
||||
137 | $array = array_merge($array1, $array2); |
||||
138 | return array_keys(array_flip($array)); |
||||
139 | } |
||||
140 | |||||
141 | |||||
142 | /** |
||||
143 | * @brief Like `array_unique()`, removes duplicate values, but works on multidimensional arrays. |
||||
144 | * @details The returned array doesn't contain duplicate values for the specified key. |
||||
145 | * @param array $array The original array. |
||||
146 | * @param array $key The key that will be used as filter. |
||||
147 | * @return array |
||||
148 | */ |
||||
149 | public static function multidimensionalUnique(array $array, $key) { |
||||
150 | $i = 0; |
||||
151 | |||||
152 | // The resulting array. |
||||
153 | $uniqueArray = []; |
||||
154 | |||||
155 | // An array with the used keys. |
||||
156 | $keys = []; |
||||
157 | |||||
158 | foreach($array as $value) { |
||||
159 | |||||
160 | if (!in_array($value[$key], $keys)) { |
||||
161 | $keys[$i] = $value[$key]; |
||||
162 | $uniqueArray[$i] = $value; |
||||
163 | } |
||||
164 | |||||
165 | $i++; |
||||
166 | } |
||||
167 | |||||
168 | return $uniqueArray; |
||||
169 | } |
||||
170 | |||||
171 | } |