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
namespace Illuminate\Http\Concerns;
4
 
5
use Illuminate\Http\UploadedFile;
6
use Illuminate\Support\Arr;
7
use Illuminate\Support\Facades\Date;
8
use SplFileInfo;
9
use stdClass;
10
use Symfony\Component\HttpFoundation\InputBag;
11
use Symfony\Component\VarDumper\VarDumper;
12
 
13
trait InteractsWithInput
14
{
15
    /**
16
     * Retrieve a server variable from the request.
17
     *
18
     * @param  string|null  $key
19
     * @param  string|array|null  $default
20
     * @return string|array|null
21
     */
22
    public function server($key = null, $default = null)
23
    {
24
        return $this->retrieveItem('server', $key, $default);
25
    }
26
 
27
    /**
28
     * Determine if a header is set on the request.
29
     *
30
     * @param  string  $key
31
     * @return bool
32
     */
33
    public function hasHeader($key)
34
    {
35
        return ! is_null($this->header($key));
36
    }
37
 
38
    /**
39
     * Retrieve a header from the request.
40
     *
41
     * @param  string|null  $key
42
     * @param  string|array|null  $default
43
     * @return string|array|null
44
     */
45
    public function header($key = null, $default = null)
46
    {
47
        return $this->retrieveItem('headers', $key, $default);
48
    }
49
 
50
    /**
51
     * Get the bearer token from the request headers.
52
     *
53
     * @return string|null
54
     */
55
    public function bearerToken()
56
    {
57
        $header = $this->header('Authorization', '');
58
 
59
        $position = strrpos($header, 'Bearer ');
60
 
61
        if ($position !== false) {
62
            $header = substr($header, $position + 7);
63
 
64
            return str_contains($header, ',') ? strstr($header, ',', true) : $header;
65
        }
66
    }
67
 
68
    /**
69
     * Determine if the request contains a given input item key.
70
     *
71
     * @param  string|array  $key
72
     * @return bool
73
     */
74
    public function exists($key)
75
    {
76
        return $this->has($key);
77
    }
78
 
79
    /**
80
     * Determine if the request contains a given input item key.
81
     *
82
     * @param  string|array  $key
83
     * @return bool
84
     */
85
    public function has($key)
86
    {
87
        $keys = is_array($key) ? $key : func_get_args();
88
 
89
        $input = $this->all();
90
 
91
        foreach ($keys as $value) {
92
            if (! Arr::has($input, $value)) {
93
                return false;
94
            }
95
        }
96
 
97
        return true;
98
    }
99
 
100
    /**
101
     * Determine if the request contains any of the given inputs.
102
     *
103
     * @param  string|array  $keys
104
     * @return bool
105
     */
106
    public function hasAny($keys)
107
    {
108
        $keys = is_array($keys) ? $keys : func_get_args();
109
 
110
        $input = $this->all();
111
 
112
        return Arr::hasAny($input, $keys);
113
    }
114
 
115
    /**
116
     * Apply the callback if the request contains the given input item key.
117
     *
118
     * @param  string  $key
119
     * @param  callable  $callback
120
     * @param  callable|null  $default
121
     * @return $this|mixed
122
     */
123
    public function whenHas($key, callable $callback, callable $default = null)
124
    {
125
        if ($this->has($key)) {
126
            return $callback(data_get($this->all(), $key)) ?: $this;
127
        }
128
 
129
        if ($default) {
130
            return $default();
131
        }
132
 
133
        return $this;
134
    }
135
 
136
    /**
137
     * Determine if the request contains a non-empty value for an input item.
138
     *
139
     * @param  string|array  $key
140
     * @return bool
141
     */
142
    public function filled($key)
143
    {
144
        $keys = is_array($key) ? $key : func_get_args();
145
 
146
        foreach ($keys as $value) {
147
            if ($this->isEmptyString($value)) {
148
                return false;
149
            }
150
        }
151
 
152
        return true;
153
    }
154
 
155
    /**
156
     * Determine if the request contains an empty value for an input item.
157
     *
158
     * @param  string|array  $key
159
     * @return bool
160
     */
161
    public function isNotFilled($key)
162
    {
163
        $keys = is_array($key) ? $key : func_get_args();
164
 
165
        foreach ($keys as $value) {
166
            if (! $this->isEmptyString($value)) {
167
                return false;
168
            }
169
        }
170
 
171
        return true;
172
    }
173
 
174
    /**
175
     * Determine if the request contains a non-empty value for any of the given inputs.
176
     *
177
     * @param  string|array  $keys
178
     * @return bool
179
     */
180
    public function anyFilled($keys)
181
    {
182
        $keys = is_array($keys) ? $keys : func_get_args();
183
 
184
        foreach ($keys as $key) {
185
            if ($this->filled($key)) {
186
                return true;
187
            }
188
        }
189
 
190
        return false;
191
    }
192
 
193
    /**
194
     * Apply the callback if the request contains a non-empty value for the given input item key.
195
     *
196
     * @param  string  $key
197
     * @param  callable  $callback
198
     * @param  callable|null  $default
199
     * @return $this|mixed
200
     */
201
    public function whenFilled($key, callable $callback, callable $default = null)
202
    {
203
        if ($this->filled($key)) {
204
            return $callback(data_get($this->all(), $key)) ?: $this;
205
        }
206
 
207
        if ($default) {
208
            return $default();
209
        }
210
 
211
        return $this;
212
    }
213
 
214
    /**
215
     * Determine if the request is missing a given input item key.
216
     *
217
     * @param  string|array  $key
218
     * @return bool
219
     */
220
    public function missing($key)
221
    {
222
        $keys = is_array($key) ? $key : func_get_args();
223
 
224
        return ! $this->has($keys);
225
    }
226
 
227
    /**
228
     * Apply the callback if the request is missing the given input item key.
229
     *
230
     * @param  string  $key
231
     * @param  callable  $callback
232
     * @param  callable|null  $default
233
     * @return $this|mixed
234
     */
235
    public function whenMissing($key, callable $callback, callable $default = null)
236
    {
237
        if ($this->missing($key)) {
238
            return $callback(data_get($this->all(), $key)) ?: $this;
239
        }
240
 
241
        if ($default) {
242
            return $default();
243
        }
244
 
245
        return $this;
246
    }
247
 
248
    /**
249
     * Determine if the given input key is an empty string for "has".
250
     *
251
     * @param  string  $key
252
     * @return bool
253
     */
254
    protected function isEmptyString($key)
255
    {
256
        $value = $this->input($key);
257
 
258
        return ! is_bool($value) && ! is_array($value) && trim((string) $value) === '';
259
    }
260
 
261
    /**
262
     * Get the keys for all of the input and files.
263
     *
264
     * @return array
265
     */
266
    public function keys()
267
    {
268
        return array_merge(array_keys($this->input()), $this->files->keys());
269
    }
270
 
271
    /**
272
     * Get all of the input and files for the request.
273
     *
274
     * @param  array|mixed|null  $keys
275
     * @return array
276
     */
277
    public function all($keys = null)
278
    {
279
        $input = array_replace_recursive($this->input(), $this->allFiles());
280
 
281
        if (! $keys) {
282
            return $input;
283
        }
284
 
285
        $results = [];
286
 
287
        foreach (is_array($keys) ? $keys : func_get_args() as $key) {
288
            Arr::set($results, $key, Arr::get($input, $key));
289
        }
290
 
291
        return $results;
292
    }
293
 
294
    /**
295
     * Retrieve an input item from the request.
296
     *
297
     * @param  string|null  $key
298
     * @param  mixed  $default
299
     * @return mixed
300
     */
301
    public function input($key = null, $default = null)
302
    {
303
        return data_get(
304
            $this->getInputSource()->all() + $this->query->all(), $key, $default
305
        );
306
    }
307
 
308
    /**
309
     * Retrieve input from the request as a Stringable instance.
310
     *
311
     * @param  string  $key
312
     * @param  mixed  $default
313
     * @return \Illuminate\Support\Stringable
314
     */
315
    public function str($key, $default = null)
316
    {
317
        return $this->string($key, $default);
318
    }
319
 
320
    /**
321
     * Retrieve input from the request as a Stringable instance.
322
     *
323
     * @param  string  $key
324
     * @param  mixed  $default
325
     * @return \Illuminate\Support\Stringable
326
     */
327
    public function string($key, $default = null)
328
    {
329
        return str($this->input($key, $default));
330
    }
331
 
332
    /**
333
     * Retrieve input as a boolean value.
334
     *
335
     * Returns true when value is "1", "true", "on", and "yes". Otherwise, returns false.
336
     *
337
     * @param  string|null  $key
338
     * @param  bool  $default
339
     * @return bool
340
     */
341
    public function boolean($key = null, $default = false)
342
    {
343
        return filter_var($this->input($key, $default), FILTER_VALIDATE_BOOLEAN);
344
    }
345
 
346
    /**
347
     * Retrieve input as an integer value.
348
     *
349
     * @param  string  $key
350
     * @param  int  $default
351
     * @return int
352
     */
353
    public function integer($key, $default = 0)
354
    {
355
        return intval($this->input($key, $default));
356
    }
357
 
358
    /**
359
     * Retrieve input as a float value.
360
     *
361
     * @param  string  $key
362
     * @param  float  $default
363
     * @return float
364
     */
365
    public function float($key, $default = 0.0)
366
    {
367
        return floatval($this->input($key, $default));
368
    }
369
 
370
    /**
371
     * Retrieve input from the request as a Carbon instance.
372
     *
373
     * @param  string  $key
374
     * @param  string|null  $format
375
     * @param  string|null  $tz
376
     * @return \Illuminate\Support\Carbon|null
377
     *
378
     * @throws \Carbon\Exceptions\InvalidFormatException
379
     */
380
    public function date($key, $format = null, $tz = null)
381
    {
382
        if ($this->isNotFilled($key)) {
383
            return null;
384
        }
385
 
386
        if (is_null($format)) {
387
            return Date::parse($this->input($key), $tz);
388
        }
389
 
390
        return Date::createFromFormat($format, $this->input($key), $tz);
391
    }
392
 
393
    /**
394
     * Retrieve input from the request as an enum.
395
     *
396
     * @template TEnum
397
     *
398
     * @param  string  $key
399
     * @param  class-string<TEnum>  $enumClass
400
     * @return TEnum|null
401
     */
402
    public function enum($key, $enumClass)
403
    {
404
        if ($this->isNotFilled($key) ||
405
            ! function_exists('enum_exists') ||
406
            ! enum_exists($enumClass) ||
407
            ! method_exists($enumClass, 'tryFrom')) {
408
            return null;
409
        }
410
 
411
        return $enumClass::tryFrom($this->input($key));
412
    }
413
 
414
    /**
415
     * Retrieve input from the request as a collection.
416
     *
417
     * @param  array|string|null  $key
418
     * @return \Illuminate\Support\Collection
419
     */
420
    public function collect($key = null)
421
    {
422
        return collect(is_array($key) ? $this->only($key) : $this->input($key));
423
    }
424
 
425
    /**
426
     * Get a subset containing the provided keys with values from the input data.
427
     *
428
     * @param  array|mixed  $keys
429
     * @return array
430
     */
431
    public function only($keys)
432
    {
433
        $results = [];
434
 
435
        $input = $this->all();
436
 
437
        $placeholder = new stdClass;
438
 
439
        foreach (is_array($keys) ? $keys : func_get_args() as $key) {
440
            $value = data_get($input, $key, $placeholder);
441
 
442
            if ($value !== $placeholder) {
443
                Arr::set($results, $key, $value);
444
            }
445
        }
446
 
447
        return $results;
448
    }
449
 
450
    /**
451
     * Get all of the input except for a specified array of items.
452
     *
453
     * @param  array|mixed  $keys
454
     * @return array
455
     */
456
    public function except($keys)
457
    {
458
        $keys = is_array($keys) ? $keys : func_get_args();
459
 
460
        $results = $this->all();
461
 
462
        Arr::forget($results, $keys);
463
 
464
        return $results;
465
    }
466
 
467
    /**
468
     * Retrieve a query string item from the request.
469
     *
470
     * @param  string|null  $key
471
     * @param  string|array|null  $default
472
     * @return string|array|null
473
     */
474
    public function query($key = null, $default = null)
475
    {
476
        return $this->retrieveItem('query', $key, $default);
477
    }
478
 
479
    /**
480
     * Retrieve a request payload item from the request.
481
     *
482
     * @param  string|null  $key
483
     * @param  string|array|null  $default
484
     * @return string|array|null
485
     */
486
    public function post($key = null, $default = null)
487
    {
488
        return $this->retrieveItem('request', $key, $default);
489
    }
490
 
491
    /**
492
     * Determine if a cookie is set on the request.
493
     *
494
     * @param  string  $key
495
     * @return bool
496
     */
497
    public function hasCookie($key)
498
    {
499
        return ! is_null($this->cookie($key));
500
    }
501
 
502
    /**
503
     * Retrieve a cookie from the request.
504
     *
505
     * @param  string|null  $key
506
     * @param  string|array|null  $default
507
     * @return string|array|null
508
     */
509
    public function cookie($key = null, $default = null)
510
    {
511
        return $this->retrieveItem('cookies', $key, $default);
512
    }
513
 
514
    /**
515
     * Get an array of all of the files on the request.
516
     *
517
     * @return array
518
     */
519
    public function allFiles()
520
    {
521
        $files = $this->files->all();
522
 
523
        return $this->convertedFiles = $this->convertedFiles ?? $this->convertUploadedFiles($files);
524
    }
525
 
526
    /**
527
     * Convert the given array of Symfony UploadedFiles to custom Laravel UploadedFiles.
528
     *
529
     * @param  array  $files
530
     * @return array
531
     */
532
    protected function convertUploadedFiles(array $files)
533
    {
534
        return array_map(function ($file) {
535
            if (is_null($file) || (is_array($file) && empty(array_filter($file)))) {
536
                return $file;
537
            }
538
 
539
            return is_array($file)
540
                        ? $this->convertUploadedFiles($file)
541
                        : UploadedFile::createFromBase($file);
542
        }, $files);
543
    }
544
 
545
    /**
546
     * Determine if the uploaded data contains a file.
547
     *
548
     * @param  string  $key
549
     * @return bool
550
     */
551
    public function hasFile($key)
552
    {
553
        if (! is_array($files = $this->file($key))) {
554
            $files = [$files];
555
        }
556
 
557
        foreach ($files as $file) {
558
            if ($this->isValidFile($file)) {
559
                return true;
560
            }
561
        }
562
 
563
        return false;
564
    }
565
 
566
    /**
567
     * Check that the given file is a valid file instance.
568
     *
569
     * @param  mixed  $file
570
     * @return bool
571
     */
572
    protected function isValidFile($file)
573
    {
574
        return $file instanceof SplFileInfo && $file->getPath() !== '';
575
    }
576
 
577
    /**
578
     * Retrieve a file from the request.
579
     *
580
     * @param  string|null  $key
581
     * @param  mixed  $default
582
     * @return \Illuminate\Http\UploadedFile|\Illuminate\Http\UploadedFile[]|array|null
583
     */
584
    public function file($key = null, $default = null)
585
    {
586
        return data_get($this->allFiles(), $key, $default);
587
    }
588
 
589
    /**
590
     * Retrieve a parameter item from a given source.
591
     *
592
     * @param  string  $source
593
     * @param  string  $key
594
     * @param  string|array|null  $default
595
     * @return string|array|null
596
     */
597
    protected function retrieveItem($source, $key, $default)
598
    {
599
        if (is_null($key)) {
600
            return $this->$source->all();
601
        }
602
 
603
        if ($this->$source instanceof InputBag) {
604
            return $this->$source->all()[$key] ?? $default;
605
        }
606
 
607
        return $this->$source->get($key, $default);
608
    }
609
 
610
    /**
611
     * Dump the request items and end the script.
612
     *
613
     * @param  mixed  $keys
614
     * @return never
615
     */
616
    public function dd(...$keys)
617
    {
618
        $this->dump(...$keys);
619
 
620
        exit(1);
621
    }
622
 
623
    /**
624
     * Dump the items.
625
     *
626
     * @param  mixed  $keys
627
     * @return $this
628
     */
629
    public function dump($keys = [])
630
    {
631
        $keys = is_array($keys) ? $keys : func_get_args();
632
 
633
        VarDumper::dump(count($keys) > 0 ? $this->only($keys) : $this->all());
634
 
635
        return $this;
636
    }
637
}