|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the API Platform project. |
| 5 | + * |
| 6 | + * (c) Kévin Dunglas <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +declare(strict_types=1); |
| 13 | + |
| 14 | +namespace ApiPlatform\Tests\Fixtures\TestBundle\Serializer\Normalizer; |
| 15 | + |
| 16 | +use ApiPlatform\Metadata\Get; |
| 17 | +use ApiPlatform\Tests\Fixtures\TestBundle\Entity\RelationGroupImpactOnCollection; |
| 18 | +use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface; |
| 19 | +use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait; |
| 20 | +use Symfony\Component\Serializer\Normalizer\NormalizerInterface; |
| 21 | + |
| 22 | +final class AddGroupNormalizer implements NormalizerAwareInterface, NormalizerInterface |
| 23 | +{ |
| 24 | + use NormalizerAwareTrait; |
| 25 | + |
| 26 | + private const ALREADY_CALLED = 'RELATED_GROUP_IMPACT_ON_COLLECTION_NORMALIZER_ALREADY_CALLED'; |
| 27 | + |
| 28 | + public function normalize($object, $format = null, array $context = []): array|string|int|float|bool|\ArrayObject |
| 29 | + { |
| 30 | + $context[self::ALREADY_CALLED] = true; |
| 31 | + if (!($operation = $context['operation'] ?? null)) { |
| 32 | + return $this->normalizer->normalize($object, $format, $context); |
| 33 | + } |
| 34 | + |
| 35 | + if ($operation instanceof Get && '/custom_normalizer_relation_group_impact_on_collection' === $operation->getUriTemplate()) { |
| 36 | + $context['groups'] = ['related']; |
| 37 | + } |
| 38 | + |
| 39 | + return $this->normalizer->normalize($object, $format, $context); |
| 40 | + } |
| 41 | + |
| 42 | + public function supportsNormalization($data, $format = null, array $context = []): bool |
| 43 | + { |
| 44 | + // Make sure we're not called twice |
| 45 | + if (isset($context[self::ALREADY_CALLED])) { |
| 46 | + return false; |
| 47 | + } |
| 48 | + |
| 49 | + return $data instanceof RelationGroupImpactOnCollection; |
| 50 | + } |
| 51 | +} |
0 commit comments