1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\Enum\Laravel\Http; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use Illuminate\Support\Arr; |
7
|
|
|
use Spatie\Enum\Enumerable; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @internal This class is only used to get mixed into \Illuminate\Http\Request |
11
|
|
|
* |
12
|
|
|
* @mixin \Illuminate\Http\Request |
13
|
|
|
*/ |
14
|
|
|
final class EnumRequest |
15
|
|
|
{ |
16
|
|
|
const REQUEST_ROUTE = 'route'; |
17
|
|
|
const REQUEST_QUERY = 'query'; |
18
|
|
|
const REQUEST_REQUEST = 'request'; |
19
|
|
|
|
20
|
|
|
public function transformEnums(): Closure |
21
|
|
|
{ |
22
|
|
|
return function (array $transformations): void { |
23
|
|
|
if (isset($transformations[EnumRequest::REQUEST_ROUTE])) { |
24
|
|
|
$route = $this->route(); |
|
|
|
|
25
|
|
|
|
26
|
|
|
/** @var string|Enumerable $enumClass */ |
27
|
|
|
foreach ($transformations[EnumRequest::REQUEST_ROUTE] as $key => $enumClass) { |
28
|
|
|
if (! $route->hasParameter($key)) { |
29
|
|
|
continue; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
$route->setParameter( |
33
|
|
|
$key, |
34
|
|
|
forward_static_call( |
35
|
|
|
[$enumClass, 'make'], |
36
|
|
|
$route->parameter($key) |
37
|
|
|
) |
38
|
|
|
); |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
|
42
|
|
View Code Duplication |
if (isset($transformations[EnumRequest::REQUEST_QUERY])) { |
|
|
|
|
43
|
|
|
/** @var string|Enumerable $enumClass */ |
44
|
|
|
foreach ($transformations[EnumRequest::REQUEST_QUERY] as $key => $enumClass) { |
45
|
|
|
if (! $this->query->has($key)) { |
|
|
|
|
46
|
|
|
continue; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$this->query->set( |
50
|
|
|
$key, |
51
|
|
|
forward_static_call( |
52
|
|
|
[$enumClass, 'make'], |
53
|
|
|
$this->query->get($key) |
54
|
|
|
) |
55
|
|
|
); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
View Code Duplication |
if (isset($transformations[EnumRequest::REQUEST_REQUEST])) { |
|
|
|
|
60
|
|
|
/** @var string|Enumerable $enumClass */ |
61
|
|
|
foreach ($transformations[EnumRequest::REQUEST_REQUEST] as $key => $enumClass) { |
62
|
|
|
if (! $this->request->has($key)) { |
|
|
|
|
63
|
|
|
continue; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$this->request->set( |
67
|
|
|
$key, |
68
|
|
|
forward_static_call( |
69
|
|
|
[$enumClass, 'make'], |
70
|
|
|
$this->request->get($key) |
71
|
|
|
) |
72
|
|
|
); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** @var string|Enumerable $enumClass */ |
77
|
|
|
foreach (Arr::except($transformations, [EnumRequest::REQUEST_ROUTE, EnumRequest::REQUEST_QUERY, EnumRequest::REQUEST_REQUEST]) as $key => $enumClass) { |
78
|
|
|
if (! isset($this[$key])) { |
79
|
|
|
continue; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$this[$key] = forward_static_call( |
83
|
|
|
[$enumClass, 'make'], |
84
|
|
|
$this[$key] |
85
|
|
|
); |
86
|
|
|
} |
87
|
|
|
}; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.