Completed
Push — master ( 62b301...fb68e6 )
by Lorenzo
02:11
created

array.php ➔ arrayToObject()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 10
nc 4
nop 1
dl 0
loc 17
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3 View Code Duplication
if (!function_exists('get')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
4
    /**
5
     * Get an item from an array using "dot" notation.
6
     *
7
     * @param  array $array
8
     * @param  string $key
9
     * @param  mixed $default
10
     * @return mixed
11
     */
12
    function get($array, $key, $default = null)
13
    {
14
        if (is_null($key)) {
15
            return $array;
16
        }
17
        if (isset($array[$key])) {
18
            return $array[$key];
19
        }
20
        foreach (explode('.', $key) as $segment) {
21
            if (!array_key_exists_safe($segment, $array)) {
22
                return value($default);
23
            }
24
            $array = $array[$segment];
25
        }
26
        return $array;
27
    }
28
}
29
30
if (!function_exists('head')) {
31
    /**
32
     * Get the first element of an array. Useful for method chaining.
33
     *
34
     * @param  array $array
35
     * @return mixed
36
     */
37
    function head($array)
38
    {
39
        return reset($array);
40
    }
41
}
42
43
if (!function_exists('last')) {
44
    /**
45
     * Get the last element from an array.
46
     *
47
     * @param  array $array
48
     * @return mixed
49
     */
50
    function last($array)
51
    {
52
        return end($array);
53
    }
54
}
55
56
if (!function_exists('array_has')) {
57
    /**
58
     * Check if an item exists in an array using "dot" notation.
59
     *
60
     * @param  array $array
61
     * @param  string $key
62
     * @return bool
63
     */
64
    function array_has($array, $key)
65
    {
66
        if (empty($array) || is_null($key)) {
67
            return false;
68
        }
69
        if (array_key_exists($key, $array)) {
70
            return true;
71
        }
72
        foreach (explode('.', $key) as $segment) {
73
            if (!array_key_exists_safe($segment, $array)) {
74
                return false;
75
            }
76
            $array = $array[$segment];
77
        }
78
        return true;
79
    }
80
}
81
82 View Code Duplication
if (!function_exists('array_get')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
83
    /**
84
     * Get an item from an array using "dot" notation.
85
     *
86
     * @param  array $array
87
     * @param  string $key
88
     * @param  mixed $default
89
     * @return mixed
90
     */
91
    function array_get($array, $key, $default = null)
92
    {
93
        if (is_null($key)) {
94
            return $array;
95
        }
96
        if (isset($array[$key])) {
97
            return $array[$key];
98
        }
99
        foreach (explode('.', $key) as $segment) {
100
            if (!array_key_exists_safe($segment, $array)) {
101
                return value($default);
102
            }
103
            $array = $array[$segment];
104
        }
105
        return $array;
106
    }
107
}
108
109
if (!function_exists('array_set')) {
110
    /**
111
     * Set an array item to a given value using "dot" notation.
112
     *
113
     * If no key is given to the method, the entire array will be replaced.
114
     *
115
     * @param  array $array
116
     * @param  string $key
117
     * @param  mixed $value
118
     * @return array
119
     */
120
    function array_set(&$array, $key, $value)
121
    {
122
        if (is_null($key)) {
123
            return $array = $value;
124
        }
125
        $keys = explode('.', $key);
126
        while (count($keys) > 1) {
127
            $key = array_shift($keys);
128
            // If the key doesn't exist at this depth, we will just create an empty array
129
            // to hold the next value, allowing us to create the arrays to hold final
130
            // values at the correct depth. Then we'll keep digging into the array.
131
            if (!isset($array[$key]) || !is_array($array[$key])) {
132
                $array[$key] = array();
133
            }
134
            $array =& $array[$key];
135
        }
136
        $array[array_shift($keys)] = $value;
137
        return $array;
138
    }
139
}
140
141
/**
142
 * Return an array with only integers value contained in the array passed
143
 * @param array $array
144
 * @return array
145
 **/
