1 | <?php |
||
11 | abstract class JsonApiRequest extends FormRequest |
||
12 | { |
||
13 | /** |
||
14 | * Base validation rules for all JSON API requests. |
||
15 | * |
||
16 | * @var array |
||
17 | */ |
||
18 | private $baseRules = [ |
||
19 | 'fields' => 'regex:^(?:[A-Za-z]+[A-Za-z_.\-,]*)*[A-Za-z]+$', |
||
20 | 'include' => 'regex:^(?:[A-Za-z]+[A-Za-z_.\-,]*)*[A-Za-z]+$', |
||
21 | 'sort' => 'regex:^-?(?:[A-Za-z]+[A-Za-z_.\-,]*)*[A-Za-z]+$', |
||
22 | 'filter' => 'array', |
||
23 | 'filter.*' => 'alpha_dash', |
||
24 | 'page' => 'array', |
||
25 | 'page.size' => 'integer', |
||
26 | 'page.number' => 'integer', |
||
27 | ]; |
||
28 | |||
29 | /** |
||
30 | * Base validation rules for the individual request type. |
||
31 | * |
||
32 | * @var array |
||
33 | */ |
||
34 | protected $rules = []; |
||
35 | |||
36 | /** |
||
37 | * Get the validator instance for the request. |
||
38 | * |
||
39 | * @return Validator |
||
40 | */ |
||
41 | protected function getValidatorInstance() |
||
50 | |||
51 | /** |
||
52 | * Perform additional logic before the request input is validated. |
||
53 | * |
||
54 | * @throws HttpException |
||
55 | */ |
||
56 | protected function beforeValidation() |
||
66 | |||
67 | /** |
||
68 | * Format the errors from the given Validator instance. |
||
69 | * |
||
70 | * @param Validator $validator |
||
71 | * |
||
72 | * @return array |
||
73 | */ |
||
74 | protected function formatErrors(Validator $validator) |
||
92 | |||
93 | /** |
||
94 | * Get the proper failed validation response for the request. |
||
95 | * |
||
96 | * @param array $errors |
||
97 | * |
||
98 | * @return JsonApiResponse |
||
99 | */ |
||
100 | public function response(array $errors) |
||
104 | |||
105 | /** |
||
106 | * Return an input field containing comma separated values as an array. |
||
107 | * |
||
108 | * @param string $key |
||
109 | */ |
||
110 | public function inputSet(string $key): array |
||
114 | } |
||
115 |