vendor/shopware/core/Framework/Api/Converter/ApiVersionConverter.php line 82

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Framework\Api\Converter;
  3. use Shopware\Core\Framework\Api\Converter\Exceptions\ApiConversionException;
  4. use Shopware\Core\Framework\DataAbstractionLayer\Entity;
  5. use Shopware\Core\Framework\DataAbstractionLayer\EntityDefinition;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Field\Field;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Field\ManyToManyAssociationField;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Field\ManyToOneAssociationField;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Field\OneToManyAssociationField;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Field\OneToOneAssociationField;
  11. /**
  12.  * @package core
  13.  */
  14. class ApiVersionConverter
  15. {
  16.     /**
  17.      * @var ConverterRegistry
  18.      */
  19.     private $converterRegistry;
  20.     /**
  21.      * @internal
  22.      */
  23.     public function __construct(ConverterRegistry $converterRegistry)
  24.     {
  25.         $this->converterRegistry $converterRegistry;
  26.     }
  27.     public function convertEntity(EntityDefinition $definitionEntity $entity): array
  28.     {
  29.         return $entity->jsonSerialize();
  30.     }
  31.     public function convertPayload(EntityDefinition $definition, array $payloadApiConversionException $conversionExceptionstring $pointer ''): array
  32.     {
  33.         $toOneFields $definition->getFields()->filter(function (Field $field) {
  34.             return $field instanceof OneToOneAssociationField || $field instanceof ManyToOneAssociationField;
  35.         });
  36.         /** @var OneToOneAssociationField|OneToManyAssociationField $field */
  37.         foreach ($toOneFields as $field) {
  38.             if (!\array_key_exists($field->getPropertyName(), $payload) || !\is_array($payload[$field->getPropertyName()])) {
  39.                 continue;
  40.             }
  41.             $payload[$field->getPropertyName()] = $this->convertPayload(
  42.                 $field->getReferenceDefinition(),
  43.                 $payload[$field->getPropertyName()],
  44.                 $conversionException,
  45.                 $pointer '/' $field->getPropertyName()
  46.             );
  47.         }
  48.         $toManyFields $definition->getFields()->filter(function (Field $field) {
  49.             return $field instanceof OneToManyAssociationField || $field instanceof ManyToManyAssociationField;
  50.         });
  51.         /** @var OneToManyAssociationField|ManyToManyAssociationField $field */
  52.         foreach ($toManyFields as $field) {
  53.             if (!\array_key_exists($field->getPropertyName(), $payload) || !\is_array($payload[$field->getPropertyName()])) {
  54.                 continue;
  55.             }
  56.             foreach ($payload[$field->getPropertyName()] as $key => $entityPayload) {
  57.                 $payload[$field->getPropertyName()][$key] = $this->convertPayload(
  58.                     $field instanceof ManyToManyAssociationField $field->getToManyReferenceDefinition() : $field->getReferenceDefinition(),
  59.                     $entityPayload,
  60.                     $conversionException,
  61.                     $pointer '/' $key '/' $field->getPropertyName()
  62.                 );
  63.             }
  64.         }
  65.         $payload $this->validateFields($definition$payload);
  66.         return $payload;
  67.     }
  68.     private function validateFields(EntityDefinition $definition, array $payload): array
  69.     {
  70.         return $this->converterRegistry->convert($definition->getEntityName(), $payload);
  71.     }
  72. }