Skip to content

Commit

Permalink
Simplified Links
Browse files Browse the repository at this point in the history
  • Loading branch information
kocsismate committed Nov 25, 2015
1 parent 6c8f328 commit 53866bb
Show file tree
Hide file tree
Showing 12 changed files with 49 additions and 192 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
34 changes: 11 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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())
]
);
*/
}
```

Expand Down Expand Up @@ -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());
```

Expand Down Expand Up @@ -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())
]
);
*/
}

/**
Expand Down Expand Up @@ -425,15 +413,15 @@ 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)
;
},
"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)
;
Expand Down
3 changes: 1 addition & 2 deletions examples/Book/JsonApi/Document/BookDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -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())
]
Expand Down
7 changes: 3 additions & 4 deletions examples/Book/JsonApi/Resource/BookResourceTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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))
]
Expand Down Expand Up @@ -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")
]
Expand All @@ -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")
]
Expand Down
3 changes: 1 addition & 2 deletions examples/User/JsonApi/Document/UserDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -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())
]
Expand Down
2 changes: 1 addition & 1 deletion examples/User/JsonApi/Document/UsersDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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))
]
Expand Down
90 changes: 23 additions & 67 deletions src/JsonApi/Schema/Links.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -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;
Expand All @@ -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;

Expand Down
13 changes: 0 additions & 13 deletions src/JsonApi/Schema/RelativeLinks.php

This file was deleted.

2 changes: 1 addition & 1 deletion tests/JsonApi/Response/ResponderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function testCreated()
public function testCreatedWithLinks()
{
$href = "http://example.com/users";
$document = new StubSuccessfulDocument([], [], null, [], Links::createAbsoluteWithSelf(new Link($href)));
$document = new StubSuccessfulDocument([], [], null, [], new Links("", ["self" => new Link($href)]));

$response = $this->createResponder()->created($document, []);
$this->assertEquals([$href], $response->getHeader("location"));
Expand Down
Loading

0 comments on commit 53866bb

Please sign in to comment.