Created
January 8, 2014 08:45
-
-
Save h4cc/8313723 to your computer and use it in GitHub Desktop.
JMS\Discriminator Example.
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 | |
namespace Entity; | |
use JMS\Serializer\Annotation as JMS; | |
/** | |
* @JMS\Discriminator(field = "type", map = { | |
* "user": "Entity\User", | |
* "mod": "Entity\Moderator" | |
* }) | |
*/ | |
abstract class AbstractUser | |
{ | |
/** | |
* @JMS\Type("integer") | |
*/ | |
public $id; | |
/** | |
* @JMS\Type("string") | |
*/ | |
public $name; | |
/** | |
* @JMS\Type("string") | |
*/ | |
public $type; | |
} |
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 | |
namespace Entity; | |
use JMS\Serializer\Annotation as JMS; | |
class Moderator extends AbstractUser | |
{ | |
/** | |
* @JMS\Type("string") | |
*/ | |
public $foo; | |
} | |
/* | |
Serialized output: | |
* | |
{ | |
id: 1, | |
name: "a Moderator name", | |
type: "mod", | |
foo: "..." | |
} | |
*/ |
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 | |
namespace Entity; | |
use JMS\Serializer\Annotation as JMS; | |
class User extends AbstractUser | |
{ | |
/** | |
* @JMS\Type("string") | |
*/ | |
public $bar; | |
} | |
/* | |
Serialized output: | |
* | |
{ | |
id: 1, | |
name: "a User name", | |
type: "user", | |
bar: "..." | |
} | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment