Created
December 21, 2012 15:29
-
-
Save baldurrensch/4353471 to your computer and use it in GitHub Desktop.
This is a handler for the JMS serializer to handle parent and child type serialization. You need to set the type of your collection as `@Type("\Acme\MyBundle\Model\ChildType")`, so that the serializer picks up the handler correctly. Also remember that you need to hook up the handler as a service (and give it the correct tag).
This file contains 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 Acme\MyBundle\Serializer\Handler; | |
use JMS\Serializer\Handler\SubscribingHandlerInterface; | |
use JMS\Serializer\EventDispatcher\Event; | |
use JMS\Serializer\GraphNavigator; | |
use JMS\Serializer\XmlSerializationVisitor; | |
use JMS\Serializer\GenericSerializationVisitor; | |
use Acme\MyBundle\Model\MyCustomParentType; | |
/** | |
* Custom Handler for Parent and Child Types | |
*/ | |
class CustomHandler implements SubscribingHandlerInterface | |
{ | |
public static function getSubscribingMethods() | |
{ | |
$methods = array(); | |
foreach (array('json', 'xml', 'yml') as $format) { | |
$methods[] = array( | |
'direction' => GraphNavigator::DIRECTION_SERIALIZATION, | |
'format' => $format, | |
'type' => 'Acme\MyBundle\Model\MyCustomParentType', | |
'method' => 'serializeTo'.('xml' == $format ? 'XML' : 'Array'), | |
); | |
} | |
return $methods; | |
} | |
public function serializeToXML(XmlSerializationVisitor $visitor, MyCustomParentType $object, array $type) | |
{ | |
if ($object instanceof \Acme\MyBundle\Model\ChildType1) { | |
return $visitor->getNavigator()->accept($object, null, $visitor); | |
} else if (...) { | |
... | |
} | |
} | |
public function serializeToArray(GenericSerializationVisitor $visitor, MyCustomParentType $object, array $type) | |
{ | |
... | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
$visitor->getNavigatior() is deprecated