Completed
Push — master ( 1df67d...8eb5fd )
by Antonio Carlos
01:44
created

helpers.php ➔ with()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
ccs 0
cts 1
cp 0
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
8
if (!class_exists(Illuminate\Support\Collection::class)) {
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)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
17
        {
18 5
            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)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
30
        {
31 31
            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)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
45
        {
46 98
            if (is_null($key)) {
47 26
                return $target;
48
            }
49
50 86
            $key = is_array($key) ? $key : explode('.', $key);
51
52 86
            while (! is_null($segment = array_shift($key))) {
53 86
                if ($segment === '*') {
54 1
                    if ($target instanceof Collection) {
55
                        $target = $target->all();
56 1
                    } elseif (! is_array($target)) {
57
                        return value($default);
58
                    }
59
60 1
                    $result = Arr::pluck($target, $key);
61
62 1
                    return in_array('*', $key) ? Arr::collapse($result) : $result;
63
                }
64
65 86
                if (Arr::accessible($target) && Arr::exists($target, $segment)) {
66 72
                    $target = $target[$segment];
67 29
                } elseif (is_object($target) && isset($target->{$segment})) {
68 25
                    $target = $target->{$segment};
69
                } else {
70 8
                    return value($default);
71
                }
72
            }
73
74 86
            return $target;
75
        }
76
    }
77
78
    if (! function_exists('with')) {
79
        /**
80
         * Return the given object. Useful for chaining.
81
         *
82
         * @param  mixed  $object
83
         * @return mixed
84
         */
85
        function with($object)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
86
        {
87
            return $object;
88
        }
89
    }
90
91
    if (! function_exists('dd')) {
92
        /**
93
         * Dump the passed variables and end the script.
94
         *
95
         * @param  mixed
96
         * @return void
97
         */
98
        function dd(...$args)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
99
        {
100
            foreach ($args as $x) {
101
                (new Dumper)->dump($x);
102
            }
103
            die(1);
104
        }
105
    }
106
}
107