From a9feac5ffd8aa96c80ebc7178ba04ce2a8dc379d Mon Sep 17 00:00:00 2001 From: Toby Zerner Date: Tue, 23 Jul 2019 21:03:52 +0000 Subject: [PATCH] wip --- src/Adapter/AdapterInterface.php | 2 ++ src/Adapter/EloquentAdapter.php | 12 +++++++++-- src/Exception/ResourceNotFoundException.php | 7 ++++++- src/Handler/Create.php | 22 ++++++++++++++++++++- src/Handler/Index.php | 17 ++++++++++------ 5 files changed, 50 insertions(+), 10 deletions(-) diff --git a/src/Adapter/AdapterInterface.php b/src/Adapter/AdapterInterface.php index 8db6dd1..eea7885 100644 --- a/src/Adapter/AdapterInterface.php +++ b/src/Adapter/AdapterInterface.php @@ -34,6 +34,8 @@ interface AdapterInterface public function delete($model); + public function filterByIds($query, array $ids); + public function filterByAttribute($query, Attribute $attribute, $value); public function filterByHasOne($query, HasOne $relationship, array $ids); diff --git a/src/Adapter/EloquentAdapter.php b/src/Adapter/EloquentAdapter.php index 72d8ff9..63b92d4 100644 --- a/src/Adapter/EloquentAdapter.php +++ b/src/Adapter/EloquentAdapter.php @@ -117,6 +117,13 @@ class EloquentAdapter implements AdapterInterface $model->delete(); } + public function filterByIds($query, array $ids) + { + $key = $query->getModel()->getQualifiedKeyName(); + + $query->whereIn($key, $ids); + } + public function filterByAttribute($query, Attribute $field, $value) { $property = $this->getAttributeProperty($field); @@ -145,8 +152,9 @@ class EloquentAdapter implements AdapterInterface public function filterByHasOne($query, HasOne $field, array $ids) { - $property = $this->getRelationshipProperty($field); - $foreignKey = $query->getModel()->{$property}()->getQualifiedForeignKey(); + $relation = $query->getModel()->{$this->getRelationshipProperty($field)}(); + + $foreignKey = $relation->getQualifiedForeignKeyName(); $query->whereIn($foreignKey, $ids); } diff --git a/src/Exception/ResourceNotFoundException.php b/src/Exception/ResourceNotFoundException.php index bea9972..83b26a1 100644 --- a/src/Exception/ResourceNotFoundException.php +++ b/src/Exception/ResourceNotFoundException.php @@ -25,12 +25,17 @@ class ResourceNotFoundException extends \RuntimeException implements ErrorProvid return [ new Error( new Error\Title('Resource Not Found'), - new Error\Status('404'), + new Error\Status($this->getJsonApiStatus()), new Error\Detail($this->getMessage()) ) ]; } + public function getJsonApiStatus(): string + { + return '404'; + } + public function getType(): string { return $this->type; diff --git a/src/Handler/Create.php b/src/Handler/Create.php index a231cf3..b66c22d 100644 --- a/src/Handler/Create.php +++ b/src/Handler/Create.php @@ -32,11 +32,31 @@ class Create implements RequestHandlerInterface $model = $this->resource->getAdapter()->create(); + $data = $this->parseData($request->getParsedBody()); + + $adapter = $this->resource->getAdapter(); + + $this->assertFieldsExist($data); + + $this->assertFieldsWritable($data, $model, $request); + + $this->fillDefaultValues($data, $request); + + $this->loadRelatedResources($data, $request); + + $this->assertDataValid($data, $model, $request, true); + + $this->applyValues($data, $model, $request); + foreach ($schema->creatingCallbacks as $callback) { $callback($request, $model); } - $this->save($model, $request, true); + $adapter->save($model); + + $this->saveFields($data, $model, $request); + + $this->runSavedCallbacks($data, $model, $request); foreach ($schema->createdCallbacks as $callback) { $callback($request, $model); diff --git a/src/Handler/Index.php b/src/Handler/Index.php index ca2dd98..5764adf 100644 --- a/src/Handler/Index.php +++ b/src/Handler/Index.php @@ -40,11 +40,11 @@ class Index implements RequestHandlerInterface $query = $adapter->query(); foreach ($schema->scopes as $scope) { - $scope($request, $query); + $request = $scope($request, $query) ?: $request; } foreach ($schema->indexScopes as $scope) { - $scope($request, $query); + $request = $scope($request, $query) ?: $request; } if ($filter = $request->getAttribute('jsonApiFilter')) { @@ -57,7 +57,9 @@ class Index implements RequestHandlerInterface $paginationLinks = []; $members = [ - new Link\SelfLink($this->buildUrl($request)) + new Link\SelfLink($this->buildUrl($request)), + new Meta('offset', $offset), + new Meta('limit', $limit), ]; if ($offset > 0) { @@ -197,9 +199,7 @@ class Index implements RequestHandlerInterface } foreach ($filter as $name => $value) { - if (! isset($schema->fields[$name]) - || ! $schema->fields[$name]->filterable - ) { + if ($name !== 'id' && (! isset($schema->fields[$name]) || ! $schema->fields[$name]->filterable)) { throw new BadRequestException("Invalid filter [$name]", "filter[$name]"); } } @@ -261,6 +261,11 @@ class Index implements RequestHandlerInterface $adapter = $this->resource->getAdapter(); foreach ($filter as $name => $value) { + if ($name === 'id') { + $adapter->filterByIds($query, explode(',', $value)); + continue; + } + $field = $schema->fields[$name]; if ($field->filter) {