Completed
Push — master ( adee67...85a1dc )
by Antonio Carlos
01:34
created

helpers.php ➔ collect()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
ccs 1
cts 1
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
use IlluminateAgnostic\Str\Support\Arr;
4
use IlluminateAgnostic\Str\Support\Str;
5
use IlluminateAgnostic\Str\Support\Collection;
6
use IlluminateAgnostic\Str\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\Str\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('data_get')) {
23
        /**
24
         * Get an item from an array or object using "dot" notation.
25
         *
26
         * @param  mixed   $target
27
         * @param  string|array  $key
28
         * @param  mixed   $default
29
         * @return mixed
30
         */
31
        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...
32
        {
33 98
            if (is_null($key)) {
34 26
                return $target;
35
            }
36
37 86
            $key = is_array($key) ? $key : explode('.', $key);
38
39 86
            while (!is_null($segment = array_shift($key))) {
40 86
                if ($segment === '*') {
41 1
                    if ($target instanceof Collection) {
42
                        $target = $target->all();
43 1
                    } elseif (!is_array($target)) {
44
                        return value($default);
45
                    }
46
47 1
                    $result = Arr::pluck($target, $key);
48
49 1
                    return in_array('*', $key)
50
                        ? Arr::collapse($result)
51 1
                        : $result;
52
                }
53
54
                if (
55 86
                    Arr::accessible($target) &&
56 86
                    Arr::exists($target, $segment)
57
                ) {
58 72
                    $target = $target[$segment];
59 29
                } elseif (is_object($target) && isset($target->{$segment})) {
60 25
                    $target = $target->{$segment};
61
                } else {
62 8
                    return value($default);
63
                }
64
            }
65
66 86
            return $target;
67
        }
68
    }
69
70
    if (!function_exists('value')) {
71
        /**
72
         * Return the default value of the given value.
73
         *
74
         * @param  mixed  $value
75
         * @return mixed
76
         */
77
        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...
78
        {
79 31
            return $value instanceof Closure ? $value() : $value;
80
        }
81
    }
82
83
    if (!function_exists('dd')) {
84
        /**
85
         * Dump the passed variables and end the script.
86
         *
87
         * @param  mixed  $args
88
         * @return void
89
         */
90
        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...
91
        {
92
            http_response_code(500);
93
94
            foreach ($args as $x) {
95
                (new Dumper())->dump($x);
96
            }
97
98
            die(1);
99
        }
100
    }
101
}
102