diff --git a/README.md b/README.md index 1361ba2..8b5203f 100644 --- a/README.md +++ b/README.md @@ -3,9 +3,8 @@ jsonapi jsonapi for phramework based on specifications from http://jsonapi.org/ -[![Coverage Status](https://coveralls.io/repos/phramework/jsonapi/badge.svg?branch=master&service=github)](https://coveralls.io/github/phramework/jsonapi?branch=master) -[![Build Status](https://travis-ci.org/phramework/jsonapi.svg?branch=master)](https://travis-ci.org/phramework/jsonapi) -[![Stories in Ready](https://badge.waffle.io/phramework/jsonapi.svg?label=ready&title=Ready)](http://waffle.io/phramework/jsonapi) +[![Coverage Status](https://coveralls.io/repos/phramework/jsonapi/badge.svg?branch=master&service=github)](https://coveralls.io/github/phramework/jsonapi?branch=3.x) +[![Build Status](https://travis-ci.org/phramework/jsonapi.svg?branch=3.x)](https://travis-ci.org/phramework/jsonapi) ## Usage diff --git a/src/Controller/Helper/RequestBodyQueue.php b/src/Controller/Helper/RequestBodyQueue.php index f2a2dfa..bedc1e9 100644 --- a/src/Controller/Helper/RequestBodyQueue.php +++ b/src/Controller/Helper/RequestBodyQueue.php @@ -23,6 +23,7 @@ * @license https://www.apache.org/licenses/LICENSE-2.0 Apache-2.0 * @author Xenofon Spafaridis * @since 3.0.0 + * @deprecated */ trait RequestBodyQueue { diff --git a/src/Controller/Helper/RequestWithBody.php b/src/Controller/Helper/RequestWithBody.php new file mode 100644 index 0000000..6375312 --- /dev/null +++ b/src/Controller/Helper/RequestWithBody.php @@ -0,0 +1,64 @@ + + * @since 3.0.0 + */ +class RequestWithBody +{ + public static function prepareData( + ServerRequestInterface $request, + int $bulkLimit = null + ) { + //todo figure out a permanent solution to have body as object instead of array, for every framework + $body = json_decode(json_encode($request->getParsedBody())); + + //Access request body primary data + $data = $body->data ?? new \stdClass(); + + /** + * @var bool + */ + $isBulk = true; + + //Treat all request data (bulk or not) as an array of resources + if (is_object($data) + || (is_array($data) && Util::isArrayAssoc($data)) + ) { + $isBulk = false; + $data = [$data]; + } + + //check bulk limit + if ($bulkLimit !== null && count($data) > $bulkLimit) { + throw new RequestException(sprintf( + 'Number of batch requests is exceeding the maximum of %s', + $bulkLimit + )); + } + + return [$data, $isBulk]; + } +} diff --git a/src/Controller/Patch.php b/src/Controller/Patch.php index 950403d..ab55e29 100644 --- a/src/Controller/Patch.php +++ b/src/Controller/Patch.php @@ -31,21 +31,18 @@ */ trait Patch { - use RequestBodyQueue; - //prototype public static function handlePatch( ServerRequestInterface $request, ResponseInterface $response, ResourceModel $model, - string $id, array $validationCallbacks = [], callable $viewCallback = null, - int $bulkLimit = null, + int $bulkLimit = 1, array $directives = [] ) : ResponseInterface { //Validate id using model's validator - $id = $model->getIdAttributeValidator()->parse($id); + //$id = $model->getIdAttributeValidator()->parse($id); //gather data as a queue diff --git a/src/Controller/Post.php b/src/Controller/Post.php index 185ca8f..c83a7f8 100644 --- a/src/Controller/Post.php +++ b/src/Controller/Post.php @@ -22,6 +22,8 @@ use Phramework\Exceptions\RequestException; use Phramework\Exceptions\Source\Pointer; use Phramework\JSONAPI\Controller\Helper\RequestBodyQueue; +use Phramework\JSONAPI\Controller\Helper\RequestWithBody; +use Phramework\JSONAPI\Controller\Helper\ResourceQueueItem; use Phramework\JSONAPI\Directive\Directive; use Phramework\JSONAPI\ResourceModel; use Phramework\Util\Util; @@ -38,8 +40,6 @@ */ trait Post { - use RequestBodyQueue; - //prototype public static function handlePost( ServerRequestInterface $request, @@ -50,32 +50,7 @@ public static function handlePost( int $bulkLimit = null, //todo decide 1 or null for default array $directives = [] ) : ResponseInterface { - //todo figure out a permanent solution to have body as object instead of array, for every framework - $body = json_decode(json_encode($request->getParsedBody())); - - //Access request body primary data - $data = $body->data ?? new \stdClass(); - - /** - * @var bool - */ - $isBulk = true; - - //Treat all request data (bulk or not) as an array of resources - if (is_object($data) - || (is_array($data) && Util::isArrayAssoc($data)) - ) { - $isBulk = false; - $data = [$data]; - } - - //check bulk limit - if ($bulkLimit !== null && count($data) > $bulkLimit) { - throw new RequestException(sprintf( - 'Number of batch requests is exceeding the maximum of %s', - $bulkLimit - )); - } + list($data, $isBulk) = RequestWithBody::prepareData($request, $bulkLimit); $typeValidator = (new EnumValidator([$model->getResourceType()])); @@ -119,11 +94,10 @@ public static function handlePost( $requestAttributes = $resource->attributes ?? new \stdClass(); $requestRelationships = $resource->relationships ?? new \stdClass(); - //todo use helper class - $queueItem = (object) [ - 'attributes' => $requestAttributes, - 'relationships' => $requestRelationships - ]; + $queueItem = new ResourceQueueItem( + $requestAttributes, + $requestRelationships + ); $requestQueue->push($queueItem); @@ -135,7 +109,7 @@ public static function handlePost( foreach ($requestQueue as $i => $q) { $validationModel->attributes ->setSource(new Pointer('/data/' . $i . '/attributes')) - ->parse($q->attributes); + ->parse($q->getAttributes()); } //on each call validation callback @@ -151,10 +125,13 @@ public static function handlePost( //process queue while (!$requestQueue->isEmpty()) { + /** + * @var ResourceQueueItem + */ $queueItem = $requestQueue->pop(); $id = $model->post( - $queueItem->attributes + $queueItem->getAttributes() ); Controller::assertUnknownError( @@ -163,7 +140,7 @@ public static function handlePost( ); //POST item's relationships - $relationships = $queueItem->relationships; + $relationships = $queueItem->getRelationships(); foreach ($relationships as $key => $relationship) { //Call post relationship method to post each of relationships pairs