From a46232433b183b0c5922bac6e954693752256196 Mon Sep 17 00:00:00 2001 From: Anton Tuyakhov Date: Sat, 21 Jul 2018 12:49:41 +0200 Subject: [PATCH] Adding url UrlRule, Controller classes. Support handling of empty resources --- src/Controller.php | 32 ++++++++++++++++++++++++++ src/JsonApiResponseFormatter.php | 18 ++++++++++----- src/UrlRule.php | 32 ++++++++++++++++++++++++++ tests/JsonApiResponseFormatterTest.php | 3 +++ tests/actions/UpdateActionTest.php | 5 +--- 5 files changed, 80 insertions(+), 10 deletions(-) create mode 100644 src/Controller.php create mode 100644 src/UrlRule.php diff --git a/src/Controller.php b/src/Controller.php new file mode 100644 index 0000000..7286eba --- /dev/null +++ b/src/Controller.php @@ -0,0 +1,32 @@ + + */ + +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, + ], + ] + ]); + } + +} \ No newline at end of file diff --git a/src/JsonApiResponseFormatter.php b/src/JsonApiResponseFormatter.php index fb4d34b..36612e9 100644 --- a/src/JsonApiResponseFormatter.php +++ b/src/JsonApiResponseFormatter.php @@ -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; @@ -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)) { @@ -76,8 +83,7 @@ public function format($response) } $apiDocument = ['errors' => $formattedErrors]; } - - $response->content = Json::encode($apiDocument, $options); } + $response->content = Json::encode($apiDocument, $options); } } diff --git a/src/UrlRule.php b/src/UrlRule.php new file mode 100644 index 0000000..a5206fd --- /dev/null +++ b/src/UrlRule.php @@ -0,0 +1,32 @@ + + */ + +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}' => '' + ])); + $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(); + } + +} \ No newline at end of file diff --git a/tests/JsonApiResponseFormatterTest.php b/tests/JsonApiResponseFormatterTest.php index 90d790c..8b0b4dc 100644 --- a/tests/JsonApiResponseFormatterTest.php +++ b/tests/JsonApiResponseFormatterTest.php @@ -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; @@ -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(); @@ -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(); diff --git a/tests/actions/UpdateActionTest.php b/tests/actions/UpdateActionTest.php index 4835ecb..988dcb3 100644 --- a/tests/actions/UpdateActionTest.php +++ b/tests/actions/UpdateActionTest.php @@ -1,9 +1,6 @@ */ namespace tuyakhov\jsonapi\tests\actions;