Skip to content

Commit

Permalink
Adding url UrlRule, Controller classes. Support handling of empty res…
Browse files Browse the repository at this point in the history
…ources
  • Loading branch information
tuyakhov committed Jul 21, 2018
1 parent 42aa49b commit a462324
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 10 deletions.
32 changes: 32 additions & 0 deletions src/Controller.php
@@ -0,0 +1,32 @@
<?php
/**
* @author Anton Tuyakhov <atuyakhov@gmail.com>
*/

namespace tuyakhov\jsonapi;


use yii\filters\ContentNegotiator;
use yii\helpers\ArrayHelper;
use yii\web\Response;

class Controller extends \yii\rest\Controller
{
public $serializer = 'tuyakhov\jsonapi\Serializer';

/**
* @inheritdoc
*/
public function behaviors()
{
return ArrayHelper::merge(parent::behaviors(), [
'contentNegotiator' => [
'class' => ContentNegotiator::className(),
'formats' => [
'application/vnd.api+json' => Response::FORMAT_JSON,
],
]
]);
}

}
18 changes: 12 additions & 6 deletions src/JsonApiResponseFormatter.php
Expand Up @@ -8,6 +8,7 @@
use yii\base\Component;
use yii\helpers\ArrayHelper;
use yii\helpers\Json;
use yii\helpers\Url;
use yii\web\ErrorHandler;
use yii\web\Response;
use yii\web\ResponseFormatterInterface;
Expand Down Expand Up @@ -52,11 +53,17 @@ class JsonApiResponseFormatter extends Component implements ResponseFormatterInt
public function format($response)
{
$response->getHeaders()->set('Content-Type', 'application/vnd.api+json; charset=UTF-8');
$apiDocument = [
'data' => $response->data,
'links' => [
'self' => Url::current([], true)
]
];
$options = $this->encodeOptions;
if ($this->prettyPrint) {
$options |= JSON_PRETTY_PRINT;
}
if ($response->data !== null) {
$options = $this->encodeOptions;
if ($this->prettyPrint) {
$options |= JSON_PRETTY_PRINT;
}
$apiDocument = $response->data;
if ($response->isClientError || $response->isServerError) {
if (ArrayHelper::isAssociative($response->data)) {
Expand All @@ -76,8 +83,7 @@ public function format($response)
}
$apiDocument = ['errors' => $formattedErrors];
}

$response->content = Json::encode($apiDocument, $options);
}
$response->content = Json::encode($apiDocument, $options);
}
}
32 changes: 32 additions & 0 deletions src/UrlRule.php
@@ -0,0 +1,32 @@
<?php
/**
* @author Anton Tuyakhov <atuyakhov@gmail.com>
*/

namespace tuyakhov\jsonapi;


/**
* UrlRule is provided to simplify the creation of URL rules for JSON API support.
* @package tuyakhov\jsonapi
*/
class UrlRule extends \yii\rest\UrlRule
{
/**
* @inheritdoc
*/
public function init()
{
$this->tokens = array_merge($this->tokens, array_merge([
'{relationship}' => '<name:\w+>'
]));
$this->patterns = array_merge($this->patterns, [
'DELETE {id}/relationships/{relationship}' => 'delete-relationship',
'POST,PATCH {id}/relationships/{relationship}' => 'update-relationship',
'GET {id}/{relationship}' => 'view-related',
'{id}/{relationship}' => 'options'
]);
parent::init();
}

}
3 changes: 3 additions & 0 deletions tests/JsonApiResponseFormatterTest.php
Expand Up @@ -8,6 +8,7 @@
use tuyakhov\jsonapi\JsonApiResponseFormatter;
use tuyakhov\jsonapi\Serializer;
use tuyakhov\jsonapi\tests\data\ResourceModel;
use yii\base\Controller;
use yii\helpers\Json;
use yii\web\Response;
use yii\web\ServerErrorHttpException;
Expand All @@ -16,6 +17,7 @@ class JsonApiResponseFormatterTest extends TestCase
{
public function testFormatException()
{
\Yii::$app->controller = new Controller('test', \Yii::$app);
$formatter = new JsonApiResponseFormatter();
$exception = new ServerErrorHttpException('Server error');
$response = new Response();
Expand All @@ -42,6 +44,7 @@ public function testFormatException()

public function testFormModelError()
{
\Yii::$app->controller = new Controller('test', \Yii::$app);
$formatter = new JsonApiResponseFormatter();
$exception = new ServerErrorHttpException('Server error');
$response = new Response();
Expand Down
5 changes: 1 addition & 4 deletions tests/actions/UpdateActionTest.php
@@ -1,9 +1,6 @@
<?php
/**
* Created by PhpStorm.
* User: anton
* Date: 08/03/2017
* Time: 19:07
* @author Anton Tuyakhov <atuyakhov@gmail.com>
*/

namespace tuyakhov\jsonapi\tests\actions;
Expand Down

0 comments on commit a462324

Please sign in to comment.