Skip to content

Commit

Permalink
let serializer add links
Browse files Browse the repository at this point in the history
  • Loading branch information
bnchdrff committed Nov 3, 2015
1 parent ef939db commit f794bdc
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/AbstractSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

abstract class AbstractSerializer implements SerializerInterface
{
use LinksTrait;

/**
* The type.
*
Expand Down Expand Up @@ -64,4 +66,8 @@ public function getRelationship($model, $name)
return $relationship;
}
}

public function getLinks($model) {
return [];
}
}
14 changes: 13 additions & 1 deletion src/Resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,22 @@ public function toArray()
$array['relationships'] = $relationships;
}

if (! empty($this->links)) {
$array['links'] = [];

if (count($this->links)) {
$array['links'] = $this->links;
}

$serializer_links = $this->serializer->getLinks($this->data);

if (count($serializer_links)) {
$array['links'] = array_merge($array['links'], $serializer_links);
}

if (!count($array['links'])) {
unset($array['links']);
}

return $array;
}

Expand Down

3 comments on commit f794bdc

@bwaidelich
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great addition, without this feature I wasn't able to create a discoverable REST API using this library.
The getLinks() method would have to be added to the SerializerInterface too, and it should probably get a ({@inheritdoc}) doc comment like the other methods.

@bwaidelich
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hope you don't mind that I went ahead and created the PR for this: tobyzerner#79

@bnchdrff
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome, thanks. I wasn't sure about the ins & outs of how to organize this code; your patch looks good.

Please sign in to comment.