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

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