-
-
Notifications
You must be signed in to change notification settings - Fork 79
Allow serializers to add links #79
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
* | ||
* | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Those trailing whitespaces were removed by my IDE, but I guess this is fine |
||
* @return string New function name | ||
*/ | ||
private function replaceDashWithUppercase($name) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should a Resource instance's links take precedence over the serializer links? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's what I assumed (and what is covered by a test), but frankly I wasn't really sure about it.. And after thinking about it again it might be more flexible if a serializer can overrule resource links. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah yes, didn't see the test and misread the order of |
||
if (! empty($links)) { | ||
$array['links'] = $links; | ||
} | ||
|
||
return $array; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,15 +23,18 @@ 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', | ||
'id' => '123', | ||
'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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Each class must be in a file by itself There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah well.. nit pick yourself ;) |
||
{ | ||
public function getLinks($post) | ||
{ | ||
return ['self' => sprintf('/posts/%s', $post->id)]; | ||
} | ||
} | ||
|
||
class CommentSerializer extends AbstractSerializer | ||
{ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The Resource constructor expects those parameters