vendor/shopware/core/Framework/DataAbstractionLayer/Field/Field.php line 119

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Framework\DataAbstractionLayer\Field;
  3. use Shopware\Core\Framework\Api\Context\AdminApiSource;
  4. use Shopware\Core\Framework\DataAbstractionLayer\Dbal\FieldAccessorBuilder\DefaultFieldAccessorBuilder;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Dbal\FieldAccessorBuilder\FieldAccessorBuilderInterface;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Dbal\FieldResolver\AbstractFieldResolver;
  7. use Shopware\Core\Framework\DataAbstractionLayer\DefinitionInstanceRegistry;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Field\Flag\ApiAware;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Field\Flag\Flag;
  10. use Shopware\Core\Framework\DataAbstractionLayer\FieldSerializer\FieldSerializerInterface;
  11. use Shopware\Core\Framework\Log\Package;
  12. use Shopware\Core\Framework\Struct\Struct;
  13. #[Package('core')]
  14. abstract class Field extends Struct
  15. {
  16.     /**
  17.      * @var Flag[]
  18.      */
  19.     protected $flags = [];
  20.     /**
  21.      * @var string
  22.      */
  23.     protected $propertyName;
  24.     /**
  25.      * @var FieldSerializerInterface
  26.      */
  27.     private $serializer;
  28.     /**
  29.      * @var AbstractFieldResolver|null
  30.      */
  31.     private $resolver;
  32.     /**
  33.      * @var FieldAccessorBuilderInterface|null
  34.      */
  35.     private $accessorBuilder;
  36.     /**
  37.      * @var DefinitionInstanceRegistry
  38.      */
  39.     private $registry;
  40.     public function __construct(string $propertyName)
  41.     {
  42.         $this->propertyName $propertyName;
  43.         $this->addFlags(new ApiAware(AdminApiSource::class));
  44.     }
  45.     public function compile(DefinitionInstanceRegistry $registry): void
  46.     {
  47.         $this->registry $registry;
  48.     }
  49.     public function getPropertyName(): string
  50.     {
  51.         return $this->propertyName;
  52.     }
  53.     public function getExtractPriority(): int
  54.     {
  55.         return 0;
  56.     }
  57.     public function setFlags(Flag ...$flags): self
  58.     {
  59.         $this->flags = [];
  60.         foreach ($flags as $flag) {
  61.             $this->flags[\get_class($flag)] = $flag;
  62.         }
  63.         if (!$this->is(ApiAware::class)) {
  64.             $this->addFlags(new ApiAware(AdminApiSource::class));
  65.         }
  66.         return $this;
  67.     }
  68.     public function addFlags(Flag ...$flags): self
  69.     {
  70.         foreach ($flags as $flag) {
  71.             $this->flags[\get_class($flag)] = $flag;
  72.         }
  73.         return $this;
  74.     }
  75.     public function removeFlag(string $class): self
  76.     {
  77.         unset($this->flags[$class]);
  78.         return $this;
  79.     }
  80.     public function is(string $class): bool
  81.     {
  82.         return $this->getFlag($class) !== null;
  83.     }
  84.     public function getFlag(string $class): ?Flag
  85.     {
  86.         return $this->flags[$class] ?? null;
  87.     }
  88.     /**
  89.      * @return Flag[]
  90.      */
  91.     public function getFlags(): array
  92.     {
  93.         return array_values($this->flags);
  94.     }
  95.     public function getSerializer(): FieldSerializerInterface
  96.     {
  97.         $this->initLazy();
  98.         return $this->serializer;
  99.     }
  100.     /**
  101.      * @return AbstractFieldResolver|null
  102.      */
  103.     public function getResolver()
  104.     {
  105.         $this->initLazy();
  106.         return $this->resolver;
  107.     }
  108.     public function getAccessorBuilder(): ?FieldAccessorBuilderInterface
  109.     {
  110.         $this->initLazy();
  111.         return $this->accessorBuilder;
  112.     }
  113.     public function isCompiled(): bool
  114.     {
  115.         return $this->registry !== null;
  116.     }
  117.     abstract protected function getSerializerClass(): string;
  118.     protected function getResolverClass(): ?string
  119.     {
  120.         return null;
  121.     }
  122.     protected function getAccessorBuilderClass(): ?string
  123.     {
  124.         if ($this instanceof StorageAware) {
  125.             return DefaultFieldAccessorBuilder::class;
  126.         }
  127.         return null;
  128.     }
  129.     private function initLazy(): void
  130.     {
  131.         if ($this->serializer !== null) {
  132.             return;
  133.         }
  134.         $this->serializer $this->registry->getSerializer($this->getSerializerClass());
  135.         $resolverClass $this->getResolverClass();
  136.         if ($resolverClass !== null) {
  137.             $this->resolver $this->registry->getResolver($resolverClass);
  138.         }
  139.         $accessorBuilderClass $this->getAccessorBuilderClass();
  140.         if ($accessorBuilderClass !== null) {
  141.             $this->accessorBuilder $this->registry->getAccessorBuilder($accessorBuilderClass);
  142.         }
  143.     }
  144. }