vendor/api-platform/core/src/Hal/Serializer/ObjectNormalizer.php line 40

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the API Platform project.
  4.  *
  5.  * (c) Kévin Dunglas <dunglas@gmail.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. declare(strict_types=1);
  11. namespace ApiPlatform\Hal\Serializer;
  12. use ApiPlatform\Api\IriConverterInterface;
  13. use ApiPlatform\Core\Api\IriConverterInterface as LegacyIriConverterInterface;
  14. use Symfony\Component\Serializer\Exception\LogicException;
  15. use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface;
  16. use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
  17. use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
  18. /**
  19.  * Decorates the output with JSON HAL metadata when appropriate, but otherwise
  20.  * just passes through to the decorated normalizer.
  21.  */
  22. final class ObjectNormalizer implements NormalizerInterfaceDenormalizerInterfaceCacheableSupportsMethodInterface
  23. {
  24.     public const FORMAT 'jsonhal';
  25.     private $decorated;
  26.     private $iriConverter;
  27.     public function __construct(NormalizerInterface $decorated$iriConverter)
  28.     {
  29.         $this->decorated $decorated;
  30.         $this->iriConverter $iriConverter;
  31.         if ($iriConverter instanceof LegacyIriConverterInterface) {
  32.             trigger_deprecation('api-platform/core''2.7'sprintf('Use an implementation of "%s" instead of "%s".'IriConverterInterface::class, LegacyIriConverterInterface::class));
  33.         }
  34.     }
  35.     public function supportsNormalization($data$format null, array $context = []): bool
  36.     {
  37.         return self::FORMAT === $format && $this->decorated->supportsNormalization($data$format$context);
  38.     }
  39.     public function hasCacheableSupportsMethod(): bool
  40.     {
  41.         return $this->decorated instanceof CacheableSupportsMethodInterface && $this->decorated->hasCacheableSupportsMethod();
  42.     }
  43.     /**
  44.      * @param mixed|null $format
  45.      *
  46.      * @return array|string|int|float|bool|\ArrayObject|null
  47.      */
  48.     public function normalize($object$format null, array $context = [])
  49.     {
  50.         if (isset($context['api_resource'])) {
  51.             $originalResource $context['api_resource'];
  52.             unset($context['api_resource']);
  53.         }
  54.         $data $this->decorated->normalize($object$format$context);
  55.         if (!\is_array($data)) {
  56.             return $data;
  57.         }
  58.         if (!isset($originalResource)) {
  59.             return $data;
  60.         }
  61.         $metadata = [
  62.             '_links' => [
  63.                 'self' => [
  64.                     'href' => $this->iriConverter instanceof LegacyIriConverterInterface $this->iriConverter->getIriFromItem($originalResource) : $this->iriConverter->getIriFromResource($originalResource),
  65.                 ],
  66.             ],
  67.         ];
  68.         return $metadata $data;
  69.     }
  70.     public function supportsDenormalization($data$type$format null, array $context = []): bool
  71.     {
  72.         // prevent the use of lower priority normalizers (e.g. serializer.normalizer.object) for this format
  73.         return self::FORMAT === $format;
  74.     }
  75.     /**
  76.      * @param mixed|null $format
  77.      *
  78.      * @throws LogicException
  79.      */
  80.     public function denormalize($data$class$format null, array $context = [])
  81.     {
  82.         throw new LogicException(sprintf('%s is a read-only format.'self::FORMAT));
  83.     }
  84. }
  85. class_alias(ObjectNormalizer::class, \ApiPlatform\Core\Hal\Serializer\ObjectNormalizer::class);