Subversion-Projekte lars-tiefland.laravel_shop

Revision

Zur aktuellen Revision | Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
use Illuminate\Support\Arr;
4
use Illuminate\Support\Collection;
5
 
6
if (! function_exists('collect')) {
7
    /**
8
     * Create a collection from the given value.
9
     *
10
     * @template TKey of array-key
11
     * @template TValue
12
     *
13
     * @param  \Illuminate\Contracts\Support\Arrayable<TKey, TValue>|iterable<TKey, TValue>|null  $value
14
     * @return \Illuminate\Support\Collection<TKey, TValue>
15
     */
16
    function collect($value = [])
17
    {
18
        return new Collection($value);
19
    }
20
}
21
 
22
if (! function_exists('data_fill')) {
23
    /**
24
     * Fill in data where it's missing.
25
     *
26
     * @param  mixed  $target
27
     * @param  string|array  $key
28
     * @param  mixed  $value
29
     * @return mixed
30
     */
31
    function data_fill(&$target, $key, $value)
32
    {
33
        return data_set($target, $key, $value, false);
34
    }
35
}
36
 
37
if (! function_exists('data_get')) {
38
    /**
39
     * Get an item from an array or object using "dot" notation.
40
     *
41
     * @param  mixed  $target
42
     * @param  string|array|int|null  $key
43
     * @param  mixed  $default
44
     * @return mixed
45
     */
46
    function data_get($target, $key, $default = null)
47
    {
48
        if (is_null($key)) {
49
            return $target;
50
        }
51
 
52
        $key = is_array($key) ? $key : explode('.', $key);
53
 
54
        foreach ($key as $i => $segment) {
55
            unset($key[$i]);
56
 
57
            if (is_null($segment)) {
58
                return $target;
59
            }
60
 
61
            if ($segment === '*') {
62
                if ($target instanceof Collection) {
63
                    $target = $target->all();
64
                } elseif (! is_iterable($target)) {
65
                    return value($default);
66
                }
67
 
68
                $result = [];
69
 
70
                foreach ($target as $item) {
71
                    $result[] = data_get($item, $key);
72
                }
73
 
74
                return in_array('*', $key) ? Arr::collapse($result) : $result;
75
            }
76
 
77
            if (Arr::accessible($target) && Arr::exists($target, $segment)) {
78
                $target = $target[$segment];
79
            } elseif (is_object($target) && isset($target->{$segment})) {
80
                $target = $target->{$segment};
81
            } else {
82
                return value($default);
83
            }
84
        }
85
 
86
        return $target;
87
    }
88
}
89
 
90
if (! function_exists('data_set')) {
91
    /**
92
     * Set an item on an array or object using dot notation.
93
     *
94
     * @param  mixed  $target
95
     * @param  string|array  $key
96
     * @param  mixed  $value
97
     * @param  bool  $overwrite
98
     * @return mixed
99
     */
100
    function data_set(&$target, $key, $value, $overwrite = true)
101
    {
102
        $segments = is_array($key) ? $key : explode('.', $key);
103
 
104
        if (($segment = array_shift($segments)) === '*') {
105
            if (! Arr::accessible($target)) {
106
                $target = [];
107
            }
108
 
109
            if ($segments) {
110
                foreach ($target as &$inner) {
111
                    data_set($inner, $segments, $value, $overwrite);
112
                }
113
            } elseif ($overwrite) {
114
                foreach ($target as &$inner) {
115
                    $inner = $value;
116
                }
117
            }
118
        } elseif (Arr::accessible($target)) {
119
            if ($segments) {
120
                if (! Arr::exists($target, $segment)) {
121
                    $target[$segment] = [];
122
                }
123
 
124
                data_set($target[$segment], $segments, $value, $overwrite);
125
            } elseif ($overwrite || ! Arr::exists($target, $segment)) {
126
                $target[$segment] = $value;
127
            }
128
        } elseif (is_object($target)) {
129
            if ($segments) {
130
                if (! isset($target->{$segment})) {
131
                    $target->{$segment} = [];
132
                }
133
 
134
                data_set($target->{$segment}, $segments, $value, $overwrite);
135
            } elseif ($overwrite || ! isset($target->{$segment})) {
136
                $target->{$segment} = $value;
137
            }
138
        } else {
139
            $target = [];
140
 
141
            if ($segments) {
142
                data_set($target[$segment], $segments, $value, $overwrite);
143
            } elseif ($overwrite) {
144
                $target[$segment] = $value;
145
            }
146
        }
147
 
148
        return $target;
149
    }
150
}
151
 
152
if (! function_exists('head')) {
153
    /**
154
     * Get the first element of an array. Useful for method chaining.
155
     *
156
     * @param  array  $array
157
     * @return mixed
158
     */
159
    function head($array)
160
    {
161
        return reset($array);
162
    }
163
}
164
 
165
if (! function_exists('last')) {
166
    /**
167
     * Get the last element from an array.
168
     *
169
     * @param  array  $array
170
     * @return mixed
171
     */
172
    function last($array)
173
    {
174
        return end($array);
175
    }
176
}
177
 
178
if (! function_exists('value')) {
179
    /**
180
     * Return the default value of the given value.
181
     *
182
     * @param  mixed  $value
183
     * @param  mixed  $args
184
     * @return mixed
185
     */
186
    function value($value, ...$args)
187
    {
188
        return $value instanceof Closure ? $value(...$args) : $value;
189
    }
190
}