-
-
Save afurculita/2cd6093a43b5fc654ecd to your computer and use it in GitHub Desktop.
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 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