Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save magnetik/1815d79e7380c0339e88 to your computer and use it in GitHub Desktop.
Save magnetik/1815d79e7380c0339e88 to your computer and use it in GitHub Desktop.
<?php
namespace AppBundle\Utils;
use JMS\Serializer\Construction\DoctrineObjectConstructor;
use JMS\Serializer\Construction\ObjectConstructorInterface;
use JMS\Serializer\Construction\UnserializeObjectConstructor;
use JMS\Serializer\DeserializationContext;
use JMS\Serializer\Metadata\ClassMetadata;
use JMS\Serializer\VisitorInterface;
class JMSSerializerDoctrineUnserializeObjectConstructor implements ObjectConstructorInterface
{
private $doctrineObjectConstructor;
private $unserializeObjectConstructor;
public function __construct(DoctrineObjectConstructor $doctrineObjectConstructor, UnserializeObjectConstructor $unserializeObjectConstructor)
{
$this->doctrineObjectConstructor = $doctrineObjectConstructor;
$this->unserializeObjectConstructor = $unserializeObjectConstructor;
}
public function construct(VisitorInterface $visitor, ClassMetadata $metadata, $data, array $type, DeserializationContext $context)
{
$object = $this->doctrineObjectConstructor->construct($visitor, $metadata, $data, $type, $context);
if (!is_object($object)) {
return $this->unserializeObjectConstructor->construct($visitor, $metadata, $data, $type, $context);
}
return $object;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment