Completed
Push — master ( 952630...288481 )
by Lorenzo
02:11
created

array.php ➔ array_get_key_value_safe()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 4
nc 2
nop 3
dl 0
loc 8
rs 9.2
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 mixed
247
     * @see https://github.com/ngfw/Recipe/blob/master/src/ngfw/Recipe.php
248
     */
249
    function arrayToObject($array)
250
    {
251
        if (isNullOrEmptyArray($array)) {
252
            return $array;
253
        }
254
255
        $object = new \stdClass();
256
        foreach ($array as $name => $value) {
257
            $object->$name = arrayToObject($value);
258
        }
259
        return $object;
260
    }
261
}
262
263
if (!function_exists('arrayToString')) {
264
265
    /**
266
     * Convert Array to string
267
     * expected output: <key1>="value1" <key2>="value2".
268
     *
269
     * @param array $array array to convert to string
270
     *
271
     * @return string
272
     * @see https://github.com/ngfw/Recipe/blob/master/src/ngfw/Recipe.php
273
     */
274
    function arrayToString(array $array = []) : string
275
    {
276
        if (isNullOrEmptyArray($array)) {
277
            return '';
278
        }
279
280
        $string = '';
281
        foreach ($array as $key => $value) {
282
            $string .= $key . '="' . $value . '" ';
283
        }
284
        return rtrim($string, ' ');
285
    }
286
}
287
288
if (!function_exists('array_key_exists_safe')) {
289
290
    /**
291
     * Check if a key exists in array
292
     * @param array $array
293
     * @param string $key
294
     * @return bool
295
     */
296
    function array_key_exists_safe(array $array, string $key) : bool
297
    {
298
        if (isNullOrEmptyArray($array) || isNullOrEmpty($key)) {
299
            return false;
300
        }
301
302
        return array_key_exists($key, $array);
303
    }
304
}
305
306
if (!function_exists('array_get_key_value_safe')) {
307
308
    /**
309
     * Retrieve a single key from an array.
310
     * If the key does not exist in the array, or array is null or empty
311
     * the default value will be returned instead.
312
     * @param array $array
313
     * @param string $key
314
     * @param string $default
315
     * @return mixed
316
     */
317
    function array_get_key_value_safe(array $array, string $key, $default = null)
318
    {
319
        if (isNullOrEmptyArray($array) || isNullOrEmpty($key) || !array_key_exists($key, $array)) {
320
            return $default;
321
        }
322
323
        return $array[$key];
324
    }
325
}
326
327
if (!function_exists('isNullOrEmptyArray')) {
328
329
    /**
330
     * Check if array is null or empty.
331
     * @param $array
332
     * @return bool
333
     */
334
    function isNullOrEmptyArray($array):bool
335
    {
336
        return $array === null || !is_array($array) || count($array) < 1;
337
    }
338
}
339
340
if (!function_exists('isNullOrEmptyArrayKey')) {
341
342
    /**
343
     * Check if an array key not exits or exists and is null or empty.
344
     * @param $array
345
     * @param string $key
346
     * @param bool $withTrim if set to true (default) check if trim()!='' too.
347
     * @return bool
348
     */
349
    function isNullOrEmptyArrayKey(array $array, string $key, bool $withTrim = true):bool
350
    {
351
        return !array_key_exists_safe($array, $key) || $array[$key]===null || isNullOrEmpty($array[$key], $withTrim);
352
    }
353
}
354
355
if (!function_exists('isNotNullOrEmptyArray')) {
356
357
    /**
358
     * Check if array is not null and not empty.
359
     * @param $array
360
     * @return bool
361
     */
362
    function isNotNullOrEmptyArray($array):bool
363
    {
364
        return !isNullOrEmptyArray($array);
365
    }
366
}
367
368
if (!function_exists('isNotNullOrEmptyArrayKey')) {
369
370
    /**
371
     * Check if an array key exists and is not null and not empty.
372
     * @param $array
373
     * @param string $key
374
     * @param bool $withTrim if set to true (default) check if trim()!='' too.
375
     * @return bool
376
     */
377
    function isNotNullOrEmptyArrayKey(array $array, string $key, bool $withTrim = true):bool
378
    {
379
        return !isNullOrEmptyArrayKey($array, $key, $withTrim);
380
    }
381
}
382