Skip to content

Instantly share code, notes, and snippets.

@afurculita
Forked from stof/normalizer.php
Last active August 29, 2015 14:23
Show Gist options
  • Save afurculita/2cd6093a43b5fc654ecd to your computer and use it in GitHub Desktop.
Save afurculita/2cd6093a43b5fc654ecd to your computer and use it in GitHub Desktop.
<?php
namespace Gitonomy\Bundle\CoreBundle\Serializer\Normalizer;
use Doctrine\Common\Persistence\ManagerRegistry;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
class EntitiesNormalizer implements NormalizerInterface, DenormalizerInterface
{
protected $doctrineRegistry;
public function __construct(ManagerRegistry $doctrineRegistry)
{
$this->doctrineRegistry = $doctrineRegistry;
}
public function normalize($object, $format = null)
{
$class = get_class($object):
return $this->doctrineRegistry
->getManagerForClass($class)
->getMetadataFactory()
->getMetadataFor($class)
->getIdentifierValues($object)
;
}
public function denormalize($data, $class, $format = null)
{
return $this->doctrineRegistry
->getManagerForClass($class)
->getRepository($class)
->find($data)
;
}
public function supportsNormalization($data, $format = null)
{
return null !== $this->doctrineRegistry->getManagerForClass(get_class($class));
}
public function supportsDenormalization($data, $type, $format = null)
{
return null !== $this->doctrineRegistry->getManagerForClass($type);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment