|
1
|
|
|
<?php namespace Pz\LaravelDoctrine\Rest; |
|
2
|
|
|
|
|
3
|
|
|
use Illuminate\Validation\ValidationException; |
|
4
|
|
|
use Pz\Doctrine\Rest\Contracts\RestRequestContract; |
|
5
|
|
|
use Pz\Doctrine\Rest\Exceptions\RestException; |
|
6
|
|
|
|
|
7
|
|
|
use Pz\Doctrine\Rest\RestResponse; |
|
8
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
9
|
|
|
use Illuminate\Foundation\Http\FormRequest; |
|
10
|
|
|
use Illuminate\Contracts\Validation\Validator; |
|
11
|
|
|
|
|
12
|
|
|
class RestRequest extends FormRequest implements RestRequestContract |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var bool |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $isRelationships = false; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @return array |
|
21
|
|
|
*/ |
|
22
|
8 |
|
public function rules() |
|
23
|
|
|
{ |
|
24
|
|
|
return [ |
|
25
|
8 |
|
'filter' => 'sometimes|required', |
|
26
|
|
|
'include' => 'sometimes|required', |
|
27
|
|
|
'exclude' => 'sometimes|required|array', |
|
28
|
|
|
'fields' => 'sometimes|required|array', |
|
29
|
|
|
'sort' => 'sometimes|required|string', |
|
30
|
|
|
|
|
31
|
|
|
'page' => 'sometimes|required|array', |
|
32
|
|
|
'page.number' => 'sometimes|required|numeric', |
|
33
|
|
|
'page.size' => 'sometimes|required|numeric', |
|
34
|
|
|
'page.limit' => 'sometimes|required|numeric', |
|
35
|
|
|
'page.offset' => 'sometimes|required|numeric', |
|
36
|
|
|
]; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param null|bool $value |
|
41
|
|
|
* |
|
42
|
|
|
* @return bool|null |
|
43
|
|
|
*/ |
|
44
|
7 |
|
public function isRelationships($value = null) |
|
45
|
|
|
{ |
|
46
|
7 |
|
if ($value !== null) { |
|
47
|
1 |
|
$this->isRelationships = $value; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
7 |
|
return $this->isRelationships; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @return array |
|
55
|
|
|
* @throws RestException |
|
56
|
|
|
*/ |
|
57
|
4 |
|
public function getData() |
|
58
|
|
|
{ |
|
59
|
4 |
|
if ((null === $data = $this->get('data')) || !is_array($data)) { |
|
60
|
1 |
|
throw RestException::missingRootData(); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
4 |
|
return $data; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @return array|null |
|
68
|
|
|
*/ |
|
69
|
4 |
|
public function getOrderBy() |
|
70
|
|
|
{ |
|
71
|
4 |
|
if ($sort = $this->input('sort')) { |
|
72
|
1 |
|
$fields = explode(',', $sort); |
|
73
|
1 |
|
$orderBy = []; |
|
74
|
|
|
|
|
75
|
1 |
|
foreach ($fields as $field) { |
|
76
|
1 |
|
if (empty($field)) continue; |
|
77
|
|
|
|
|
78
|
1 |
|
$direction = 'ASC'; |
|
79
|
1 |
|
if ($field[0] === '-') { |
|
80
|
1 |
|
$field = substr($field, 1); |
|
81
|
1 |
|
$direction = 'DESC'; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
1 |
|
$orderBy[$field] = $direction; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
1 |
|
return $orderBy; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
4 |
|
return null; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @return null|int |
|
95
|
|
|
*/ |
|
96
|
4 |
|
public function getStart() |
|
97
|
|
|
{ |
|
98
|
4 |
|
if (null !== ($limit = $this->getLimit())) { |
|
99
|
1 |
|
if ($number = $this->input('page.number')) { |
|
100
|
1 |
|
return ($number - 1) * $limit; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
1 |
|
return $this->input('page.offset', 0); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
4 |
|
return null; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @return int|null |
|
111
|
|
|
*/ |
|
112
|
4 |
|
public function getLimit() |
|
113
|
|
|
{ |
|
114
|
4 |
|
if ($this->has('page')) { |
|
115
|
1 |
|
if ($this->has('page.number')) { |
|
116
|
1 |
|
return $this->input('page.size', static::DEFAULT_LIMIT); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
1 |
|
return $this->input('page.limit', static::DEFAULT_LIMIT); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
4 |
|
return null; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* @return \Illuminate\Routing\Route|object|string |
|
127
|
|
|
*/ |
|
128
|
7 |
|
public function getId() |
|
129
|
|
|
{ |
|
130
|
7 |
|
return $this->route('id'); |
|
|
|
|
|
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* @return array|null |
|
135
|
|
|
*/ |
|
136
|
7 |
|
public function getExclude() |
|
137
|
|
|
{ |
|
138
|
7 |
|
return $this->input('exclude'); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* @return array|null |
|
143
|
|
|
*/ |
|
144
|
7 |
|
public function getInclude() |
|
145
|
|
|
{ |
|
146
|
7 |
|
$include = @explode(',', $this->input('include')); |
|
147
|
|
|
|
|
148
|
7 |
|
if (!is_array($include)) { |
|
|
|
|
|
|
149
|
|
|
RestException::invalidInclude(); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
7 |
|
return $include; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
7 |
|
public function getFields() |
|
156
|
|
|
{ |
|
157
|
7 |
|
return $this->input('fields'); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* @return array|string|null |
|
162
|
|
|
*/ |
|
163
|
4 |
|
public function getFilter() |
|
164
|
|
|
{ |
|
165
|
4 |
|
return $this->input('filter'); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* @return bool |
|
170
|
|
|
*/ |
|
171
|
8 |
|
protected function passesAuthorization() |
|
172
|
|
|
{ |
|
173
|
8 |
|
return true; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* @param Validator $validator |
|
178
|
|
|
* |
|
179
|
|
|
* @throws ValidationException |
|
180
|
|
|
*/ |
|
181
|
|
|
protected function failedValidation(Validator $validator) |
|
182
|
|
|
{ |
|
183
|
|
|
$exception = RestException::create(Response::HTTP_UNPROCESSABLE_ENTITY, 'Validation failed'); |
|
184
|
|
|
foreach ($validator->errors()->getMessages() as $pointer => $messages) { |
|
185
|
|
|
foreach ($messages as $message) { |
|
186
|
|
|
$exception->errorValidation($pointer, $message); |
|
187
|
|
|
} |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
throw new ValidationException($validator, RestResponse::exception($exception)); |
|
191
|
|
|
} |
|
192
|
|
|
} |
|
193
|
|
|
|