Issues (41)

src/helpers/general.php (3 issues)

1
<?php
2
3
use Nip\Utility\Arr;
4
5
if (!function_exists('env')) {
6
    /**
7
     * Gets the value of an environment variable.
8
     *
9
     * @param  string $key
10
     * @param  mixed $default
11
     * @return mixed
12
     */
13
    function env($key, $default = null)
14
    {
15
        return \Nip\Utility\Env::get($key, $default);
16
    }
17
}
18
19
if (! function_exists('data_get')) {
20
    /**
21
     * Get an item from an array or object using "dot" notation.
22
     *
23
     * @param  mixed   $target
24
     * @param  string|array|int|callable  $key
25
     * @param  mixed   $default
26
     * @return mixed
27
     */
28
    function data_get($target, $key, $default = null)
29
    {
30
        if (is_null($key)) {
0 ignored issues
show
The condition is_null($key) is always false.
Loading history...
31
            return $target;
32
        }
33
34
        if (is_callable($key)) {
35
            return call_user_func($key, $target);
0 ignored issues
show
It seems like $key can also be of type integer; however, parameter $callback of call_user_func() does only seem to accept callable, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

35
            return call_user_func(/** @scrutinizer ignore-type */ $key, $target);
Loading history...
36
        }
37
38
        $key = is_array($key) ? $key : explode('.', $key);
0 ignored issues
show
It seems like $key can also be of type callable; however, parameter $string of explode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

38
        $key = is_array($key) ? $key : explode('.', /** @scrutinizer ignore-type */ $key);
Loading history...
39
40
        while (! is_null($segment = array_shift($key))) {
41
            if ($segment === '*') {
42
                if (is_object($target) && method_exists($target, 'all')) {
43
                    $target = $target->all();
44
                } elseif (! is_array($target)) {
45
                    return value($default);
46
                }
47
                $result = [];
48
                foreach ($target as $item) {
49 1
                    $result[] = data_get($item, $key);
50
                }
51
                return in_array('*', $key) ? Arr::collapse($result) : $result;
52
            }
53
            if (Arr::accessible($target) && Arr::exists($target, $segment)) {
54
                $target = $target[$segment];
55
            } elseif (is_object($target)) {
56
                if (isset($target->{$segment})) {
57
                    $target = $target->{$segment};
58
                } elseif (method_exists($target, $segment)) {
59
                    $target = $target->{$segment}();
60
                }
61
            } else {
62
                return value($default);
63
            }
64 1
        }
65
        return $target;
66
    }
67 1
}
68
if (! function_exists('head')) {
69 1
    /**
70 1
     * Get the first element of an array. Useful for method chaining.
71
     *
72
     * @param  array  $array
73
     * @return mixed
74
     */
75
    function head($array)
76
    {
77
        return reset($array);
78
    }
79
}
80
81
if (! function_exists('last')) {
82 1
    /**
83 1
     * Get the last element from an array.
84 1
     *
85
     * @param  array  $array
86
     * @return mixed
87 1
     */
88
    function last($array)
89
    {
90 1
        return end($array);
91
    }
92
}
93
94
if (!function_exists('value')) {
95
    /**
96
     * Return the default value of the given value.
97
     *
98
     * @param mixed $value
99
     * @return mixed
100
     */
101
    function value($value)
102
    {
103
        return $value instanceof Closure ? $value() : $value;
104
    }
105
}