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

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