1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Huntie\JsonApi\Http\Requests; |
4
|
|
|
|
5
|
|
|
use Huntie\JsonApi\Exceptions\HttpException; |
6
|
|
|
use Huntie\JsonApi\Http\JsonApiResponse; |
7
|
|
|
use Illuminate\Contracts\Validation\Validator; |
8
|
|
|
use Illuminate\Foundation\Http\FormRequest; |
9
|
|
|
use Illuminate\Http\Response; |
10
|
|
|
|
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() |
42
|
|
|
{ |
43
|
|
|
$this->beforeValidation(); |
44
|
|
|
|
45
|
|
|
$validator = parent::getValidatorInstance(); |
46
|
|
|
$validator->setRules(array_merge($this->baseRules, $this->rules, $validator->getRules())); |
47
|
|
|
|
48
|
|
|
return $validator; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Perform additional logic before the request input is validated. |
53
|
|
|
* |
54
|
|
|
* @throws HttpException |
55
|
|
|
*/ |
56
|
|
|
protected function beforeValidation() |
57
|
|
|
{ |
58
|
|
|
if (!$this->isJson()) { |
59
|
|
|
throw new HttpException('Unsupported Content-Type', Reponse::HTTP_UNSUPPORTED_MEDIA_TYPE); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
if ($this->exists('include') && config('jsonapi.enable_included_resources') === false) { |
63
|
|
|
throw new HttpException('Inclusion of related resources is not supported'); |
64
|
|
|
} |
65
|
|
|
} |
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) |
75
|
|
|
{ |
76
|
|
|
$errors = []; |
77
|
|
|
|
78
|
|
|
foreach ($validator->getMessageBag()->messages() as $field => $messages) { |
79
|
|
|
foreach ($messages as $message) { |
80
|
|
|
$errors[] = [ |
81
|
|
|
'source' => [ |
82
|
|
|
'pointer' => str_replace('.', '/', $field), |
83
|
|
|
], |
84
|
|
|
'title' => 'Invalid attribute', |
85
|
|
|
'detail' => str_replace('data.attributes.', '', $message), |
86
|
|
|
]; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return compact('errors'); |
91
|
|
|
} |
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) |
101
|
|
|
{ |
102
|
|
|
return new JsonApiResponse($errors, Response::HTTP_UNPROCESSABLE_ENTITY); |
103
|
|
|
} |
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 |
111
|
|
|
{ |
112
|
|
|
return preg_split('/,/', $this->input($key), null, PREG_SPLIT_NO_EMPTY); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|