From 53866bb16ae825cd8871f8833524e6c662269f95 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kocsis=20M=C3=A1t=C3=A9?= Date: Wed, 25 Nov 2015 21:45:35 +0100 Subject: [PATCH] Simplified Links --- CHANGELOG.md | 4 + README.md | 34 +++---- .../Book/JsonApi/Document/BookDocument.php | 3 +- .../Resource/BookResourceTransformer.php | 7 +- .../User/JsonApi/Document/UserDocument.php | 3 +- .../User/JsonApi/Document/UsersDocument.php | 2 +- .../Resource/ContactResourceTransformer.php | 3 +- src/JsonApi/Schema/Links.php | 90 +++++-------------- src/JsonApi/Schema/RelativeLinks.php | 13 --- tests/JsonApi/Response/ResponderTest.php | 2 +- tests/JsonApi/Schema/LinksTest.php | 78 +--------------- .../AbstractResourceTransformerTest.php | 2 +- 12 files changed, 49 insertions(+), 192 deletions(-) delete mode 100644 src/JsonApi/Schema/RelativeLinks.php diff --git a/CHANGELOG.md b/CHANGELOG.md index de333678..1653073b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,9 +9,13 @@ CHANGED: - Renamed `getDefaultRelationships()` to `getDefaultIncludedRelationships()` in transformers to better reflect its meaning - The "data" key of relationships won't be present in the response when it is empty +- Renamed `Links::addLinks()` to `Links::setLinks` and `Links::addLink()` to `Links::setLink()` REMOVED: +- Most of the `Links::create*` static methods to simplify creation +- `RelativeLinks` class as it became useless + FIXED: - `Responder::getDocumentResourceResponse()` was wrongly called statically diff --git a/README.md b/README.md index 3d01a37e..a01061da 100644 --- a/README.md +++ b/README.md @@ -213,17 +213,12 @@ public function getLinks() ); /* This is equivalent to the following: - return Links::createRelativeWithSelf( + return Links::createWithBaseUri( "http://example.com/api", - new Link("/books/" . $this->getResourceId()) - ); - - or: - - return Links::createAbsoluteWithSelf( - new Link("http://example.com/api/books/" . $this->getResourceId()) + [ + new Link("/books/" . $this->getResourceId()) + ] ); - */ } ``` @@ -263,7 +258,7 @@ There is an `ErrorDocument` too, which makes it possible to build error response /** @var ErrorDocument $errorDocument */ $errorDocument = new ErrorDocument(); $errorDocument->setJsonApi(new JsonApi("1.0")); -$errorDocument->setLinks(Links::createAbsoluteWithSelf("http://example.com/api/errors/404"))); +$errorDocument->setLinks(Links::createWithoutBaseUri()->setSelf("http://example.com/api/errors/404"))); $errorDocument->addError(new MyError()); ``` @@ -360,24 +355,17 @@ class BookResourceTransformer extends AbstractResourceTransformer public function getLinks($book) { return new Links( - "http://example.com/api" [ "self" => new Link("/books/" . $this->getId($book)) ] ); /* This is equivalent to the following: - return Links::createRelativeWithSelf( - "http://example.com/api", - new Link("/books/" . $this->getResourceId()) - ); - - or: - - return Links::createAbsoluteWithSelf( - new Link("http://example.com/api/books/" . $this->getResourceId()) + return Links::createWithoutBaseUri( + [ + "self" => new Link("/books/" . $this->getResourceId()) + ] ); - */ } /** @@ -425,7 +413,7 @@ class BookResourceTransformer extends AbstractResourceTransformer "authors" => function(array $book) { return ToManyRelationship::create() ->setLinks( - Links::createAbsoluteWithSelf(new Link("http://example.com/api/books/relationships/authors")) + Links::createWithoutBaseUri()->setSelf(new Link("/books/relationships/authors")) ) ->setData($book["authors"], $this->authorTransformer) ; @@ -433,7 +421,7 @@ class BookResourceTransformer extends AbstractResourceTransformer "publisher" => function($book) { return ToOneRelationship::create() ->setLinks( - Links::createAbsoluteWithSelf(new Link("http://example.com/api/books/relationships/publisher")) + Links::createWithoutBaseUri()->setSelf(new Link("/books/relationships/publisher")) ) ->setData($book["publisher"], $this->publisherTransformer) ; diff --git a/examples/Book/JsonApi/Document/BookDocument.php b/examples/Book/JsonApi/Document/BookDocument.php index f224ba2c..e8210c5b 100644 --- a/examples/Book/JsonApi/Document/BookDocument.php +++ b/examples/Book/JsonApi/Document/BookDocument.php @@ -58,8 +58,7 @@ public function getMeta() */ public function getLinks() { - return new Links( - "http://example.com/api", + return Links::createWithoutBaseUri( [ "self" => new Link("/books/" . $this->getResourceId()) ] diff --git a/examples/Book/JsonApi/Resource/BookResourceTransformer.php b/examples/Book/JsonApi/Resource/BookResourceTransformer.php index 0d809046..6fe1a30e 100644 --- a/examples/Book/JsonApi/Resource/BookResourceTransformer.php +++ b/examples/Book/JsonApi/Resource/BookResourceTransformer.php @@ -82,8 +82,7 @@ public function getMeta($book) */ public function getLinks($book) { - return new Links( - "http://example.com/api", + return Links::createWithoutBaseUri( [ "self" => new Link($this->getSelfLinkHref($book)) ] @@ -148,7 +147,7 @@ public function getRelationships($book) ToManyRelationship::create() ->setLinks( new Links( - "http://example.com/api" . $this->getSelfLinkHref($book), + $this->getSelfLinkHref($book), [ "self" => new Link("/relationships/authors") ] @@ -162,7 +161,7 @@ public function getRelationships($book) ToOneRelationship::create() ->setLinks( new Links( - "http://example.com/api" . $this->getSelfLinkHref($book), + $this->getSelfLinkHref($book), [ "self" => new Link("/relationships/publisher") ] diff --git a/examples/User/JsonApi/Document/UserDocument.php b/examples/User/JsonApi/Document/UserDocument.php index 9e3a3045..6193ff2d 100644 --- a/examples/User/JsonApi/Document/UserDocument.php +++ b/examples/User/JsonApi/Document/UserDocument.php @@ -57,8 +57,7 @@ public function getMeta() */ public function getLinks() { - return new Links( - "http://example.com/api", + return Links::createWithoutBaseUri( [ "self" => new Link("/users/" . $this->getResourceId()) ] diff --git a/examples/User/JsonApi/Document/UsersDocument.php b/examples/User/JsonApi/Document/UsersDocument.php index ce7553c5..715e5523 100644 --- a/examples/User/JsonApi/Document/UsersDocument.php +++ b/examples/User/JsonApi/Document/UsersDocument.php @@ -56,6 +56,6 @@ public function getMeta() */ public function getLinks() { - return Links::createRelativeWithPagination("http://example.com/api", "/users", $this->domainObject); + return Links::createWithoutBaseUri()->setPagination("/users", $this->domainObject); } } diff --git a/examples/User/JsonApi/Resource/ContactResourceTransformer.php b/examples/User/JsonApi/Resource/ContactResourceTransformer.php index bb2fa43b..54c376bd 100644 --- a/examples/User/JsonApi/Resource/ContactResourceTransformer.php +++ b/examples/User/JsonApi/Resource/ContactResourceTransformer.php @@ -58,8 +58,7 @@ public function getMeta($contact) */ public function getLinks($contact) { - return new Links( - "http://example.com/api", + return Links::createWithoutBaseUri( [ "self" => new Link("/contacts/" . $this->getId($contact)) ] diff --git a/src/JsonApi/Schema/Links.php b/src/JsonApi/Schema/Links.php index 4eef342a..8d85ca93 100644 --- a/src/JsonApi/Schema/Links.php +++ b/src/JsonApi/Schema/Links.php @@ -19,7 +19,7 @@ class Links * @param \WoohooLabs\Yin\JsonApi\Schema\Link[] $links * @return $this */ - public static function createAbsolute(array $links = []) + public static function createWithoutBaseUri(array $links = []) { return new self("", $links); } @@ -29,97 +29,53 @@ public static function createAbsolute(array $links = []) * @param \WoohooLabs\Yin\JsonApi\Schema\Link[] $links * @return $this */ - public static function createRelative($baseUri, array $links = []) + public static function createWithBaseUri($baseUri, array $links = []) { return new self($baseUri, $links); } - /** - * @param \WoohooLabs\Yin\JsonApi\Schema\Link $self - * @return $this - */ - public static function createAbsoluteWithSelf(Link $self) - { - return new self("", ["self" => $self]); - } - /** * @param string $baseUri - * @param \WoohooLabs\Yin\JsonApi\Schema\Link $self - * @return $this - */ - public static function createRelativeWithSelf($baseUri, Link $self) - { - return new self($baseUri, ["self" => $self]); - } - - /** - * @param \WoohooLabs\Yin\JsonApi\Schema\Link $related - * @return $this + * @param \WoohooLabs\Yin\JsonApi\Schema\Link[] $links */ - public static function createAbsoluteWithRelated(Link $related) + public function __construct($baseUri = "", array $links = []) { - return new self("", ["related" => $related]); + $this->baseUri = $baseUri; + $this->links = $links; } /** - * @param string $baseUri - * @param \WoohooLabs\Yin\JsonApi\Schema\Link $related - * @return $this + * @return array */ - public static function createRelativeWithRelated($baseUri, Link $related) + public function transform() { - return new self($baseUri, ["related" => $related]); - } + $links = []; - /** - * @param string $uri - * @param \WoohooLabs\Yin\JsonApi\Schema\Pagination\PaginationLinkProviderInterface $pagination - * @return $this - */ - public static function createAbsoluteWithPagination($uri, PaginationLinkProviderInterface $pagination) - { - $links = new self(""); + foreach ($this->links as $rel => $link) { + /** @var \WoohooLabs\Yin\JsonApi\Schema\Link $link */ + $links[$rel] = $link ? $link->transform($this->baseUri) : null; + } - return $links->setPagination($uri, $pagination); + return $links; } /** - * @param string $baseUri - * @param string $uri - * @param \WoohooLabs\Yin\JsonApi\Schema\Pagination\PaginationLinkProviderInterface $pagination - * @return $this + * @return string */ - public static function createRelativeWithPagination($baseUri, $uri, PaginationLinkProviderInterface $pagination) + public function getBaseUri() { - $links = new self($baseUri); - - return $links->setPagination($uri, $pagination); + return $this->baseUri; } /** * @param string $baseUri - * @param \WoohooLabs\Yin\JsonApi\Schema\Link[] $links + * @return $this */ - public function __construct($baseUri = "", array $links = []) + public function setBaseUri($baseUri) { $this->baseUri = $baseUri; - $this->links = $links; - } - - /** - * @return array - */ - public function transform() - { - $links = []; - foreach ($this->links as $rel => $link) { - /** @var \WoohooLabs\Yin\JsonApi\Schema\Link $link */ - $links[$rel] = $link ? $link->transform($this->baseUri) : null; - } - - return $links; + return $this; } /** @@ -265,10 +221,10 @@ public function getLink($name) * @param \WoohooLabs\Yin\JsonApi\Schema\Link[] $links * @return $this */ - public function addLinks(array $links) + public function setLinks(array $links) { foreach ($links as $rel => $link) { - $this->addLink($rel, $link); + $this->setLink($rel, $link); } return $this; @@ -279,7 +235,7 @@ public function addLinks(array $links) * @param \WoohooLabs\Yin\JsonApi\Schema\Link $link * @return $this */ - public function addLink($name, Link $link = null) + public function setLink($name, Link $link = null) { $this->links[$name] = $link; diff --git a/src/JsonApi/Schema/RelativeLinks.php b/src/JsonApi/Schema/RelativeLinks.php deleted file mode 100644 index f16cf45b..00000000 --- a/src/JsonApi/Schema/RelativeLinks.php +++ /dev/null @@ -1,13 +0,0 @@ - new Link($href)])); $response = $this->createResponder()->created($document, []); $this->assertEquals([$href], $response->getHeader("location")); diff --git a/tests/JsonApi/Schema/LinksTest.php b/tests/JsonApi/Schema/LinksTest.php index cb692db1..94f36fe2 100644 --- a/tests/JsonApi/Schema/LinksTest.php +++ b/tests/JsonApi/Schema/LinksTest.php @@ -8,80 +8,6 @@ class LinksTest extends PHPUnit_Framework_TestCase { - public function testCreateAbsolute() - { - $links = ["self" => new Link("http://example.com/api/users")]; - - $linksObject = $this->createLinks("", $links); - $this->assertEquals($linksObject, Links::createAbsolute($links)); - } - - public function testCreateRelative() - { - $baseUri = "http://example.com/api"; - $links = ["self" => new Link("/users")]; - - $linksObject = $this->createLinks($baseUri, $links); - $this->assertEquals($linksObject, Links::createRelative($baseUri, $links)); - } - - public function testCreateAbsoluteWithSelf() - { - $self = new Link("http://example.com/api/users"); - $links = ["self" => $self]; - - $linksObject = $this->createLinks("", $links); - $this->assertEquals($linksObject, Links::createAbsoluteWithSelf($self)); - } - - public function testCreateRelativeWithSelf() - { - $baseUri = "http://example.com/api"; - $self = new Link("/users"); - $links = ["self" => $self]; - - $linksObject = $this->createLinks($baseUri, $links); - $this->assertEquals($linksObject, Links::createRelativeWithSelf($baseUri, $self)); - } - - public function testCreateAbsoluteWithRelated() - { - $self = new Link("http://example.com/api/users"); - $links = ["related" => $self]; - - $linksObject = $this->createLinks("", $links); - $this->assertEquals($linksObject, Links::createAbsoluteWithRelated($self)); - } - - public function testCreateRelativeWithRelated() - { - $baseUri = "http://example.com/api"; - $self = new Link("/users"); - $links = ["related" => $self]; - - $linksObject = $this->createLinks($baseUri, $links); - $this->assertEquals($linksObject, Links::createRelativeWithRelated($baseUri, $self)); - } - - public function testCreateAbsoluteWithPagination() - { - $uri = "http://example.com/api/users/"; - $pagination = new StubPaginationLinkProvider(); - - $linksObject = $this->createLinks("")->setPagination($uri, $pagination); - $this->assertEquals($linksObject, Links::createAbsoluteWithPagination($uri, $pagination)); - } - - public function testCreateRelativeWithPagination() - { - $baseUri = "http://example.com/api"; - $uri = "/users"; - $pagination = new StubPaginationLinkProvider(); - - $linksObject = $this->createLinks($baseUri)->setPagination($uri, $pagination); - $this->assertEquals($linksObject, Links::createRelativeWithPagination($baseUri, $uri, $pagination)); - } - public function testTransform() { $self = new Link("http://example.com/api/users"); @@ -169,7 +95,7 @@ public function testGetLink() { $self = new Link("http://example.com/api/users"); - $linksObject = $this->createLinks()->addLink("self", $self); + $linksObject = $this->createLinks()->setLink("self", $self); $this->assertEquals($self, $linksObject->getLink("self")); } @@ -179,7 +105,7 @@ public function testGetMultipleLinks() $related = new Link("http://example.com/api/people/1"); $links = ["self" => $self, "related" => $related]; - $linksObject = $this->createLinks()->addLinks($links); + $linksObject = $this->createLinks()->setLinks($links); $this->assertEquals($self, $linksObject->getLink("self")); $this->assertEquals($related, $linksObject->getLink("related")); } diff --git a/tests/JsonApi/Transformer/AbstractResourceTransformerTest.php b/tests/JsonApi/Transformer/AbstractResourceTransformerTest.php index 20232035..8f87372e 100644 --- a/tests/JsonApi/Transformer/AbstractResourceTransformerTest.php +++ b/tests/JsonApi/Transformer/AbstractResourceTransformerTest.php @@ -87,7 +87,7 @@ public function testTransformToResourceWithMeta() public function testTransformToResourceWithLinks() { $domainObject = []; - $links = Links::createAbsoluteWithSelf(new Link("http://example.com/api/users")); + $links = Links::createWithoutBaseUri()->setSelf(new Link("http://example.com/api/users")); $transformer = $this->createTransformer("", "", [], $links); $transformedResource = $this->transformToResource($transformer, $domainObject);