Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/Controller/Helper/RequestBodyQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
* @license https://www.apache.org/licenses/LICENSE-2.0 Apache-2.0
* @author Xenofon Spafaridis <nohponex@gmail.com>
* @since 3.0.0
* @deprecated
*/
trait RequestBodyQueue
{
Expand Down
64 changes: 64 additions & 0 deletions src/Controller/Helper/RequestWithBody.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php
declare(strict_types=1);
/*
* Copyright 2015-2016 Xenofon Spafaridis
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace Phramework\JSONAPI\Controller\Helper;

use Phramework\Exceptions\RequestException;
use Phramework\Util\Util;
use Psr\Http\Message\ServerRequestInterface;

/**
* @license https://www.apache.org/licenses/LICENSE-2.0 Apache-2.0
* @author Xenofon Spafaridis <nohponex@gmail.com>
* @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];
}
}
7 changes: 2 additions & 5 deletions src/Controller/Patch.php
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
49 changes: 13 additions & 36 deletions src/Controller/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -38,8 +40,6 @@
*/
trait Post
{
use RequestBodyQueue;

//prototype
public static function handlePost(
ServerRequestInterface $request,
Expand All @@ -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()]));

Expand Down Expand Up @@ -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);

Expand All @@ -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
Expand All @@ -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(
Expand All @@ -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
Expand Down