for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
/*
* This file is part of the xAPI package.
*
* (c) Christian Flothmann <[email protected]>
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Xabbuh\XApi\Serializer\Event;
use JMS\Serializer\EventDispatcher\EventSubscriberInterface;
use JMS\Serializer\EventDispatcher\PreSerializeEvent;
/**
* Event listener that sets the proper class name for polymorphic objects
* before they are serialized.
* @author Christian Flothmann <[email protected]>
class SetSerializedTypeEventSubscriber implements EventSubscriberInterface
{
* {@inheritDoc}
public static function getSubscribedEvents()
return array(
array(
'event' => 'serializer.pre_serialize',
'method' => 'onPreSerialize',
),
);
}
public function onPreSerialize(PreSerializeEvent $event)
/** @var object $object */
$object = $event->getObject();
if (!is_object($object)) {
return;
$type = $event->getType();
$polymorphicTypes = array(
'Xabbuh\XApi\Model\Actor',
'Xabbuh\XApi\Model\Object',
if (in_array($type['name'], $polymorphicTypes)) {
$event->setType(get_class($object));