diff --git a/README.md b/README.md index c4a8d76..3a1b609 100644 --- a/README.md +++ b/README.md @@ -115,7 +115,7 @@ $document->setMeta(['key' => 'value']); They also allow you to add links in a similar way: ```php -$resource = new Resource; +$resource = new Resource($data, $serializer); $resource->addLink('self', 'url'); $resource->setLinks(['key' => 'value']); ``` @@ -131,6 +131,19 @@ $document->addPaginationLinks( 100 // The total number of results ); ``` +Serializers can provide links as well: + +```php +use Tobscure\JsonApi\AbstractSerializer; + +class PostSerializer extends AbstractSerializer +{ + // ... + + public function getLinks($post) { + return ['self' => '/posts/' . $post->id]; + } +} ### Parameters diff --git a/src/AbstractSerializer.php b/src/AbstractSerializer.php index 53dcd7a..dc269cb 100644 --- a/src/AbstractSerializer.php +++ b/src/AbstractSerializer.php @@ -46,6 +46,14 @@ public function getAttributes($model, array $fields = null) return []; } + /** + * {@inheritdoc} + */ + public function getLinks($model) + { + return []; + } + /** * {@inheritdoc} * @@ -72,9 +80,9 @@ public function getRelationship($model, $name) /** * Removes all dashes from relationsship and uppercases the following letter. * @example If relationship parent-page is needed the the function name will be changed to parentPage - * + * * @param string Name of the function - * + * * @return string New function name */ private function replaceDashWithUppercase($name) diff --git a/src/Resource.php b/src/Resource.php index 5f4f3ef..d86f190 100644 --- a/src/Resource.php +++ b/src/Resource.php @@ -80,8 +80,16 @@ public function toArray() $array['relationships'] = $relationships; } + $links = []; if (! empty($this->links)) { - $array['links'] = $this->links; + $links = $this->links; + } + $serializerLinks = $this->serializer->getLinks($this->data); + if (! empty($serializerLinks)) { + $links = array_merge($serializerLinks, $links); + } + if (! empty($links)) { + $array['links'] = $links; } return $array; diff --git a/src/SerializerInterface.php b/src/SerializerInterface.php index 8b0a9fe..194ec10 100644 --- a/src/SerializerInterface.php +++ b/src/SerializerInterface.php @@ -38,6 +38,14 @@ public function getId($model); */ public function getAttributes($model, array $fields = null); + /** + * Get the links array. + * + * @param mixed $model + * @return array + */ + public function getLinks($model); + /** * Get a relationship. * diff --git a/tests/ResourceTest.php b/tests/ResourceTest.php index ac51bb5..5fffeeb 100644 --- a/tests/ResourceTest.php +++ b/tests/ResourceTest.php @@ -23,7 +23,7 @@ public function testToArrayReturnsArray() { $data = (object) ['id' => '123', 'foo' => 'bar', 'baz' => 'qux']; - $resource = new Resource($data, new PostSerializer4); + $resource = new Resource($data, new PostSerializer4WithLinks); $this->assertEquals([ 'type' => 'posts', @@ -31,7 +31,10 @@ public function testToArrayReturnsArray() 'attributes' => [ 'foo' => 'bar', 'baz' => 'qux' - ] + ], + 'links' => [ + 'self' => '/posts/123' + ], ], $resource->toArray()); } @@ -118,6 +121,27 @@ public function testCanMergeWithAnotherResource() ] ], $resource1->toArray()); } + + public function testLinksMergeWithSerializerLinks() + { + $post1 = (object) ['id' => '123', 'foo' => 'bar', 'comments' => [1]]; + + $resource1 = new Resource($post1, new PostSerializer4WithLinks()); + $resource1->addLink('self', 'overridden/by/resource'); + $resource1->addLink('related', '/some/other/comment'); + + $this->assertEquals([ + 'type' => 'posts', + 'id' => '123', + 'attributes' => [ + 'foo' => 'bar' + ], + 'links' => [ + 'self' => 'overridden/by/resource', + 'related' => '/some/other/comment' + ], + ], $resource1->toArray()); + } } class PostSerializer4 extends AbstractSerializer @@ -143,6 +167,13 @@ public function comments($post) return new Relationship(new Collection($post->comments, new CommentSerializer)); } } +class PostSerializer4WithLinks extends PostSerializer4 +{ + public function getLinks($post) + { + return ['self' => sprintf('/posts/%s', $post->id)]; + } +} class CommentSerializer extends AbstractSerializer {