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 NormalizerInterface, CacheableSupportsMethodInterface
  23. {
  24. use JsonLdContextTrait;
  25. public const FORMAT = 'jsonld';
  26. private $decorated;
  27. private $iriConverter;
  28. private $anonymousContextBuilder;
  29. public function __construct(NormalizerInterface $decorated, $iriConverter, AnonymousContextBuilderInterface $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. public function supportsNormalization($data, $format = null, array $context = []): bool
  39. {
  40. return self::FORMAT === $format && $this->decorated->supportsNormalization($data, $format, $context);
  41. }
  42. public function hasCacheableSupportsMethod(): bool
  43. {
  44. return $this->decorated instanceof CacheableSupportsMethodInterface && $this->decorated->hasCacheableSupportsMethod();
  45. }
  46. /**
  47. * @param mixed|null $format
  48. *
  49. * @return array|string|int|float|bool|\ArrayObject|null
  50. */
  51. public function normalize($object, $format = null, array $context = [])
  52. {
  53. if (isset($context['api_resource'])) {
  54. $originalResource = $context['api_resource'];
  55. unset($context['api_resource']);
  56. }
  57. /*
  58. * Converts the normalized data array of a resource into an IRI, if the
  59. * normalized data array is empty.
  60. *
  61. * This is useful when traversing from a non-resource towards an attribute
  62. * which is a resource, as we do not have the benefit of {@see PropertyMetadata::isReadableLink}.
  63. *
  64. * It must not be propagated to subresources, as {@see PropertyMetadata::isReadableLink}
  65. * should take effect.
  66. */
  67. $context['api_empty_resource_as_iri'] = true;
  68. $data = $this->decorated->normalize($object, $format, $context);
  69. if (!\is_array($data) || !$data) {
  70. return $data;
  71. }
  72. if (isset($originalResource)) {
  73. try {
  74. $context['output']['iri'] = $this->iriConverter instanceof LegacyIriConverterInterface ? $this->iriConverter->getIriFromItem($originalResource) : $this->iriConverter->getIriFromResource($originalResource);
  75. } catch (InvalidArgumentException $e) {
  76. // The original resource has no identifiers
  77. }
  78. $context['api_resource'] = $originalResource;
  79. }
  80. $metadata = $this->createJsonLdContext($this->anonymousContextBuilder, $object, $context);
  81. return $metadata + $data;
  82. }
  83. }
  84. class_alias(ObjectNormalizer::class, \ApiPlatform\Core\JsonLd\Serializer\ObjectNormalizer::class);