Completed
Push — master ( d885df...318105 )
by Antonio Carlos
02:50 queued 01:07
created

helpers.php ➔ array_wrap()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 1
dl 0
loc 4
ccs 1
cts 1
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
use IlluminateAgnostic\Collection\Support\Str;
4
use IlluminateAgnostic\Collection\Support\Arr;
5
use IlluminateAgnostic\Collection\Support\Collection;
6
use IlluminateAgnostic\Collection\Support\Debug\Dumper;
7
use Illuminate\Support\Collection as IlluminateCollection;
8
9
if (! function_exists('collect')) {
10
    /**
11
     * Create a collection from the given value.
12
     *
13
     * @param  mixed  $value
14
     * @return \IlluminateAgnostic\Collection\Support\Collection|\Illuminate\Support\Collection
15
     */
16
    function collect($value = null)
17
    {
18 3
        return new Collection($value);
19
    }
20
}
21
22
if (! function_exists('value')) {
23
    /**
24
     * Return the default value of the given value.
25
     *
26
     * @param  mixed  $value
27
     * @return mixed
28
     */
29
    function value($value)
30
    {
31 13
        return $value instanceof Closure ? $value() : $value;
32
    }
33
}
34
35
if (! function_exists('data_get')) {
36
    /**
37
     * Get an item from an array or object using "dot" notation.
38
     *
39
     * @param  mixed   $target
40
     * @param  string|array  $key
41
     * @param  mixed   $default
42
     * @return mixed
43
     */
44
    function data_get($target, $key, $default = null)
45
    {
46 38
        if (is_null($key)) {
47 4
            return $target;
48
        }
49
50 38
        $key = is_array($key) ? $key : explode('.', $key);
51
52 38
        while (! is_null($segment = array_shift($key))) {
53 38
            if ($segment === '*') {
54
                if ($target instanceof Collection) {
55
                    $target = $target->all();
56
                } elseif (! is_array($target)) {
57
                    return value($default);
58
                }
59
60
                $result = Arr::pluck($target, $key);
61
62
                return in_array('*', $key) ? Arr::collapse($result) : $result;
63
            }
64
65 38
            if (Arr::accessible($target) && Arr::exists($target, $segment)) {
66 32
                $target = $target[$segment];
67 12
            } elseif (is_object($target) && isset($target->{$segment})) {
68 11
                $target = $target->{$segment};
69
            } else {
70 1
                return value($default);
71
            }
72
        }
73
74 38
        return $target;
75
    }
76
}
77
78
79
if (! function_exists('with')) {
80
    /**
81
     * Return the given object. Useful for chaining.
82
     *
83
     * @param  mixed  $object
84
     * @return mixed
85
     */
86
    function with($object)
87
    {
88
        return $object;
89
    }
90
}
91
92
if (! function_exists('dd')) {
93
    /**
94
     * Dump the passed variables and end the script.
95
     *
96
     * @param  mixed
97
     * @return void
98
     */
99
    function dd(...$args)
100
    {
101
        foreach ($args as $x) {
102
            (new Dumper)->dump($x);
103
        }
104
        die(1);
105
    }
106
}
107
108
if (! function_exists('studly_case')) {
109
    /**
110
     * Convert a value to studly caps case.
111
     *
112
     * @param  string  $value
113
     * @return string
114
     */
115
    function studly_case($value)
116
    {
117
        return Str::studly($value);
118
    }
119
}
120