vendor/shopware/core/Framework/Api/Converter/DefaultApiConverter.php line 40

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Framework\Api\Converter;
  3. use Shopware\Core\Framework\DataAbstractionLayer\DefinitionInstanceRegistry;
  4. use Shopware\Core\Framework\DataAbstractionLayer\Field\Flag\Deprecated;
  5. use Shopware\Core\Framework\Feature;
  6. use Shopware\Core\PlatformRequest;
  7. use Symfony\Component\HttpFoundation\RequestStack;
  8. /**
  9.  * @deprecated tag:v6.5.0 - Will be removed. Api payloads will be no longer converted over the Deprecated flag
  10.  */
  11. class DefaultApiConverter
  12. {
  13.     /**
  14.      * @var DefinitionInstanceRegistry
  15.      */
  16.     private $definitionInstanceRegistry;
  17.     /**
  18.      * @var array
  19.      */
  20.     private $deprecations;
  21.     /**
  22.      * @var RequestStack
  23.      */
  24.     private $requestStack;
  25.     /**
  26.      * @internal
  27.      */
  28.     public function __construct(DefinitionInstanceRegistry $definitionInstanceRegistryRequestStack $requestStack)
  29.     {
  30.         $this->definitionInstanceRegistry $definitionInstanceRegistry;
  31.         $this->requestStack $requestStack;
  32.     }
  33.     public function convert(string $entityName, array $payload): array
  34.     {
  35.         Feature::triggerDeprecationOrThrow(
  36.             'v6.5.0.0',
  37.             Feature::deprecatedClassMessage(__CLASS__'v6.5.0.0')
  38.         );
  39.         $definition $this->definitionInstanceRegistry->getByEntityName($entityName);
  40.         $fields $definition->getFields()->filterByFlag(Deprecated::class);
  41.         if ($fields->count() === 0) {
  42.             return $payload;
  43.         }
  44.         foreach ($fields as $field) {
  45.             /** @var Deprecated|null $deprecated */
  46.             $deprecated $field->getFlag(Deprecated::class);
  47.             if ($deprecated === null) {
  48.                 continue;
  49.             }
  50.             if ($deprecated->getReplaceBy() === null) {
  51.                 continue;
  52.             }
  53.             // When the user sends both fields. The prefer the replaceBy field
  54.             if (isset($payload[$field->getPropertyName()], $payload[$deprecated->getReplaceBy()])) {
  55.                 unset($payload[$field->getPropertyName()]);
  56.             }
  57.         }
  58.         return $payload;
  59.     }
  60.     public function isDeprecated(string $entityName, ?string $fieldName null): bool
  61.     {
  62.         Feature::triggerDeprecationOrThrow(
  63.             'v6.5.0.0',
  64.             Feature::deprecatedClassMessage(__CLASS__'v6.5.0.0')
  65.         );
  66.         if ($this->ignoreDeprecations()) {
  67.             return false;
  68.         }
  69.         if ($fieldName === null) {
  70.             return \array_key_exists($entityName$this->getDeprecations()) && !\is_array($this->getDeprecations()[$entityName]);
  71.         }
  72.         return \in_array($fieldName$this->getDeprecations()[$entityName] ?? [], true);
  73.     }
  74.     protected function getDeprecations(): array
  75.     {
  76.         Feature::triggerDeprecationOrThrow(
  77.             'v6.5.0.0',
  78.             Feature::deprecatedClassMessage(__CLASS__'v6.5.0.0')
  79.         );
  80.         if ($this->deprecations !== null) {
  81.             return $this->deprecations;
  82.         }
  83.         foreach ($this->definitionInstanceRegistry->getDefinitions() as $definition) {
  84.             $this->deprecations[$definition->getEntityName()] = [];
  85.             $fields $definition->getFields()->filterByFlag(Deprecated::class);
  86.             foreach ($fields as $field) {
  87.                 $this->deprecations[$definition->getEntityName()][] = $field->getPropertyName();
  88.             }
  89.         }
  90.         return $this->deprecations;
  91.     }
  92.     protected function ignoreDeprecations(): bool
  93.     {
  94.         Feature::triggerDeprecationOrThrow(
  95.             'v6.5.0.0',
  96.             Feature::deprecatedClassMessage(__CLASS__'v6.5.0.0')
  97.         );
  98.         // We don't have a request
  99.         if ($this->requestStack->getMainRequest() === null) {
  100.             return false;
  101.         }
  102.         return $this->requestStack->getMainRequest()->headers->get(PlatformRequest::HEADER_IGNORE_DEPRECATIONS) === 'true';
  103.     }
  104. }