Skip to content

Instantly share code, notes, and snippets.

@yoye
Created January 17, 2014 09:47
Show Gist options
  • Save yoye/8470772 to your computer and use it in GitHub Desktop.
Save yoye/8470772 to your computer and use it in GitHub Desktop.
<?php
use Hateoas\HateoasBuilder;
use Hateoas\Configuration\Annotation as Hateoas;
use JMS\Serializer\Annotation as Serializer;
use JMS\Serializer\SerializerBuilder;
use JMS\Serializer\SerializationContext;
/**
* @Serializer\ExclusionPolicy("all")
* @Hateoas\Relation(
* "friend",
* embedded = @Hateoas\Embedded(
* "expr(object.getFriend())",
* exclusion = @Hateoas\Exclusion(maxDepth = 1)
* )
* )
*/
class User
{
/**
* @Serializer\Expose
*/
private $username;
private $friend;
function __construct($username)
{
$this->username = $username;
}
public function getFriend()
{
return $this->friend;
}
public function setFriend($friend)
{
$this->friend = $friend;
}
}
$foo = new User('foo');
$bar = new User('bar');
$foobar = new User('foobar');
$baz = new User('baz');
$foo->setFriend($bar);
$bar->setFriend($foobar);
$foobar->setFriend($baz);
$hateoas = HateoasBuilder::create()->build();
echo $hateoas->serialize($foo, 'json', SerializationContext::create()->enableMaxDepthChecks());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment