Skip to content

Instantly share code, notes, and snippets.

@stof
Forked from alexandresalome/normalizer.php
Created October 5, 2012 12:34
Show Gist options
  • Save stof/3839594 to your computer and use it in GitHub Desktop.
Save stof/3839594 to your computer and use it in GitHub Desktop.
Normalizer for Doctrine entities
<?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);
}
}
@lyrixx
Copy link

lyrixx commented Oct 5, 2012

line 41, where does $class come from ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment