From efd244e91a362f4807a6ea5e152de96e8e0d4747 Mon Sep 17 00:00:00 2001 From: Xenofon Spafaridis Date: Fri, 9 Jun 2017 19:16:26 +0300 Subject: [PATCH] Implement related relationship access --- src/Controllers/ArticleController.php | 50 ++++++++++++++++--- src/Controllers/Routing.php | 18 +++++++ src/Controllers/TagController.php | 49 +++++++++++++++--- src/Controllers/UserController.php | 49 +++++++++++++++--- tests/testphase/tag/get-byid-article.json | 22 ++++++++ .../tag/get-byid-relationships-article.json | 22 ++++++++ 6 files changed, 190 insertions(+), 20 deletions(-) create mode 100644 tests/testphase/tag/get-byid-article.json create mode 100644 tests/testphase/tag/get-byid-relationships-article.json diff --git a/src/Controllers/ArticleController.php b/src/Controllers/ArticleController.php index ce758ac..2f2a23f 100644 --- a/src/Controllers/ArticleController.php +++ b/src/Controllers/ArticleController.php @@ -34,8 +34,11 @@ class ArticleController extends \Phramework\Examples\JSONAPI\Controller * @param string $method Request method * @param array $headers Request headers */ - public static function GET(\stdClass $params, string $method, array $headers) - { + public static function GET( + \stdClass $params, + string $method, + array $headers + ) { static::handleGET( $params, Article::class, @@ -52,8 +55,12 @@ public static function GET(\stdClass $params, string $method, array $headers) * @param array $headers Request headers * @param string $id Resource id */ - public static function GETById(\stdClass $params, string $method, array $headers, string $id) - { + public static function GETById( + \stdClass $params, + string $method, + array $headers, + string $id + ) { static::handleGETById( $params, $id, @@ -69,8 +76,11 @@ public static function GETById(\stdClass $params, string $method, array $headers * @param string $method Request method * @param array $headers Request headers */ - public static function POST(\stdClass $params, string $method, array $headers) - { + public static function POST( + \stdClass $params, + string $method, + array $headers + ) { $now = time(); static::handlePOST( @@ -204,4 +214,32 @@ public static function byIdRelationships( [] ); } + + /** + * Access resource's relationship resources + * `/article/{id}/{relationship}/` handler + * @param \stdClass $params + * @param string $method + * @param array $headers + * @param string $id + * @param string $relationship + */ + public static function byIdRelationshipsRelated( + \stdClass $params, + string $method, + array $headers, + string $id, + string $relationship + ) { + static::handleByIdRelationshipsRelated( + $params, + $method, + $headers, + $id, + $relationship, + Article::class, + [\Phramework\Phramework::METHOD_GET], + [] + ); + } } diff --git a/src/Controllers/Routing.php b/src/Controllers/Routing.php index 1370a91..55b0ec6 100644 --- a/src/Controllers/Routing.php +++ b/src/Controllers/Routing.php @@ -52,6 +52,12 @@ public function getRouting(): array 'byIdRelationships', Phramework::METHOD_ANY ], + [ + 'article/{id}/{relationship}', + ArticleController::class, + 'byIdRelationshipsRelated', + Phramework::METHOD_ANY + ], [ 'tag/', @@ -71,6 +77,12 @@ public function getRouting(): array 'byIdRelationships', Phramework::METHOD_ANY ], + [ + 'tag/{id}/{relationship}', + TagController::class, + 'byIdRelationshipsRelated', + Phramework::METHOD_ANY + ], [ 'user/', @@ -90,6 +102,12 @@ public function getRouting(): array 'byIdRelationships', Phramework::METHOD_ANY ], + [ + 'user/{id}/{relationship}', + UserController::class, + 'byIdRelationshipsRelated', + Phramework::METHOD_ANY + ], ]; } } diff --git a/src/Controllers/TagController.php b/src/Controllers/TagController.php index 83ee4d2..bcb9e9f 100644 --- a/src/Controllers/TagController.php +++ b/src/Controllers/TagController.php @@ -33,8 +33,11 @@ class TagController extends \Phramework\Examples\JSONAPI\Controller * @param string $method Request method * @param array $headers Request headers */ - public static function GET($params, $method, $headers) - { + public static function GET( + \stdClass $params, + string $method, + array $headers + ) { static::handleGET( $params, Tag::class, @@ -50,8 +53,12 @@ public static function GET($params, $method, $headers) * @param array $headers Request headers * @param string $id Resource id */ - public static function GETById($params, $method, $headers, string $id) - { + public static function GETById( + \stdClass $params, + string $method, + array $headers, + string $id + ) { static::handleGETById( $params, $id, @@ -70,9 +77,9 @@ public static function GETById($params, $method, $headers, string $id) * @param string $relationship Relationship */ public static function byIdRelationships( - $params, - $method, - $headers, + \stdClass $params, + string $method, + array $headers, string $id, string $relationship ) { @@ -88,4 +95,32 @@ public static function byIdRelationships( [] ); } + + /** + * Access resource's relationship resources + * `/tag/{id}/{relationship}/` handler + * @param \stdClass $params + * @param string $method + * @param array $headers + * @param string $id + * @param string $relationship + */ + public static function byIdRelationshipsRelated( + \stdClass $params, + string $method, + array $headers, + string $id, + string $relationship + ) { + static::handleByIdRelationshipsRelated( + $params, + $method, + $headers, + $id, + $relationship, + Tag::class, + [\Phramework\Phramework::METHOD_GET], + [] + ); + } } diff --git a/src/Controllers/UserController.php b/src/Controllers/UserController.php index 8317414..6284a6a 100644 --- a/src/Controllers/UserController.php +++ b/src/Controllers/UserController.php @@ -33,8 +33,11 @@ class UserController extends \Phramework\Examples\JSONAPI\Controller * @param string $method Request method * @param array $headers Request headers */ - public static function GET($params, $method, $headers) - { + public static function GET( + \stdClass $params, + string $method, + array $headers + ) { static::handleGET( $params, User::class, @@ -50,8 +53,12 @@ public static function GET($params, $method, $headers) * @param array $headers Request headers * @param string $id Resource id */ - public static function GETById($params, $method, $headers, string $id) - { + public static function GETById( + \stdClass $params, + string $method, + array $headers, + string $id + ) { static::handleGETById( $params, $id, @@ -70,9 +77,9 @@ public static function GETById($params, $method, $headers, string $id) * @param string $relationship Relationship */ public static function byIdRelationships( - $params, - $method, - $headers, + \stdClass $params, + string $method, + array $headers, string $id, string $relationship ) { @@ -88,4 +95,32 @@ public static function byIdRelationships( [] ); } + + /** + * Access resource's relationship resources + * `/user/{id}/{relationship}/` handler + * @param \stdClass $params + * @param string $method + * @param array $headers + * @param string $id + * @param string $relationship + */ + public static function byIdRelationshipsRelated( + \stdClass $params, + string $method, + array $headers, + string $id, + string $relationship + ) { + static::handleByIdRelationshipsRelated( + $params, + $method, + $headers, + $id, + $relationship, + User::class, + [\Phramework\Phramework::METHOD_GET], + [] + ); + } } diff --git a/tests/testphase/tag/get-byid-article.json b/tests/testphase/tag/get-byid-article.json new file mode 100644 index 0000000..695595b --- /dev/null +++ b/tests/testphase/tag/get-byid-article.json @@ -0,0 +1,22 @@ +{ + "meta": { + "order": 21 + }, + "request": { + "url": "tag/{{tagId}}/relationships/article", + "method": "GET", + "headers": [ + "{{{headerRequestAccept}}}", + "{{{headerRequestContentType}}}" + ] + }, + "response": { + "statusCode": 200, + "headers": { + "Content-Type": "{{{headerResponseContentType}}}" + }, + "ruleObjects": [ + "{{{responseBodyJsonapiResource}}}" + ] + } +} \ No newline at end of file diff --git a/tests/testphase/tag/get-byid-relationships-article.json b/tests/testphase/tag/get-byid-relationships-article.json new file mode 100644 index 0000000..e08945d --- /dev/null +++ b/tests/testphase/tag/get-byid-relationships-article.json @@ -0,0 +1,22 @@ +{ + "meta": { + "order": 21 + }, + "request": { + "url": "tag/{{tagId}}/article", + "method": "GET", + "headers": [ + "{{{headerRequestAccept}}}", + "{{{headerRequestContentType}}}" + ] + }, + "response": { + "statusCode": 200, + "headers": { + "Content-Type": "{{{headerResponseContentType}}}" + }, + "ruleObjects": [ + "{{{responseBodyJsonapiResource}}}" + ] + } +} \ No newline at end of file