Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
use Illuminate\Contracts\Support\DeferringDisplayableValue;
4
use Illuminate\Contracts\Support\Htmlable;
5
use Illuminate\Support\Arr;
6
use Illuminate\Support\Env;
7
use Illuminate\Support\HigherOrderTapProxy;
8
use Illuminate\Support\Optional;
9
use Illuminate\Support\Str;
10
 
11
if (! function_exists('append_config')) {
12
    /**
13
     * Assign high numeric IDs to a config item to force appending.
14
     *
15
     * @param  array  $array
16
     * @return array
17
     */
18
    function append_config(array $array)
19
    {
20
        $start = 9999;
21
 
22
        foreach ($array as $key => $value) {
23
            if (is_numeric($key)) {
24
                $start++;
25
 
26
                $array[$start] = Arr::pull($array, $key);
27
            }
28
        }
29
 
30
        return $array;
31
    }
32
}
33
 
34
if (! function_exists('blank')) {
35
    /**
36
     * Determine if the given value is "blank".
37
     *
38
     * @param  mixed  $value
39
     * @return bool
40
     */
41
    function blank($value)
42
    {
43
        if (is_null($value)) {
44
            return true;
45
        }
46
 
47
        if (is_string($value)) {
48
            return trim($value) === '';
49
        }
50
 
51
        if (is_numeric($value) || is_bool($value)) {
52
            return false;
53
        }
54
 
55
        if ($value instanceof Countable) {
56
            return count($value) === 0;
57
        }
58
 
59
        return empty($value);
60
    }
61
}
62
 
63
if (! function_exists('class_basename')) {
64
    /**
65
     * Get the class "basename" of the given object / class.
66
     *
67
     * @param  string|object  $class
68
     * @return string
69
     */
70
    function class_basename($class)
71
    {
72
        $class = is_object($class) ? get_class($class) : $class;
73
 
74
        return basename(str_replace('\\', '/', $class));
75
    }
76
}
77
 
78
if (! function_exists('class_uses_recursive')) {
79
    /**
80
     * Returns all traits used by a class, its parent classes and trait of their traits.
81
     *
82
     * @param  object|string  $class
83
     * @return array
84
     */
85
    function class_uses_recursive($class)
86
    {
87
        if (is_object($class)) {
88
            $class = get_class($class);
89
        }
90
 
91
        $results = [];
92
 
93
        foreach (array_reverse(class_parents($class)) + [$class => $class] as $class) {
94
            $results += trait_uses_recursive($class);
95
        }
96
 
97
        return array_unique($results);
98
    }
99
}
100
 
101
if (! function_exists('e')) {
102
    /**
103
     * Encode HTML special characters in a string.
104
     *
105
     * @param  \Illuminate\Contracts\Support\DeferringDisplayableValue|\Illuminate\Contracts\Support\Htmlable|\BackedEnum|string|null  $value
106
     * @param  bool  $doubleEncode
107
     * @return string
108
     */
109
    function e($value, $doubleEncode = true)
110
    {
111
        if ($value instanceof DeferringDisplayableValue) {
112
            $value = $value->resolveDisplayableValue();
113
        }
114
 
115
        if ($value instanceof Htmlable) {
116
            return $value->toHtml();
117
        }
118
 
119
        if ($value instanceof BackedEnum) {
120
            $value = $value->value;
121
        }
122
 
123
        return htmlspecialchars($value ?? '', ENT_QUOTES, 'UTF-8', $doubleEncode);
124
    }
125
}
126
 
127
if (! function_exists('env')) {
128
    /**
129
     * Gets the value of an environment variable.
130
     *
131
     * @param  string  $key
132
     * @param  mixed  $default
133
     * @return mixed
134
     */
135
    function env($key, $default = null)
136
    {
137
        return Env::get($key, $default);
138
    }
139
}
140
 
141
if (! function_exists('filled')) {
142
    /**
143
     * Determine if a value is "filled".
144
     *
145
     * @param  mixed  $value
146
     * @return bool
147
     */
148
    function filled($value)
149
    {
150
        return ! blank($value);
151
    }
152
}
153
 
154
if (! function_exists('object_get')) {
155
    /**
156
     * Get an item from an object using "dot" notation.
157
     *
158
     * @param  object  $object
159
     * @param  string|null  $key
160
     * @param  mixed  $default
161
     * @return mixed
162
     */
163
    function object_get($object, $key, $default = null)
164
    {
165
        if (is_null($key) || trim($key) === '') {
166
            return $object;
167
        }
168
 
169
        foreach (explode('.', $key) as $segment) {
170
            if (! is_object($object) || ! isset($object->{$segment})) {
171
                return value($default);
172
            }
173
 
174
            $object = $object->{$segment};
175
        }
176
 
177
        return $object;
178
    }
179
}
180
 
181
if (! function_exists('optional')) {
182
    /**
183
     * Provide access to optional objects.
184
     *
185
     * @param  mixed  $value
186
     * @param  callable|null  $callback
187
     * @return mixed
188
     */
189
    function optional($value = null, callable $callback = null)
190
    {
191
        if (is_null($callback)) {
192
            return new Optional($value);
193
        } elseif (! is_null($value)) {
194
            return $callback($value);
195
        }
196
    }
197
}
198
 
199
if (! function_exists('preg_replace_array')) {
200
    /**
201
     * Replace a given pattern with each value in the array in sequentially.
202
     *
203
     * @param  string  $pattern
204
     * @param  array  $replacements
205
     * @param  string  $subject
206
     * @return string
207
     */
208
    function preg_replace_array($pattern, array $replacements, $subject)
209
    {
210
        return preg_replace_callback($pattern, function () use (&$replacements) {
211
            foreach ($replacements as $value) {
212
                return array_shift($replacements);
213
            }
214
        }, $subject);
215
    }
216
}
217
 