146
function CleanUpArrayOfInt($array)
147
{
148
    $result = array();
149
    if (isNullOrEmptyArray($array)) {
150
        return $result;
151
    }
152
    reset($array);
153
    while (list($key, $value) = each($array)) {
0 ignored issues
show
Unused Code introduced by
The assignment to $key is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
154
        if (isInteger($value)) {
155
            $result[] = $value;
156
        }
157
    }
158
    reset($array);
159
160
    return $result;
161
}
162
163
if (!function_exists('array_split_filter')) {
164
165
    /**
166
     * Returns an array with two elements.
167
     *
168
     * Iterates over each value in the array passing them to the callback function.
169
     * If the callback function returns true, the current value from array is returned in the first
170
     * element of result array. If not, it is return in the second element of result array.
171
     *
172
     * Array keys are preserved.
173
     *
174
     * @param array $array
175
     * @param callable $callback
176
     * @return array
177
     * @see https://github.com/spatie/array-functions/blob/master/src/array_functions.php
178
     */
179
    function array_split_filter(array $array, callable $callback)
180
    {
181
        $passesFilter = array_filter($array, $callback);
182
        $negatedCallback = function ($item) use ($callback) {
183
            return !$callback($item);
184
        };
185
        $doesNotPassFilter = array_filter($array, $negatedCallback);
186
        return [$passesFilter, $doesNotPassFilter];
187
    }
188
}
189
190
if (!function_exists('in_array_column')) {
191
192
    /**
193
     * Checks whether specific value exists in array of object.
194
     * For exampe, following code
195
     *  $exist = in_array_column([['id' => 1], ['id' => 2], ['id' => 3]], 3, 'id');
196
     * will produce 2
197
     * @author wapmorgan
198
     * @since 2015.05.19
199
     * @param array $haystack Source array
200
     * @param mixed $needle Needed value
201
     * @param string $column Column to perform search
202
     * @param bool $strict Should search be strict or not.
203
     * @return bool True if value exists in array, False otherwise.
204
     * @see modified from https://github.com/wapmorgan/php-functions-repository/blob/master/i/in_array_column.php
205
     */
206
    function in_array_column($haystack, $needle, $column, $strict = false)
207
    {
208
        foreach ($haystack as $k => $elem) {
209
            if ((!$strict && $elem[$column] == $needle) || ($strict && $elem[$column] === $needle)) {
210
                return true;
211
            }
212
        }
213
        return false;
214
    }
215
}
216
217
if (!function_exists('objectToArray')) {
218
219
    /**
220
     * Convert objecte to the array.
221
     *
222
     * @param $object
223
     *
224
     * @return array
225
     * @see https://github.com/ngfw/Recipe/blob/master/src/ngfw/Recipe.php
226
     */
227
    function objectToArray($object) : array
228
    {
229
        if (!is_object($object) && !is_array($object)) {
230
            return [];
231
        }
232
        if (is_object($object)) {
233
            $object = get_object_vars($object);
234
        }
235
        return array_map('objectToArray', $object);
236
    }
237
}
238
239
if (!function_exists('arrayToObject')) {
240
241
    /**
242
     * Convert array to the object.
243
     *
244
     * @param array $array PHP array
245
     *
246
     * @return stdClass (object)
247
     * @see https://github.com/ngfw/Recipe/blob/master/src/ngfw/Recipe.php
248
     */
249
    function arrayToObject(array $array = []) : \stdClass
250
    {
251
        $object = new \stdClass();
252
253
        if (!is_array($array) || count($array) < 1) {
254
            return $object;
255
        }
256
257
        foreach ($array as $name => $value) {
258
            if (is_array($value)) {
259
                $object->$name = arrayToObject($value);
260
                continue;
261
            }
262
            $object->{$name} = $value;
263
        }
264
        return $object;
265
    }
266
}
267
268
if (!function_exists('arrayToString')) {
269
270
    /**
271
     * Convert Array to string
272
     * expected output: <key1>="value1" <key2>="value2".
273
     *
274
     * @param array $array array to convert to string
275
     *
276
     * @return string
277
     * @see https://github.com/ngfw/Recipe/blob/master/src/ngfw/Recipe.php
278
     */
279
    function arrayToString(array $array = []) : string
280
    {
281
        if (isNullOrEmptyArray($array)) {
282
            return '';
283
        }
284
285
        $string = '';
286
        foreach ($array as $key => $value) {
287
            $string .= $key . '="' . $value . '" ';
288
        }
289
        return rtrim($string, ' ');
290
    }
291
}
292
293
if (!function_exists('array_key_exists_safe')) {
294
295
    /**
296
     * Check if a key exists in array
297
     * @param array $array
298
     * @param string $key
299
     * @return bool
300
     */
301
    function array_key_exists_safe(array $array, string $key) : bool
302
    {
303
        if (isNullOrEmptyArray($array) || isNullOrEmpty($key)) {
304
            return false;
305
        }
306
307
        return array_key_exists($key, $array);
308
    }
309
}
310
311
if (!function_exists('array_get_key_value_safe')) {
312
313
    /**
314
     * Retrieve a single key from an array.
315
     * If the key does not exist in the array, or array is null or empty
316
     * the default value will be returned instead.
317
     * @param array $array
318
     * @param string $key
319
     * @param string $default
320
     * @return mixed
321
     */
322
    function array_get_key_value_safe(array $array, string $key, $default = null)
323
    {
324
        if (isNullOrEmptyArray($array) || isNullOrEmpty($key) || !array_key_exists($key, $array)) {
325
            return $default;
326
        }
327
328
        return $array[$key];
329
    }
330
}
331
332
if (!function_exists('isNullOrEmptyArray')) {
333
334
    /**
335
     * Check if array is null or empty.
336
     * @param $array
337
     * @return bool
338
     */
339
    function isNullOrEmptyArray($array):bool
340
    {
341
        return $array === null || !is_array($array) || count($array) < 1;
342
    }
343
}
344
345
if (!function_exists('isNullOrEmptyArrayKey')) {
346
347
    /**
348
     * Check if an array key not exits or exists and is null or empty.
349
     * @param $array
350
     * @param string $key
351
     * @param bool $withTrim if set to true (default) check if trim()!='' too.
352
     * @return bool
353
     */
354
    function isNullOrEmptyArrayKey(array $array, string $key, bool $withTrim = true):bool
355
    {
356
        return !array_key_exists_safe($array, $key) || $array[$key] === null || isNullOrEmpty($array[$key], $withTrim);
357
    }
358
}
359
360
if (!function_exists('isNotNullOrEmptyArray')) {
361
362
    /**
363
     * Check if array is not null and not empty.
364
     * @param $array
365
     * @return bool
366
     */
367
    function isNotNullOrEmptyArray($array):bool
368
    {
369
        return !isNullOrEmptyArray($array);
370
    }
371
}
372
373
if (!function_exists('isNotNullOrEmptyArrayKey')) {
374
375
    /**
376
     * Check if an array key exists and is not null and not empty.
377
     * @param $array
378
     * @param string $key
379
     * @param bool $withTrim if set to true (default) check if trim()!='' too.
380
     * @return bool
381
     */
382
    function isNotNullOrEmptyArrayKey(array $array, string $key, bool $withTrim = true):bool
383
    {
384
        return !isNullOrEmptyArrayKey($array, $key, $withTrim);
385
    }
386
}
387