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.     /**
  36.      * {@inheritdoc}
  37.      */
  38.     public function supportsNormalization($data$format null, array $context = []): bool
  39.     {
  40.         return self::FORMAT === $format && $this->decorated->supportsNormalization($data$format$context);
  41.     }
  42.     /**
  43.      * {@inheritdoc}
  44.      */
  45.     public function hasCacheableSupportsMethod(): bool
  46.     {
  47.         return $this->decorated instanceof CacheableSupportsMethodInterface && $this->decorated->hasCacheableSupportsMethod();
  48.     }
  49.     /**
  50.      * {@inheritdoc}
  51.      *
  52.      * @return array|string|int|float|bool|\ArrayObject|null
  53.      */
  54.     public function normalize($object$format null, array $context = [])
  55.     {
  56.         if (isset($context['api_resource'])) {
  57.             $originalResource $context['api_resource'];
  58.             unset($context['api_resource']);
  59.         }
  60.         $data $this->decorated->normalize($object$format$context);
  61.         if (!\is_array($data)) {
  62.             return $data;
  63.         }
  64.         if (!isset($originalResource)) {
  65.             return $data;
  66.         }
  67.         $metadata = [
  68.             '_links' => [
  69.                 'self' => [
  70.                     'href' => $this->iriConverter instanceof LegacyIriConverterInterface $this->iriConverter->getIriFromItem($originalResource) : $this->iriConverter->getIriFromResource($originalResource),
  71.                 ],
  72.             ],
  73.         ];
  74.         return $metadata $data;
  75.     }
  76.     /**
  77.      * {@inheritdoc}
  78.      */
  79.     public function supportsDenormalization($data$type$format null, array $context = []): bool
  80.     {
  81.         // prevent the use of lower priority normalizers (e.g. serializer.normalizer.object) for this format
  82.         return self::FORMAT === $format;
  83.     }
  84.     /**
  85.      * {@inheritdoc}
  86.      *
  87.      * @throws LogicException
  88.      *
  89.      * @return mixed
  90.      */
  91.     public function denormalize($data$class$format null, array $context = [])
  92.     {
  93.         throw new LogicException(sprintf('%s is a read-only format.'self::FORMAT));
  94.     }
  95. }
  96. class_alias(ObjectNormalizer::class, \ApiPlatform\Core\Hal\Serializer\ObjectNormalizer::class);