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

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