Skip to content

Instantly share code, notes, and snippets.

@h4cc
Created January 8, 2014 08:45
Show Gist options
  • Save h4cc/8313723 to your computer and use it in GitHub Desktop.
Save h4cc/8313723 to your computer and use it in GitHub Desktop.
JMS\Discriminator Example.
<?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;
}
<?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: "..."
}
*/
<?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