Last active
December 21, 2015 15:46
-
-
Save mRoca/b03a05679e10d3f9afe3 to your computer and use it in GitHub Desktop.
EntityNormalizer - Max Depth
This file contains hidden or 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 AppBundle\JsonLd\Serializer; | |
| use Dunglas\ApiBundle\Api\IriConverterInterface; | |
| use Dunglas\ApiBundle\Api\ResourceCollectionInterface; | |
| use Dunglas\ApiBundle\JsonLd\ContextBuilder; | |
| use Dunglas\ApiBundle\JsonLd\Serializer\ItemNormalizer; | |
| use Dunglas\ApiBundle\Mapping\ClassMetadataFactoryInterface; | |
| use Symfony\Component\PropertyAccess\PropertyAccessorInterface; | |
| use Symfony\Component\Serializer\NameConverter\NameConverterInterface; | |
| /** | |
| * Temporary class used to avoid parent entity inclusion. | |
| * TODO Create a @MaxDepth anotation. | |
| */ | |
| class EntityNormalizer extends ItemNormalizer | |
| { | |
| const RELATIONS_DEFAULT_MAX_DEPTH = 1; | |
| /** | |
| * @var ResourceCollectionInterface | |
| */ | |
| private $resourceCollection; | |
| /** | |
| * @var IriConverterInterface | |
| */ | |
| private $iriConverter; | |
| public function __construct( | |
| ResourceCollectionInterface $resourceCollection, | |
| IriConverterInterface $iriConverter, | |
| ClassMetadataFactoryInterface $apiClassMetadataFactory, | |
| ContextBuilder $contextBuilder, | |
| PropertyAccessorInterface $propertyAccessor, | |
| NameConverterInterface $nameConverter = null | |
| ) { | |
| $this->resourceCollection = $resourceCollection; | |
| $this->iriConverter = $iriConverter; | |
| parent::__construct( | |
| $resourceCollection, | |
| $iriConverter, | |
| $apiClassMetadataFactory, | |
| $contextBuilder, | |
| $propertyAccessor, | |
| $nameConverter | |
| ); | |
| } | |
| /** | |
| * {@inheritdoc} | |
| */ | |
| public function supportsNormalization($data, $format = null) | |
| { | |
| if (self::FORMAT !== $format || !is_object($data) || $data instanceof \Traversable) { | |
| return false; | |
| } | |
| return null !== $this->resourceCollection->getResourceForEntity($data); | |
| } | |
| /** | |
| * {@inheritdoc} | |
| */ | |
| public function normalize($object, $format = null, array $context = []) | |
| { | |
| // TODO use an annotation on entities properties for max_depth value | |
| $context['current_depth'] = ($context['current_depth'] ?? 0) + 1; | |
| if ($context['current_depth'] > self::RELATIONS_DEFAULT_MAX_DEPTH) { | |
| return $this->iriConverter->getIriFromItem($object); | |
| } | |
| return parent::normalize($object, $format, $context); | |
| } | |
| /** | |
| * {@inheritdoc} | |
| */ | |
| public function supportsDenormalization($data, $type, $format = null) | |
| { | |
| return false; | |
| } | |
| } |
This file contains hidden or 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
| app.serializer.json_ld.normalizer.entity: | |
| public: false | |
| class: AppBundle\JsonLd\Serializer\EntityNormalizer | |
| arguments: | |
| - @api.resource_collection | |
| - @api.iri_converter | |
| - @api.mapping.class_metadata_factory | |
| - @api.json_ld.context_builder | |
| - @property_accessor | |
| - @?api.name_converter | |
| tags: | |
| - { name: serializer.normalizer, priority: 60 } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment