Created
January 17, 2014 09:47
-
-
Save yoye/8470772 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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