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

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