1 | <?php |
||||
2 | |||||
3 | namespace VGirol\JsonApi\Requests; |
||||
4 | |||||
5 | use Illuminate\Contracts\Validation\Factory as ValidationFactory; |
||||
6 | use Illuminate\Contracts\Validation\Validator; |
||||
7 | use Illuminate\Foundation\Http\FormRequest; |
||||
8 | use Illuminate\Support\Arr; |
||||
9 | use Illuminate\Validation\Rule; |
||||
10 | use VGirol\JsonApi\Exceptions\JsonApiValidationException; |
||||
11 | use VGirol\JsonApi\Services\ValidateService; |
||||
12 | use VGirol\JsonApiConstant\Members; |
||||
13 | use VGirol\JsonApiStructure\Messages as JsonApiStructureMessages; |
||||
14 | |||||
15 | abstract class AbstractFormRequest extends FormRequest |
||||
16 | { |
||||
17 | protected $isCollection = false; |
||||
18 | |||||
19 | private $cachedRules = []; |
||||
20 | |||||
21 | /** |
||||
22 | * Get the validation rules that apply to the request. |
||||
23 | * |
||||
24 | * @return array |
||||
25 | */ |
||||
26 | abstract public function rules(): array; |
||||
27 | |||||
28 | /** |
||||
29 | * Get the prepared validation rules that apply to the request. |
||||
30 | * |
||||
31 | * @return array |
||||
32 | */ |
||||
33 | public function preparedRules(): array |
||||
34 | { |
||||
35 | if (empty($this->cachedRules)) { |
||||
36 | $this->cachedRules = \array_merge($this->getDefaultRules(), $this->getCustomRules()); |
||||
37 | } |
||||
38 | |||||
39 | return $this->cachedRules; |
||||
40 | } |
||||
41 | |||||
42 | /** |
||||
43 | * Undocumented function |
||||
44 | * |
||||
45 | * @return array |
||||
46 | */ |
||||
47 | public function preparedMessages(): array |
||||
48 | { |
||||
49 | return \array_merge($this->getDefaultMessages(), $this->getCustomMessages()); |
||||
50 | } |
||||
51 | |||||
52 | /** |
||||
53 | * Get the validated data from the request. |
||||
54 | * |
||||
55 | * @param string|null $name |
||||
56 | * |
||||
57 | * @return array|string|null |
||||
58 | */ |
||||
59 | public function validated(string $name = null) |
||||
60 | { |
||||
61 | $all = parent::validated(); |
||||
62 | |||||
63 | return $name ? Arr::get($all, $name) : $all; |
||||
64 | } |
||||
65 | |||||
66 | /** |
||||
67 | * Undocumented function |
||||
68 | * |
||||
69 | * @return void |
||||
70 | */ |
||||
71 | protected function validateRequestStructure(): void |
||||
72 | { |
||||
73 | // Validates request structure |
||||
74 | resolve(ValidateService::class) |
||||
75 | ->setMethod($this->method()) |
||||
0 ignored issues
–
show
|
|||||
76 | ->setRouteType($this->getRouteType()) |
||||
77 | ->setCollection($this->isCollection) |
||||
78 | ->validateRequestStructure($this); |
||||
79 | } |
||||
80 | |||||
81 | /** |
||||
82 | * Undocumented function |
||||
83 | * |
||||
84 | * @return int |
||||
85 | */ |
||||
86 | protected function getRouteType(): int |
||||
87 | { |
||||
88 | if ($this->routeIs('*.related.*')) { |
||||
0 ignored issues
–
show
The method
routeIs() does not exist on VGirol\JsonApi\Requests\AbstractFormRequest .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
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. ![]() |
|||||
89 | return ValidateService::ROUTE_RELATED; |
||||
90 | } |
||||
91 | if ($this->routeIs('*.relationship.*')) { |
||||
92 | return ValidateService::ROUTE_RELATIONSHIP; |
||||
93 | } |
||||
94 | |||||
95 | return ValidateService::ROUTE_MAIN; |
||||
96 | } |
||||
97 | |||||
98 | /** |
||||
99 | * Override \Illuminate\Foundation\Http\FormRequest::createDefaultValidator |
||||
100 | */ |
||||
101 | protected function createDefaultValidator(ValidationFactory $factory) |
||||
102 | { |
||||
103 | return $factory->make( |
||||
104 | $this->validationData(), |
||||
105 | $this->container->call([$this, 'preparedRules']), |
||||
0 ignored issues
–
show
It seems like
$this->container->call(a...this, 'preparedRules')) can also be of type callable ; however, parameter $rules of Illuminate\Validation\Factory::make() does only seem to accept array , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
106 | $this->preparedMessages(), |
||||
107 | $this->attributes() |
||||
108 | ); |
||||
109 | } |
||||
110 | |||||
111 | /** |
||||
112 | * Prepare the data for validation. |
||||
113 | * |
||||
114 | * @return void |
||||
115 | */ |
||||
116 | protected function prepareForValidation() |
||||
117 | { |
||||
118 | $this->validateRequestStructure(); |
||||
119 | } |
||||
120 | |||||
121 | /** |
||||
122 | * Override \Illuminate\Foundation\Http\FormRequest::failedValidation |
||||
123 | */ |
||||
124 | protected function failedValidation(Validator $validator) |
||||
125 | { |
||||
126 | throw (new JsonApiValidationException($validator)) |
||||
127 | ->errorBag($this->errorBag); |
||||
128 | } |
||||
129 | |||||
130 | /** |
||||
131 | * Undocumented function |
||||
132 | * |
||||
133 | * @return array |
||||
134 | */ |
||||
135 | protected function getCustomRules(): array |
||||
136 | { |
||||
137 | return $this->rules(); |
||||
138 | } |
||||
139 | |||||
140 | /** |
||||
141 | * Undocumented function |
||||
142 | * |
||||
143 | * @return array |
||||
144 | */ |
||||
145 | protected function getCustomMessages(): array |
||||
146 | { |
||||
147 | return $this->messages(); |
||||
148 | } |
||||
149 | |||||
150 | /** |
||||
151 | * Undocumented function |
||||
152 | * |
||||
153 | * @return boolean |
||||
154 | */ |
||||
155 | protected function isUpdate(): bool |
||||
156 | { |
||||
157 | return ($this->isMethod('PUT') || $this->isMethod('PATCH')); |
||||
0 ignored issues
–
show
The method
isMethod() does not exist on VGirol\JsonApi\Requests\AbstractFormRequest .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
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. ![]() |
|||||
158 | } |
||||
159 | |||||
160 | /** |
||||
161 | * Undocumented function |
||||
162 | * |
||||
163 | * @return array |
||||
164 | */ |
||||
165 | private function getDefaultRules(): array |
||||
166 | { |
||||
167 | $star = $this->isCollection ? '.*' : ''; |
||||
168 | |||||
169 | $idRule = [ |
||||
170 | // 'string' |
||||
171 | ]; |
||||
172 | if ($this->isUpdate()) { |
||||
173 | // array_push($idRule, 'required'); |
||||
174 | \array_push($idRule, Rule::in([$this->id])); |
||||
0 ignored issues
–
show
|
|||||
175 | } |
||||
176 | |||||
177 | $rules = [ |
||||
178 | Members::DATA . $star . '.' . Members::TYPE => [ |
||||
179 | // 'required', |
||||
180 | // 'string', |
||||
181 | Rule::in([jsonapiAliases()->getResourceType($this)]), |
||||
182 | ], |
||||
183 | Members::DATA . $star . '.' . Members::ID => $idRule |
||||
184 | ]; |
||||
185 | |||||
186 | return $rules; |
||||
187 | } |
||||
188 | |||||
189 | /** |
||||
190 | * Undocumented function |
||||
191 | * |
||||
192 | * @return array |
||||
193 | */ |
||||
194 | private function getDefaultMessages(): array |
||||
195 | { |
||||
196 | $star = $this->isCollection ? '.*' : ''; |
||||
197 | |||||
198 | return [ |
||||
199 | Members::DATA . $star . '.' . Members::TYPE . '.required' => |
||||
200 | // Messages::FORM_REQUEST_ERROR_MISSING_TYPE_MEMBER, |
||||
201 | JsonApiStructureMessages::RESOURCE_TYPE_MEMBER_IS_ABSENT, |
||||
202 | Members::DATA . $star . '.' . Members::ID . '.required' => |
||||
203 | // Messages::FORM_REQUEST_ERROR_MISSING_ID_MEMBER |
||||
204 | JsonApiStructureMessages::RESOURCE_ID_MEMBER_IS_ABSENT |
||||
205 | ]; |
||||
206 | } |
||||
207 | } |
||||
208 |
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.