vendor/shopware/core/Framework/DataAbstractionLayer/Entity.php line 55

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Framework\DataAbstractionLayer;
  3. use Shopware\Core\Framework\DataAbstractionLayer\Exception\InternalFieldAccessNotAllowedException;
  4. use Shopware\Core\Framework\Struct\ArrayEntity;
  5. use Shopware\Core\Framework\Struct\ArrayStruct;
  6. use Shopware\Core\Framework\Struct\Struct;
  7. class Entity extends Struct
  8. {
  9.     /**
  10.      * @var string
  11.      */
  12.     protected $_uniqueIdentifier;
  13.     /**
  14.      * @var string|null
  15.      */
  16.     protected $versionId;
  17.     /**
  18.      * @var array
  19.      */
  20.     protected $translated = [];
  21.     /**
  22.      * @var \DateTimeInterface|null
  23.      */
  24.     protected $createdAt;
  25.     /**
  26.      * @var \DateTimeInterface|null
  27.      */
  28.     protected $updatedAt;
  29.     /**
  30.      * @var string
  31.      */
  32.     private $_entityName;
  33.     private ?FieldVisibility $_fieldVisibility null;
  34.     public function __get($name)
  35.     {
  36.         if (FieldVisibility::$isInTwigRenderingContext) {
  37.             $this->checkIfPropertyAccessIsAllowed($name);
  38.         }
  39.         return $this->$name;
  40.     }
  41.     public function __set($name$value): void
  42.     {
  43.         $this->$name $value;
  44.     }
  45.     public function __isset($name)
  46.     {
  47.         if (FieldVisibility::$isInTwigRenderingContext) {
  48.             if (!$this->isPropertyVisible($name)) {
  49.                 return false;
  50.             }
  51.         }
  52.         return isset($this->$name);
  53.     }
  54.     public function setUniqueIdentifier(string $identifier): void
  55.     {
  56.         $this->_uniqueIdentifier $identifier;
  57.     }
  58.     public function getUniqueIdentifier(): string
  59.     {
  60.         return $this->_uniqueIdentifier;
  61.     }
  62.     public function getVersionId(): ?string
  63.     {
  64.         return $this->versionId;
  65.     }
  66.     public function setVersionId(string $versionId): void
  67.     {
  68.         $this->versionId $versionId;
  69.     }
  70.     /**
  71.      * @return mixed|Struct|null
  72.      */
  73.     public function get(string $property)
  74.     {
  75.         if (FieldVisibility::$isInTwigRenderingContext) {
  76.             $this->checkIfPropertyAccessIsAllowed($property);
  77.         }
  78.         if ($this->has($property)) {
  79.             return $this->$property;
  80.         }
  81.         if ($this->hasExtension($property)) {
  82.             return $this->getExtension($property);
  83.         }
  84.         /** @var ArrayStruct<string, mixed>|null $extension */
  85.         $extension $this->getExtension('foreignKeys');
  86.         if ($extension && $extension instanceof ArrayStruct && $extension->has($property)) {
  87.             return $extension->get($property);
  88.         }
  89.         throw new \InvalidArgumentException(
  90.             sprintf('Property %s do not exist in class %s'$property, static::class)
  91.         );
  92.     }
  93.     public function has(string $property): bool
  94.     {
  95.         if (FieldVisibility::$isInTwigRenderingContext) {
  96.             if (!$this->isPropertyVisible($property)) {
  97.                 return false;
  98.             }
  99.         }
  100.         return property_exists($this$property);
  101.     }
  102.     public function getTranslated(): array
  103.     {
  104.         return $this->translated;
  105.     }
  106.     /**
  107.      * @return mixed|null
  108.      */
  109.     public function getTranslation(string $field)
  110.     {
  111.         return $this->translated[$field] ?? null;
  112.     }
  113.     public function setTranslated(array $translated): void
  114.     {
  115.         $this->translated $translated;
  116.     }
  117.     /**
  118.      * @param mixed $value
  119.      */
  120.     public function addTranslated(string $key$value): void
  121.     {
  122.         $this->translated[$key] = $value;
  123.     }
  124.     public function getCreatedAt(): ?\DateTimeInterface
  125.     {
  126.         return $this->createdAt;
  127.     }
  128.     public function setCreatedAt(\DateTimeInterface $createdAt): void
  129.     {
  130.         $this->createdAt $createdAt;
  131.     }
  132.     public function getUpdatedAt(): ?\DateTimeInterface
  133.     {
  134.         return $this->updatedAt;
  135.     }
  136.     public function setUpdatedAt(\DateTimeInterface $updatedAt): void
  137.     {
  138.         $this->updatedAt $updatedAt;
  139.     }
  140.     /**
  141.      * @return array<mixed>
  142.      */
  143.     public function jsonSerialize(): array
  144.     {
  145.         $data parent::jsonSerialize();
  146.         unset($data['_entityName']);
  147.         unset($data['_fieldVisibility']);
  148.         $data $this->filterInvisibleFields($data);
  149.         if (!$this->hasExtension('foreignKeys')) {
  150.             return $data;
  151.         }
  152.         $extension $this->getExtension('foreignKeys');
  153.         if (!$extension instanceof ArrayEntity) {
  154.             return $data;
  155.         }
  156.         foreach ($extension->all() as $key => $value) {
  157.             if (\array_key_exists($key$data)) {
  158.                 continue;
  159.             }
  160.             $data[$key] = $value;
  161.         }
  162.         return $data;
  163.     }
  164.     public function getVars(): array
  165.     {
  166.         $data parent::getVars();
  167.         return $this->filterInvisibleFields($data);
  168.     }
  169.     public function getApiAlias(): string
  170.     {
  171.         if ($this->_entityName !== null) {
  172.             return $this->_entityName;
  173.         }
  174.         $class = static::class;
  175.         $class explode('\\'$class);
  176.         $class end($class);
  177.         /** @var string $entityName */
  178.         $entityName preg_replace(
  179.             '/_entity$/',
  180.             '',
  181.             ltrim(mb_strtolower((string) preg_replace('/[A-Z]/''_$0'$class)), '_')
  182.         );
  183.         $this->_entityName $entityName;
  184.         return $entityName;
  185.     }
  186.     /**
  187.      * @internal
  188.      */
  189.     public function internalSetEntityData(string $entityNameFieldVisibility $fieldVisibility): self
  190.     {
  191.         $this->_entityName $entityName;
  192.         $this->_fieldVisibility $fieldVisibility;
  193.         return $this;
  194.     }
  195.     /**
  196.      * @deprecated tag:v6.5.0 - reason:becomes-internal - will be marked as internal
  197.      */
  198.     public function getInternalEntityName(): ?string
  199.     {
  200.         return $this->_entityName;
  201.     }
  202.     /**
  203.      * @internal
  204.      */
  205.     protected function filterInvisibleFields(array $data): array
  206.     {
  207.         if (!$this->_fieldVisibility) {
  208.             return $data;
  209.         }
  210.         return $this->_fieldVisibility->filterInvisible($data);
  211.     }
  212.     /**
  213.      * @internal
  214.      */
  215.     protected function checkIfPropertyAccessIsAllowed(string $property): void
  216.     {
  217.         if (!$this->isPropertyVisible($property)) {
  218.             throw new InternalFieldAccessNotAllowedException($property$this);
  219.         }
  220.     }
  221.     /**
  222.      * @internal
  223.      */
  224.     protected function isPropertyVisible(string $property): bool
  225.     {
  226.         if (!$this->_fieldVisibility) {
  227.             return true;
  228.         }
  229.         return $this->_fieldVisibility->isVisible($property);
  230.     }
  231. }