vendor/api-platform/core/src/JsonLd/Serializer/ObjectNormalizer.php line 44

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\JsonLd\Serializer;
  12. use ApiPlatform\Api\IriConverterInterface;
  13. use ApiPlatform\Core\Api\IriConverterInterface as LegacyIriConverterInterface;
  14. use ApiPlatform\Exception\InvalidArgumentException;
  15. use ApiPlatform\JsonLd\AnonymousContextBuilderInterface;
  16. use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface;
  17. use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
  18. /**
  19.  * Decorates the output with JSON-LD metadata when appropriate, but otherwise just
  20.  * passes through to the decorated normalizer.
  21.  */
  22. final class ObjectNormalizer implements NormalizerInterfaceCacheableSupportsMethodInterface
  23. {
  24.     use JsonLdContextTrait;
  25.     public const FORMAT 'jsonld';
  26.     private $decorated;
  27.     private $iriConverter;
  28.     private $anonymousContextBuilder;
  29.     public function __construct(NormalizerInterface $decorated$iriConverterAnonymousContextBuilderInterface $anonymousContextBuilder)
  30.     {
  31.         $this->decorated $decorated;
  32.         $this->iriConverter $iriConverter;
  33.         $this->anonymousContextBuilder $anonymousContextBuilder;
  34.         if ($iriConverter instanceof LegacyIriConverterInterface) {
  35.             trigger_deprecation('api-platform/core''2.7'sprintf('Use an implementation of "%s" instead of "%s".'IriConverterInterface::class, LegacyIriConverterInterface::class));
  36.         }
  37.     }
  38.     /**
  39.      * {@inheritdoc}
  40.      */
  41.     public function supportsNormalization($data$format null, array $context = []): bool
  42.     {
  43.         return self::FORMAT === $format && $this->decorated->supportsNormalization($data$format$context);
  44.     }
  45.     /**
  46.      * {@inheritdoc}
  47.      */
  48.     public function hasCacheableSupportsMethod(): bool
  49.     {
  50.         return $this->decorated instanceof CacheableSupportsMethodInterface && $this->decorated->hasCacheableSupportsMethod();
  51.     }
  52.     /**
  53.      * {@inheritdoc}
  54.      *
  55.      * @return array|string|int|float|bool|\ArrayObject|null
  56.      */
  57.     public function normalize($object$format null, array $context = [])
  58.     {
  59.         if (isset($context['api_resource'])) {
  60.             $originalResource $context['api_resource'];
  61.             unset($context['api_resource']);
  62.         }
  63.         /*
  64.          * Converts the normalized data array of a resource into an IRI, if the
  65.          * normalized data array is empty.
  66.          *
  67.          * This is useful when traversing from a non-resource towards an attribute
  68.          * which is a resource, as we do not have the benefit of {@see PropertyMetadata::isReadableLink}.
  69.          *
  70.          * It must not be propagated to subresources, as {@see PropertyMetadata::isReadableLink}
  71.          * should take effect.
  72.          */
  73.         $context['api_empty_resource_as_iri'] = true;
  74.         $data $this->decorated->normalize($object$format$context);
  75.         if (!\is_array($data) || !$data) {
  76.             return $data;
  77.         }
  78.         if (isset($originalResource)) {
  79.             try {
  80.                 $context['output']['iri'] = $this->iriConverter instanceof LegacyIriConverterInterface $this->iriConverter->getIriFromItem($originalResource) : $this->iriConverter->getIriFromResource($originalResource);
  81.             } catch (InvalidArgumentException $e) {
  82.                 // The original resource has no identifiers
  83.             }
  84.             $context['api_resource'] = $originalResource;
  85.         }
  86.         $metadata $this->createJsonLdContext($this->anonymousContextBuilder$object$context);
  87.         return $metadata $data;
  88.     }
  89. }
  90. class_alias(ObjectNormalizer::class, \ApiPlatform\Core\JsonLd\Serializer\ObjectNormalizer::class);