|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace DuncanMcClean\Cargo\Http\Requests; |
| 4 | + |
| 5 | +use DuncanMcClean\Cargo\Exceptions\FormRequestDoesNotExist; |
| 6 | +use Illuminate\Contracts\Validation\Validator; |
| 7 | +use Illuminate\Foundation\Http\FormRequest; |
| 8 | + |
| 9 | +abstract class CustomizableFormRequest extends FormRequest |
| 10 | +{ |
| 11 | + private ?FormRequest $customFormRequestInstance = null; |
| 12 | + |
| 13 | + public function authorize(): bool |
| 14 | + { |
| 15 | + if ($this->hasCustomFormRequest()) { |
| 16 | + return $this->resolveCustomFormRequest()->authorize(); |
| 17 | + } |
| 18 | + |
| 19 | + return true; |
| 20 | + } |
| 21 | + |
| 22 | + public function rules(): array |
| 23 | + { |
| 24 | + if ($this->hasCustomFormRequest()) { |
| 25 | + return $this->resolveCustomFormRequest()->rules(); |
| 26 | + } |
| 27 | + |
| 28 | + return []; |
| 29 | + } |
| 30 | + |
| 31 | + public function messages(): array |
| 32 | + { |
| 33 | + if ($this->hasCustomFormRequest()) { |
| 34 | + return $this->resolveCustomFormRequest()->messages(); |
| 35 | + } |
| 36 | + |
| 37 | + return []; |
| 38 | + } |
| 39 | + |
| 40 | + public function attributes(): array |
| 41 | + { |
| 42 | + if ($this->hasCustomFormRequest()) { |
| 43 | + return $this->resolveCustomFormRequest()->attributes(); |
| 44 | + } |
| 45 | + |
| 46 | + return []; |
| 47 | + } |
| 48 | + |
| 49 | + protected function prepareForValidation(): void |
| 50 | + { |
| 51 | + if (! $this->hasCustomFormRequest()) { |
| 52 | + return; |
| 53 | + } |
| 54 | + |
| 55 | + $customRequest = $this->resolveCustomFormRequest(); |
| 56 | + |
| 57 | + if (method_exists($customRequest, 'prepareForValidation')) { |
| 58 | + $customRequest->prepareForValidation(); |
| 59 | + $this->merge($customRequest->all()); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + public function withValidator(Validator $validator): void |
| 64 | + { |
| 65 | + if (! $this->hasCustomFormRequest()) { |
| 66 | + return; |
| 67 | + } |
| 68 | + |
| 69 | + $customRequest = $this->resolveCustomFormRequest(); |
| 70 | + |
| 71 | + if (method_exists($customRequest, 'withValidator')) { |
| 72 | + $customRequest->withValidator($validator); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + protected function passedValidation(): void |
| 77 | + { |
| 78 | + if (! $this->hasCustomFormRequest()) { |
| 79 | + return; |
| 80 | + } |
| 81 | + |
| 82 | + $customRequest = $this->resolveCustomFormRequest(); |
| 83 | + |
| 84 | + if (method_exists($customRequest, 'passedValidation')) { |
| 85 | + $customRequest->passedValidation(); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + protected function hasCustomFormRequest(): bool |
| 90 | + { |
| 91 | + return $this->has('_request'); |
| 92 | + } |
| 93 | + |
| 94 | + protected function resolveCustomFormRequest(): FormRequest |
| 95 | + { |
| 96 | + if ($this->customFormRequestInstance) { |
| 97 | + return $this->customFormRequestInstance; |
| 98 | + } |
| 99 | + |
| 100 | + $formRequest = decrypt($this->input('_request')); |
| 101 | + |
| 102 | + if (! class_exists($formRequest)) { |
| 103 | + throw new FormRequestDoesNotExist("Form Request [{$formRequest}] does not exist."); |
| 104 | + } |
| 105 | + |
| 106 | + return $this->customFormRequestInstance = app($formRequest, [ |
| 107 | + $this->query(), |
| 108 | + $this->all(), |
| 109 | + $this->attributes->all(), |
| 110 | + $this->cookies->all(), |
| 111 | + $this->files->all(), |
| 112 | + $this->server->all(), |
| 113 | + $this->content, |
| 114 | + ]); |
| 115 | + } |
| 116 | +} |
0 commit comments