vendor/api-platform/core/src/Serializer/SerializerContextBuilder.php line 41

  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\Serializer;
  12. use ApiPlatform\Doctrine\Orm\State\Options;
  13. use ApiPlatform\Metadata\CollectionOperationInterface;
  14. use ApiPlatform\Metadata\Error as ErrorOperation;
  15. use ApiPlatform\Metadata\Exception\RuntimeException;
  16. use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
  17. use ApiPlatform\Symfony\Util\RequestAttributesExtractor;
  18. use Symfony\Component\HttpFoundation\Request;
  19. use Symfony\Component\Serializer\Encoder\CsvEncoder;
  20. use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;
  21. use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
  22. /**
  23.  * {@inheritdoc}
  24.  *
  25.  * @author Kévin Dunglas <dunglas@gmail.com>
  26.  */
  27. final class SerializerContextBuilder implements SerializerContextBuilderInterface
  28. {
  29.     public function __construct(private readonly ?ResourceMetadataCollectionFactoryInterface $resourceMetadataFactory null, private readonly bool $debug false)
  30.     {
  31.     }
  32.     /**
  33.      * {@inheritdoc}
  34.      */
  35.     public function createFromRequest(Request $requestbool $normalization, array $attributes null): array
  36.     {
  37.         if (null === $attributes && !$attributes RequestAttributesExtractor::extractAttributes($request)) {
  38.             throw new RuntimeException('Request attributes are not valid.');
  39.         }
  40.         if (!($operation $attributes['operation'] ?? null)) {
  41.             if (!$this->resourceMetadataFactory) {
  42.                 throw new RuntimeException('No operation');
  43.             }
  44.             $operation $this->resourceMetadataFactory->create($attributes['resource_class'])->getOperation($attributes['operation_name'] ?? null);
  45.         }
  46.         $context $normalization ? ($operation->getNormalizationContext() ?? []) : ($operation->getDenormalizationContext() ?? []);
  47.         $context['operation_name'] = $operation->getName();
  48.         $context['operation'] = $operation;
  49.         $context['resource_class'] = $attributes['resource_class'];
  50.         $context['skip_null_values'] ??= true;
  51.         $context['iri_only'] ??= false;
  52.         $context['request_uri'] = $request->getRequestUri();
  53.         $context['uri'] = $request->getUri();
  54.         $context['input'] = $operation->getInput();
  55.         $context['output'] = $operation->getOutput();
  56.         // Special case as this is usually handled by our OperationContextTrait, here we want to force the IRI in the response
  57.         if (!$operation instanceof CollectionOperationInterface && method_exists($operation'getItemUriTemplate') && $operation->getItemUriTemplate()) {
  58.             $context['item_uri_template'] = $operation->getItemUriTemplate();
  59.         }
  60.         if ($types $operation->getTypes()) {
  61.             $context['types'] = $types;
  62.         }
  63.         // TODO: remove this as uri variables are available in the SerializerProcessor but correctly parsed
  64.         if ($operation->getUriVariables()) {
  65.             $context['uri_variables'] = [];
  66.             foreach (array_keys($operation->getUriVariables()) as $parameterName) {
  67.                 $context['uri_variables'][$parameterName] = $request->attributes->get($parameterName);
  68.             }
  69.         }
  70.         if (($options $operation?->getStateOptions()) && $options instanceof Options && $options->getEntityClass()) {
  71.             $context['force_resource_class'] = $operation->getClass();
  72.         }
  73.         if ($this->debug && isset($context['groups']) && $operation instanceof ErrorOperation) {
  74.             if (!\is_array($context['groups'])) {
  75.                 $context['groups'] = (array) $context['groups'];
  76.             }
  77.             $context['groups'][] = 'trace';
  78.         }
  79.         if (!$normalization) {
  80.             if (!isset($context['api_allow_update'])) {
  81.                 $context['api_allow_update'] = \in_array($method $request->getMethod(), ['PUT''PATCH'], true);
  82.                 if ($context['api_allow_update'] && 'PATCH' === $method) {
  83.                     $context['deep_object_to_populate'] ??= true;
  84.                 }
  85.             }
  86.             if ('csv' === (method_exists(Request::class, 'getContentTypeFormat') ? $request->getContentTypeFormat() : $request->getContentType())) {
  87.                 $context[CsvEncoder::AS_COLLECTION_KEY] = false;
  88.             }
  89.         }
  90.         if ($operation->getCollectDenormalizationErrors() ?? false) {
  91.             $context[DenormalizerInterface::COLLECT_DENORMALIZATION_ERRORS] = true;
  92.         }
  93.         // to keep the cache computation smaller, we have "operation_name" and "iri" anyways
  94.         $context[AbstractObjectNormalizer::EXCLUDE_FROM_CACHE_KEY][] = 'root_operation';
  95.         $context[AbstractObjectNormalizer::EXCLUDE_FROM_CACHE_KEY][] = 'operation';
  96.         // JSON API see JsonApiProvider
  97.         if ($included $request->attributes->get('_api_included')) {
  98.             $context['api_included'] = $included;
  99.         }
  100.         return $context;
  101.     }
  102. }