218
if (! function_exists('retry')) {
219
    /**
220
     * Retry an operation a given number of times.
221
     *
222
     * @param  int|array  $times
223
     * @param  callable  $callback
224
     * @param  int|\Closure  $sleepMilliseconds
225
     * @param  callable|null  $when
226
     * @return mixed
227
     *
228
     * @throws \Exception
229
     */
230
    function retry($times, callable $callback, $sleepMilliseconds = 0, $when = null)
231
    {
232
        $attempts = 0;
233
 
234
        $backoff = [];
235
 
236
        if (is_array($times)) {
237
            $backoff = $times;
238
 
239
            $times = count($times) + 1;
240
        }
241
 
242
        beginning:
243
        $attempts++;
244
        $times--;
245
 
246
        try {
247
            return $callback($attempts);
248
        } catch (Exception $e) {
249
            if ($times < 1 || ($when && ! $when($e))) {
250
                throw $e;
251
            }
252
 
253
            $sleepMilliseconds = $backoff[$attempts - 1] ?? $sleepMilliseconds;
254
 
255
            if ($sleepMilliseconds) {
256
                usleep(value($sleepMilliseconds, $attempts, $e) * 1000);
257
            }
258
 
259
            goto beginning;
260
        }
261
    }
262
}
263
 
264
if (! function_exists('str')) {
265
    /**
266
     * Get a new stringable object from the given string.
267
     *
268
     * @param  string|null  $string
269
     * @return \Illuminate\Support\Stringable|mixed
270
     */
271
    function str($string = null)
272
    {
273
        if (func_num_args() === 0) {
274
            return new class
275
            {
276
                public function __call($method, $parameters)
277
                {
278
                    return Str::$method(...$parameters);
279
                }
280
 
281
                public function __toString()
282
                {
283
                    return '';
284
                }
285
            };
286
        }
287
 
288
        return Str::of($string);
289
    }
290
}
291
 
292
if (! function_exists('tap')) {
293
    /**
294
     * Call the given Closure with the given value then return the value.
295
     *
296
     * @param  mixed  $value
297
     * @param  callable|null  $callback
298
     * @return mixed
299
     */
300
    function tap($value, $callback = null)
301
    {
302
        if (is_null($callback)) {
303
            return new HigherOrderTapProxy($value);
304
        }
305
 
306
        $callback($value);
307
 
308
        return $value;
309
    }
310
}
311
 
312
if (! function_exists('throw_if')) {
313
    /**
314
     * Throw the given exception if the given condition is true.
315
     *
316
     * @param  mixed  $condition
317
     * @param  \Throwable|string  $exception
318
     * @param  mixed  ...$parameters
319
     * @return mixed
320
     *
321
     * @throws \Throwable
322
     */
323
    function throw_if($condition, $exception = 'RuntimeException', ...$parameters)
324
    {
325
        if ($condition) {
326
            if (is_string($exception) && class_exists($exception)) {
327
                $exception = new $exception(...$parameters);
328
            }
329
 
330
            throw is_string($exception) ? new RuntimeException($exception) : $exception;
331
        }
332
 
333
        return $condition;
334
    }
335
}
336
 
337
if (! function_exists('throw_unless')) {
338
    /**
339
     * Throw the given exception unless the given condition is true.
340
     *
341
     * @param  mixed  $condition
342
     * @param  \Throwable|string  $exception
343
     * @param  mixed  ...$parameters
344
     * @return mixed
345
     *
346
     * @throws \Throwable
347
     */
348
    function throw_unless($condition, $exception = 'RuntimeException', ...$parameters)
349
    {
350
        throw_if(! $condition, $exception, ...$parameters);
351
 
352
        return $condition;
353
    }
354
}
355
 
356
if (! function_exists('trait_uses_recursive')) {
357
    /**
358
     * Returns all traits used by a trait and its traits.
359
     *
360
     * @param  string  $trait
361
     * @return array
362
     */
363
    function trait_uses_recursive($trait)
364
    {
365
        $traits = class_uses($trait) ?: [];
366
 
367
        foreach ($traits as $trait) {
368
            $traits += trait_uses_recursive($trait);
369
        }
370
 
371
        return $traits;
372
    }
373
}
374
 
375
if (! function_exists('transform')) {
376
    /**
377
     * Transform the given value if it is present.
378
     *
379
     * @param  mixed  $value
380
     * @param  callable  $callback
381
     * @param  mixed  $default
382
     * @return mixed|null
383
     */
384
    function transform($value, callable $callback, $default = null)
385
    {
386
        if (filled($value)) {
387
            return $callback($value);
388
        }
389
 
390
        if (is_callable($default)) {
391
            return $default($value);
392
        }
393
 
394
        return $default;
395
    }
396
}
397
 
398
if (! function_exists('windows_os')) {
399
    /**
400
     * Determine whether the current environment is Windows based.
401
     *
402
     * @return bool
403
     */
404
    function windows_os()
405
    {
406
        return PHP_OS_FAMILY === 'Windows';
407
    }
408
}
409
 
410
if (! function_exists('with')) {
411
    /**
412
     * Return the given value, optionally passed through the given callback.
413
     *
414
     * @template TValue
415
     * @template TReturn
416
     *
417
     * @param  TValue  $value
418
     * @param  (callable(TValue): (TReturn))|null  $callback
419
     * @return ($callback is null ? TValue : TReturn)
420
     */
421
    function with($value, callable $callback = null)
422
    {
423
        return is_null($callback) ? $value : $callback($value);
424
    }
425
}