Completed
Push — master ( 7d0222...1df67d )
by Antonio Carlos
02:21
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
use Illuminate\Support\Collection as IlluminateCollection;
8
9
if (!class_exists(Illuminate\Support\Collection::class)) {
10
    if (! function_exists('collect')) {
11
        /**
12
         * Create a collection from the given value.
13
         *
14
         * @param  mixed  $value
15
         * @return \IlluminateAgnostic\Collection\Support\Collection|\Illuminate\Support\Collection
16
         */
17
        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...
18
        {
19
            return new Collection($value);
20
        }
21
    }
22
23
    if (! function_exists('value')) {
24
        /**
25
         * Return the default value of the given value.
26
         *
27
         * @param  mixed  $value
28
         * @return mixed
29
         */
30
        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...
31
        {
32
            return $value instanceof Closure ? $value() : $value;
33
        }
34
    }
35
36
    if (! function_exists('data_get')) {
37
        /**
38
         * Get an item from an array or object using "dot" notation.
39
         *
40
         * @param  mixed   $target
41
         * @param  string|array  $key
42
         * @param  mixed   $default
43
         * @return mixed
44
         */
45
        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...
46
        {
47
            if (is_null($key)) {
48
                return $target;
49
            }
50
51
            $key = is_array($key) ? $key : explode('.', $key);
52
53
            while (! is_null($segment = array_shift($key))) {
54
                if ($segment === '*') {
55
                    if ($target instanceof Collection) {
56
                        $target = $target->all();
57
                    } elseif (! is_array($target)) {
58
                        return value($default);
59
                    }
60
61
                    $result = Arr::pluck($target, $key);
62
63
                    return in_array('*', $key) ? Arr::collapse($result) : $result;
64
                }
65
66
                if (Arr::accessible($target) && Arr::exists($target, $segment)) {
67
                    $target = $target[$segment];
68
                } elseif (is_object($target) && isset($target->{$segment})) {
69
                    $target = $target->{$segment};
70
                } else {
71
                    return value($default);
72
                }
73
            }
74
75
            return $target;
76
        }
77
    }
78
79
    if (! function_exists('dd')) {
80
        /**
81
         * Dump the passed variables and end the script.
82
         *
83
         * @param  mixed
84
         * @return void
85
         */
86
        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...
87
        {
88
            foreach ($args as $x) {
89
                (new Dumper)->dump($x);
90
            }
91
            die(1);
92
        }
93
    }
94
}
95