Completed
Push — master ( 1efa22...d57925 )
by Lorenzo
02:40
created

array.php ➔ isNotNullOrEmptyArrayKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 3
dl 0
loc 4
rs 10
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('set')) {
31
    /**
32
     * Set an array item to a given value using "dot" notation.
33
     *
34
     * If no key is given to the method, the entire array will be replaced.
35
     *
36
     * @param  array $array
37
     * @param  string $key
38
     * @param  mixed $value
39
     * @return array
40
     */
41
    function set(&$array, $key, $value)
42
    {
43
        if (is_null($key)) {
44
            return $array = $value;
45
        }
46
        $keys = explode('.', $key);
47
        while (count($keys) > 1) {
48
            $key = array_shift($keys);
49
            // If the key doesn't exist at this depth, we will just create an empty array
50
            // to hold the next value, allowing us to create the arrays to hold final
51
            // values at the correct depth. Then we'll keep digging into the array.
52
            if (!isset($array[$key]) || !is_array($array[$key])) {
53
                $array[$key] = array();
54
            }
55
            $array =& $array[$key];
56
        }
57
        $array[array_shift($keys)] = $value;
58
        return $array;
59
    }
60
}
61
62
if (!function_exists('head')) {
63
    /**
64
     * Get the first element of an array. Useful for method chaining.
65
     *
66
     * @param  array $array
67
     * @return mixed
68
     */
69
    function head($array)
70
    {
71
        return reset($array);
72
    }
73
}
74
75
if (!function_exists('last')) {
76
    /**
77
     * Get the last element from an array.
78
     *
79
     * @param  array $array
80
     * @return mixed
81
     */
82
    function last($array)
83
    {
84
        return end($array);
85
    }
86
}
87
88
if (!function_exists('array_has')) {
89
    /**
90
     * Check if an item exists in an array using "dot" notation.
91
     *
92
     * @param  array $array
93
     * @param  string $key
94
     * @return bool
95
     */
96
    function array_has($array, $key)
97
    {
98
        if (empty($array) || is_null($key)) {
99
            return false;
100
        }
101
        if (array_key_exists($key, $array)) {
102
            return true;
103
        }
104
        foreach (explode('.', $key) as $segment) {
105
            if (!array_key_exists_safe($segment, $array)) {
106
                return false;
107
            }
108
            $array = $array[$segment];
109
        }
110
        return true;
111
    }
112
}
113
114 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...
115
    /**
116
     * Get an item from an array using "dot" notation.
117
     *
118
     * @param  array $array
119
     * @param  string $key
120
     * @param  mixed $default
121
     * @return mixed
122
     */
123
    function array_get($array, $key, $default = null)
124
    {
125
        if (is_null($key)) {
126
            return $array;
127
        }
128
        if (isset($array[$key])) {
129
            return $array[$key];
130
        }
131
        foreach (explode('.', $key) as $segment) {
132
            if (!array_key_exists_safe($segment, $array)) {
133
                return value($default);
134
            }
135
            $array = $array[$segment];
136
        }
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('isNullOrEmptyArray')) {
307
308
    /**
309
     * Check if array is null or empty.
310
     * @param $array
311
     * @return bool
312
     */
313
    function isNullOrEmptyArray($array):bool
314
    {
315
        return $array === null || !is_array($array) || count($array) < 1;
316
    }
317
}
318
319
if (!function_exists('isNullOrEmptyArrayKey')) {
320
321
    /**
322
     * Check if an array key not exits or exists and is null or empty.
323
     * @param $array
324
     * @param string $key
325
     * @param bool $withTrim if set to true (default) check if trim()!='' too.
326
     * @return bool
327
     */
328
    function isNullOrEmptyArrayKey(array $array, string $key, bool $withTrim = true):bool
329
    {
330
        return !array_key_exists_safe($array, $key) || $array[$key]===null || isNullOrEmpty($array[$key], $withTrim);
331
    }
332
}
333
334
if (!function_exists('isNotNullOrEmptyArray')) {
335
336
    /**
337
     * Check if array is not null and not empty.
338
     * @param $array
339
     * @return bool
340
     */
341
    function isNotNullOrEmptyArray($array):bool
342
    {
343
        return !isNullOrEmptyArray($array);
344
    }
345
}
346
347
if (!function_exists('isNotNullOrEmptyArrayKey')) {
348
349
    /**
350
     * Check if an array key exists and is not null and not empty.
351
     * @param $array
352
     * @param string $key
353
     * @param bool $withTrim if set to true (default) check if trim()!='' too.
354
     * @return bool
355
     */
356
    function isNotNullOrEmptyArrayKey(array $array, string $key, bool $withTrim = true):bool
357
    {
358
        return !isNullOrEmptyArrayKey($array, $key, $withTrim);
359
    }
360
}
361