Skip to content

Commit d0f4599

Browse files
committed
Add inputSet method to JsonApiRequest, refactor usage in controller
1 parent 4a78c3f commit d0f4599

File tree

2 files changed

+20
-64
lines changed

2 files changed

+20
-64
lines changed

src/Http/Controllers/JsonApiController.php

Lines changed: 10 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Huntie\JsonApi\Http\Controllers;
44

5-
use Validator;
65
use Huntie\JsonApi\Contracts\Model\IncludesRelatedResources;
76
use Huntie\JsonApi\Exceptions\InvalidRelationPathException;
87
use Huntie\JsonApi\Http\JsonApiResponse;
@@ -19,7 +18,6 @@
1918
use Illuminate\Foundation\Validation\ValidatesRequests;
2019
use Illuminate\Http\Response;
2120
use Illuminate\Routing\Controller;
22-
use Illuminate\Validation\ValidationException;
2321

2422
abstract class JsonApiController extends Controller
2523
{
@@ -62,11 +60,10 @@ public function __construct()
6260
public function indexAction($request, $query = null)
6361
{
6462
$records = $query ?: $this->model->newQuery();
65-
$params = $this->getRequestParameters($request);
66-
$this->validateIncludableRelations($params['include']);
63+
$this->validateIncludableRelations($request->inputSet('include'));
6764

68-
$records = $this->sortQuery($records, $params['sort']);
69-
$records = $this->filterQuery($records, $params['filter']);
65+
$records = $this->sortQuery($records, $request->inputSet('sort'));
66+
$records = $this->filterQuery($records, (array) $request->input('filter'));
7067

7168
try {
7269
$pageSize = min($this->model->getPerPage(), $request->input('page.size'));
@@ -77,7 +74,7 @@ public function indexAction($request, $query = null)
7774
return $this->error(Response::HTTP_BAD_REQUEST, 'Invalid query parameters');
7875
}
7976

80-
return new JsonApiResponse(new CollectionSerializer($records, $params['fields'], $params['include']));
77+
return new JsonApiResponse(new CollectionSerializer($records, $request->inputSet('fields'), $request->inputSet('include')));
8178
}
8279

8380
/**
@@ -109,10 +106,9 @@ public function storeAction($request)
109106
public function showAction($request, $record)
110107
{
111108
$record = $this->findModelInstance($record);
112-
$params = $this->getRequestParameters($request);
113-
$this->validateIncludableRelations($params['include']);
109+
$this->validateIncludableRelations($request->inputSet('include'));
114110

115-
return new JsonApiResponse(new ResourceSerializer($record, $params['fields'], $params['include']));
111+
return new JsonApiResponse(new ResourceSerializer($record, $request->inputSet('fields'), $request->inputSet('include')));
116112
}
117113

118114
/**
@@ -129,8 +125,8 @@ public function updateAction($request, $record)
129125
$record->fill((array) $request->input('data.attributes'));
130126
$record->save();
131127

132-
if ($relationships = $request->input('data.relationships')) {
133-
$this->updateResourceRelationships($record, (array) $relationships);
128+
if ($request->has('data.relationships')) {
129+
$this->updateResourceRelationships($record, (array) $request->input('data.relationships'));
134130
}
135131

136132
return new JsonApiResponse(new ResourceSerializer($record));
@@ -220,66 +216,16 @@ protected function findModelInstance($record)
220216
return $this->model->findOrFail($record);
221217
}
222218

223-
/**
224-
* Return any JSON API resource parameters from a request.
225-
*
226-
* @param Request $request
227-
*
228-
* @return array
229-
*/
230-
protected function getRequestParameters($request)
231-
{
232-
return [
233-
'fields' => $this->getRequestQuerySet($request, 'fields', '/^([A-Za-z]+.?)+[A-Za-z]+$/'),
234-
'include' => $this->getRequestQuerySet($request, 'include', '/^([A-Za-z]+.?)+[A-Za-z]+$/'),
235-
'sort' => $this->getRequestQuerySet($request, 'sort', '/[A-Za-z_]+/'),
236-
'filter' => (array) $request->input('filter'),
237-
];
238-
}
239-
240-
/**
241-
* Return any comma separated values in a request query field as an array.
242-
*
243-
* @param Request $request
244-
* @param string $key
245-
* @param string|null $validate Regular expression to test for each item
246-
*
247-
* @throws \Illuminate\Validation\ValidationException
248-
*
249-
* @return array
250-
*/
251-
protected function getRequestQuerySet($request, $key, $validate = null)
252-
{
253-
$values = preg_split('/,/', $request->input($key), null, PREG_SPLIT_NO_EMPTY);
254-
255-
$validator = Validator::make(['param' => $values], [
256-
'param.*' => 'required' . ($validate ? '|regex:' . $validate : ''),
257-
]);
258-
259-
if ($validator->fails()) {
260-
throw new ValidationException($validator, $this->error(
261-
Response::HTTP_BAD_REQUEST,
262-
sprintf('Invalid values for "%s" parameter', $key))
263-
);
264-
}
265-
266-
return $values;
267-
}
268-
269219
/**
270220
* Validate the requested included relationships against those that are
271221
* allowed on the requested resource type.
272222
*
273-
* @param array|null $relations
223+
* @param array $relations
274224
*
275225
* @throws InvalidRelationPathException
276226
*/
277-
protected function validateIncludableRelations($relations)
227+
protected function validateIncludableRelations(array $relations)
278228
{
279-
if (is_null($relations)) {
280-
return;
281-
}
282-
283229
foreach ($relations as $relation) {
284230
if (!$this->model instanceof IncludesRelatedResources || !in_array($relation, $this->model->getIncludableRelations())) {
285231
throw new InvalidRelationPathException($relation);

src/Http/Requests/JsonApiRequest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,4 +101,14 @@ public function response(array $errors)
101101
{
102102
return new JsonApiResponse($errors, Response::HTTP_UNPROCESSABLE_ENTITY);
103103
}
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+
}
104114
}

0 commit comments

Comments
 (0)