vendor/shopware/platform/src/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|null
  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|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.     public function getTranslation(string $field)
  107.     {
  108.         return $this->translated[$field] ?? null;
  109.     }
  110.     public function setTranslated(array $translated): void
  111.     {
  112.         $this->translated $translated;
  113.     }
  114.     public function addTranslated(string $key$value): void
  115.     {
  116.         $this->translated[$key] = $value;
  117.     }
  118.     public function getCreatedAt(): ?\DateTimeInterface
  119.     {
  120.         return $this->createdAt;
  121.     }
  122.     public function setCreatedAt(\DateTimeInterface $createdAt): void
  123.     {
  124.         $this->createdAt $createdAt;
  125.     }
  126.     public function getUpdatedAt(): ?\DateTimeInterface
  127.     {
  128.         return $this->updatedAt;
  129.     }
  130.     public function setUpdatedAt(\DateTimeInterface $updatedAt): void
  131.     {
  132.         $this->updatedAt $updatedAt;
  133.     }
  134.     public function jsonSerialize(): array
  135.     {
  136.         $data parent::jsonSerialize();
  137.         unset($data['_entityName']);
  138.         unset($data['_fieldVisibility']);
  139.         $data $this->filterInvisibleFields($data);
  140.         if (!$this->hasExtension('foreignKeys')) {
  141.             return $data;
  142.         }
  143.         $extension $this->getExtension('foreignKeys');
  144.         if (!$extension instanceof ArrayEntity) {
  145.             return $data;
  146.         }
  147.         foreach ($extension->all() as $key => $value) {
  148.             if (\array_key_exists($key$data)) {
  149.                 continue;
  150.             }
  151.             $data[$key] = $value;
  152.         }
  153.         return $data;
  154.     }
  155.     public function getVars(): array
  156.     {
  157.         $data parent::getVars();
  158.         return $this->filterInvisibleFields($data);
  159.     }
  160.     public function getApiAlias(): string
  161.     {
  162.         if ($this->_entityName !== null) {
  163.             return $this->_entityName;
  164.         }
  165.         $class = static::class;
  166.         $class explode('\\'$class);
  167.         $class end($class);
  168.         return $this->_entityName preg_replace(
  169.             '/_entity$/',
  170.             '',
  171.             ltrim(mb_strtolower((string) preg_replace('/[A-Z]/''_$0'$class)), '_')
  172.         );
  173.     }
  174.     /**
  175.      * @internal
  176.      */
  177.     public function internalSetEntityData(string $entityNameFieldVisibility $fieldVisibility): self
  178.     {
  179.         $this->_entityName $entityName;
  180.         $this->_fieldVisibility $fieldVisibility;
  181.         return $this;
  182.     }
  183.     /**
  184.      * @deprecated tag:v6.5.0 - will be marked as internal
  185.      */
  186.     public function getInternalEntityName(): ?string
  187.     {
  188.         return $this->_entityName;
  189.     }
  190.     /**
  191.      * @internal
  192.      */
  193.     protected function filterInvisibleFields(array $data): array
  194.     {
  195.         if (!$this->_fieldVisibility) {
  196.             return $data;
  197.         }
  198.         return $this->_fieldVisibility->filterInvisible($data);
  199.     }
  200.     /**
  201.      * @internal
  202.      */
  203.     protected function checkIfPropertyAccessIsAllowed(string $property): void
  204.     {
  205.         if (!$this->isPropertyVisible($property)) {
  206.             throw new InternalFieldAccessNotAllowedException($property$this);
  207.         }
  208.     }
  209.     /**
  210.      * @internal
  211.      */
  212.     protected function isPropertyVisible(string $property): bool
  213.     {
  214.         if (!$this->_fieldVisibility) {
  215.             return true;
  216.         }
  217.         return $this->_fieldVisibility->isVisible($property);
  218.     }
  219. }