8000 Various improvements. · josh-taylor/laravel-json-api@373addf · GitHub
[go: up one dir, main page]

Skip to content

Commit 373addf

Browse files
committed
Various improvements.
1 parent 264bdf9 commit 373addf

File tree

4 files changed

+93
-25
lines changed

4 files changed

+93
-25
lines changed

src/Http/Controllers/EloquentController.php

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,19 @@ class EloquentController extends JsonApiController
3737
{
3838

3939
/**
40-
* @var Model
40+
* @var HydratorInterface
4141
*/
42-
private $model;
42+
protected $hydrator;
4343

4444
/**
45-
* @var HydratorInterface
45+
* @var SearchInterface
4646
*/
47-
private $hydrator;
47+
protected $search;
4848

4949
/**
50-
* @var SearchInterface
50+
* @var Model
5151
*/
52-
private $search;
52+
private $model;
5353

5454
/**
5555
* EloquentController constructor.
@@ -88,8 +88,11 @@ public function index()
8888
public function create()
8989
{
9090
$model = $this->hydrate($this->getResource(), $this->model);
91+
$result = $this->commit($model);
9192

92-
if (!$this->commit($model)) {
93+
if ($result instanceof Response) {
94+
return $result;
95+
} elseif (!$result) {
9396
return $this->internalServerError();
9497
}
9598

@@ -116,8 +119,11 @@ public function read($resourceId)
116119
public function update($resourceId)
117120
{
118121
$model = $this->hydrate($this->getResource(), $this->getRecord());
122+
$result = $this->commit($model);
119123

120-
if (!$this->commit($model)) {
124+
if ($result instanceof Response) {
125+
return $result;
126+
} elseif (!$result) {
121127
return $this->internalServerError();
122128
}
123129

@@ -133,8 +139,11 @@ public function update($resourceId)
133139
public function delete($resourceId)
134140
{
135141
$model = $this->getRecord();
142+
$result = $this->destroy($model);
136143

137-
if (!$model->delete()) {
144+
if ($result instanceof Response) {
145+
return $result;
146+
} elseif (!$result) {
138147
return $this->internalServerError();
139148
}
140149

@@ -144,7 +153,7 @@ public function delete($resourceId)
144153
}
145154

146155
/**
147-
* @return Paginator|Collection
156+
* @return Paginator|Collection|Model|null
148157
*/
149158
protected function search()
150159
{
@@ -178,7 +187,7 @@ protected function hydrate(ResourceInterface $resource, Model $model)
178187
* post-save.
179188
*
180189
* @param Model $model
181-
* @return bool
190+
* @return bool|Response
182191
*/
183192
protected function commit(Model $model)
184193
{
@@ -192,7 +201,7 @@ protected function commit(Model $model)
192201
* post-delete.
193202
*
194203
* @param Model $model
195-
* @return bool
204+
* @return bool|Response
196205
*/
197206
protected function destroy(Model $model)
198207
{

src/Http/Requests/AbstractRequest.php

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,11 @@
3030
use CloudCreativity\LaravelJsonApi\Exceptions\RequestException;
3131
use Exception;
3232
use Illuminate\Http\Request as HttpRequest;
33+
use Illuminate\Http\Response;
3334
use Neomerx\JsonApi\Contracts\Encoder\Parameters\EncodingParametersInterface;
35+
use Neomerx\JsonApi\Document\Error;
3436
use Neomerx\JsonApi\Exceptions\JsonApiException;
37+
use RuntimeException;
3538
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
3639

3740
/**
@@ -45,6 +48,13 @@ abstract class AbstractRequest implements RequestHandlerInterface
4548
DecodesDocuments,
4649
ParsesQueryParameters;
4750

51+
/**
52+
* The resource type that this request handles.
53+
*
54+
* @var string
55+
*/
56+
protected $resourceType;
57+
4858
/**
4959
* A list of has-one relationships that are expected as endpoints.
5060
*
@@ -132,6 +142,18 @@ public function __construct(
132142
$this->authorizer = $authorizer;
133143
}
134144

145+
/**
146+
* @return string
147+
*/
148+
public function getResourceType()
149+
{
150+
if (empty($this->resourceType)) {
151+
throw new RuntimeException('The resourceType property must be set on: ' . static::class);
152+
}
153+
154+
return $this->resourceType;
155+
}
156+
135157
/**
136158
* Validate the given class instance.
137159
*
@@ -149,7 +171,7 @@ public function validate()
149171

150172
/** Do any pre-document authorization */
151173
if (!$this->authorizeBeforeValidation()) {
152-
throw $this->authorizer->denied();
174+
throw $this->denied();
153175
}
154176

155177
/** If a document is expected from the client, validate it. */
@@ -160,7 +182,7 @@ public function validate()
160182

161183
/** Do any post-document authorization. */
162184
if (!$this->authorizeAfterValidation()) {
163-
throw $this->authorizer->denied();
185+
throw $this->denied();
164186
}
165187

166188
/** Register the current request in the container. */
@@ -399,4 +421,14 @@ protected function allowedPagingParameters()
399421
return $param->getAllowedPagingParameters();
400422
}
401423

424+
/**
425+
* @return JsonApiException
426+
*/
427+
protected function denied()
428+
{
429+
$error = $this->authorizer ?
430+
$this->authorizer->denied() : new Error(null, null, Response::HTTP_FORBIDDEN);
431+
432+
return new JsonApiException($error, $error->getStatus());
433+
}
402434
}

src/Schema/EloquentSchema.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,14 +152,21 @@ protected function getModelAttributes(Model $model)
152152
{
153153
$attributes = [];
154154

155-
foreach ($this->attributes as $key) {
156-
$attributes[$this->keyForAttribute($key)] = $this->extractAttribute($model, $key);
155+
foreach ($this->attributes as $modelKey => $attributeKey) {
156+
if (is_numeric($modelKey)) {
157+
$modelKey = $attributeKey;
158+
$attributeKey = $this->keyForAttribute($attributeKey);
159+
}
160+
161+
$attributes[$attributeKey] = $this->extractAttribute($model, $modelKey);
157162
}
158163

159164
return $attributes;
160165
}
161166

162167
/**
168+
* Convert a model key into a resource attribute key.
169+
*
163170
* @param $modelKey
164171
* @return string
165172
*/

src/Testing/MakesJsonApiRequests.php

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ protected function assertJsonApiResponse(
8080
/**
8181
* Assert response is a JSON API resource index response.
8282
*
83-
* @param $resourceType
83+
* @param string|string[] $resourceType
8484
* @param string $contentType
8585
* @return $this
8686
*/
@@ -196,15 +196,13 @@ protected function seeStatusCode($expected)
196196
/**
197197
* See that there is a collection of resources as primary data.
198198
*
199-
* @param $resourceType
199+
* @param string|string[] $resourceType
200200
* @param bool $allowEmpty
201201
*/
202202
protected function seeDataCollection($resourceType, $allowEmpty = true)
203203
{
204204
$this->seeJsonStructure([
205-
Keys::KEYWORD_DATA => [
206-
'*' => [Keys::KEYWORD_TYPE],
207-
],
205+
Keys::KEYWORD_DATA,
208206
]);
209207

210208
$collection = $this->decodeResponseJson()[Keys::KEYWORD_DATA];
@@ -213,13 +211,24 @@ protected function seeDataCollection($resourceType, $allowEmpty = true)
213211
PHPUnit::assertNotEmpty($collection, 'Data collection is empty');
214212
}
215213

214+
$expected = array_combine((array) $resourceType, (array) $resourceType);
215+
$actual = [];
216+
216217
/** @var array $resource */
217218
foreach ($collection as $resource) {
219+
220+
if (!isset($resource[Keys::KEYWORD_TYPE])) {
221+
PHPUnit::fail('Encountered a resource without a type key.');
222+
}
223+
218224
$type = $resource[Keys::KEYWORD_TYPE];
219-
if ($resourceType !== $type) {
220-
PHPUnit::fail('Unexpected resource type in collection: ' . $type);
225+
226+
if (!isset($actual[$type])) {
227+
$actual[$type] = $type;
221228
}
222229
}
230+
231+
$this->assertEquals($expected, $actual, 'Unexpected resource types in data collection.');
223232
}
224233

225234
/**
@@ -270,10 +279,21 @@ protected function seeDataResource(array $expected)
270279
}
271280

272281
/** Have we got the correct attributes? */
273-
PHPUnit::assertArraySubset($attributes, $data[Keys::KEYWORD_ATTRIBUTES], false, 'Unexpected resource attributes');
282+
PHPUnit::assertArraySubset(
283+
$attributes,
284+
$data[Keys::KEYWORD_ATTRIBUTES],
285+
false,
286+
"Unexpected resource attributes\n" . json_encode($data[Keys::KEYWORD_ATTRIBUTES])
287+
);
288+
274289
/** Have we got the correct relationships? */
275290
$actualRelationships = isset($data[Keys::KEYWORD_RELATIONSHIPS]) ? $data[Keys::KEYWORD_RELATIONSHIPS] : [];
276-
PHPUnit::assertArraySubset($relationships, $actualRelationships, false, 'Unexpected resource relationships');
291+
PHPUnit::assertArraySubset(
292+
$relationships,
293+
$actualRelationships,
294+
false,
295+
"Unexpected resource relationships\n" . json_encode($actualRelationships)
296+
);
277297
}
278298

279299
/**

0 commit comments

Comments
 (0)
0