Skip to content

Commit a462324

Browse files
committed
Adding url UrlRule, Controller classes. Support handling of empty resources
1 parent 42aa49b commit a462324

File tree

5 files changed

+80
-10
lines changed

5 files changed

+80
-10
lines changed

src/Controller.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
/**
3+
* @author Anton Tuyakhov <atuyakhov@gmail.com>
4+
*/
5+
6+
namespace tuyakhov\jsonapi;
7+
8+
9+
use yii\filters\ContentNegotiator;
10+
use yii\helpers\ArrayHelper;
11+
use yii\web\Response;
12+
13+
class Controller extends \yii\rest\Controller
14+
{
15+
public $serializer = 'tuyakhov\jsonapi\Serializer';
16+
17+
/**
18+
* @inheritdoc
19+
*/
20+
public function behaviors()
21+
{
22+
return ArrayHelper::merge(parent::behaviors(), [
23+
'contentNegotiator' => [
24+
'class' => ContentNegotiator::className(),
25+
'formats' => [
26+
'application/vnd.api+json' => Response::FORMAT_JSON,
27+
],
28+
]
29+
]);
30+
}
31+
32+
}

src/JsonApiResponseFormatter.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use yii\base\Component;
99
use yii\helpers\ArrayHelper;
1010
use yii\helpers\Json;
11+
use yii\helpers\Url;
1112
use yii\web\ErrorHandler;
1213
use yii\web\Response;
1314
use yii\web\ResponseFormatterInterface;
@@ -52,11 +53,17 @@ class JsonApiResponseFormatter extends Component implements ResponseFormatterInt
5253
public function format($response)
5354
{
5455
$response->getHeaders()->set('Content-Type', 'application/vnd.api+json; charset=UTF-8');
56+
$apiDocument = [
57+
'data' => $response->data,
58+
'links' => [
59+
'self' => Url::current([], true)
60+
]
61+
];
62+
$options = $this->encodeOptions;
63+
if ($this->prettyPrint) {
64+
$options |= JSON_PRETTY_PRINT;
65+
}
5566
if ($response->data !== null) {
56-
$options = $this->encodeOptions;
57-
if ($this->prettyPrint) {
58-
$options |= JSON_PRETTY_PRINT;
59-
}
6067
$apiDocument = $response->data;
6168
if ($response->isClientError || $response->isServerError) {
6269
if (ArrayHelper::isAssociative($response->data)) {
@@ -76,8 +83,7 @@ public function format($response)
7683
}
7784
$apiDocument = ['errors' => $formattedErrors];
7885
}
79-
80-
$response->content = Json::encode($apiDocument, $options);
8186
}
87+
$response->content = Json::encode($apiDocument, $options);
8288
}
8389
}

src/UrlRule.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
/**
3+
* @author Anton Tuyakhov <atuyakhov@gmail.com>
4+
*/
5+
6+
namespace tuyakhov\jsonapi;
7+
8+
9+
/**
10+
* UrlRule is provided to simplify the creation of URL rules for JSON API support.
11+
* @package tuyakhov\jsonapi
12+
*/
13+
class UrlRule extends \yii\rest\UrlRule
14+
{
15+
/**
16+
* @inheritdoc
17+
*/
18+
public function init()
19+
{
20+
$this->tokens = array_merge($this->tokens, array_merge([
21+
'{relationship}' => '<name:\w+>'
22+
]));
23+
$this->patterns = array_merge($this->patterns, [
24+
'DELETE {id}/relationships/{relationship}' => 'delete-relationship',
25+
'POST,PATCH {id}/relationships/{relationship}' => 'update-relationship',
26+
'GET {id}/{relationship}' => 'view-related',
27+
'{id}/{relationship}' => 'options'
28+
]);
29+
parent::init();
30+
}
31+
32+
}

tests/JsonApiResponseFormatterTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use tuyakhov\jsonapi\JsonApiResponseFormatter;
99
use tuyakhov\jsonapi\Serializer;
1010
use tuyakhov\jsonapi\tests\data\ResourceModel;
11+
use yii\base\Controller;
1112
use yii\helpers\Json;
1213
use yii\web\Response;
1314
use yii\web\ServerErrorHttpException;
@@ -16,6 +17,7 @@ class JsonApiResponseFormatterTest extends TestCase
1617
{
1718
public function testFormatException()
1819
{
20+
\Yii::$app->controller = new Controller('test', \Yii::$app);
1921
$formatter = new JsonApiResponseFormatter();
2022
$exception = new ServerErrorHttpException('Server error');
2123
$response = new Response();
@@ -42,6 +44,7 @@ public function testFormatException()
4244

4345
public function testFormModelError()
4446
{
47+
\Yii::$app->controller = new Controller('test', \Yii::$app);
4548
$formatter = new JsonApiResponseFormatter();
4649
$exception = new ServerErrorHttpException('Server error');
4750
$response = new Response();

tests/actions/UpdateActionTest.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
<?php
22
/**
3-
* Created by PhpStorm.
4-
* User: anton
5-
* Date: 08/03/2017
6-
* Time: 19:07
3+
* @author Anton Tuyakhov <atuyakhov@gmail.com>
74
*/
85

96
namespace tuyakhov\jsonapi\tests\actions;

0 commit comments

Comments
 (0)