vendor/pimcore/pimcore/models/Property.php line 25

  1. <?php
  2. declare(strict_types=1);
  3. /**
  4.  * Pimcore
  5.  *
  6.  * This source file is available under two different licenses:
  7.  * - GNU General Public License version 3 (GPLv3)
  8.  * - Pimcore Commercial License (PCL)
  9.  * Full copyright and license information is available in
  10.  * LICENSE.md which is distributed with this source code.
  11.  *
  12.  *  @copyright  Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  13.  *  @license    http://www.pimcore.org/license     GPLv3 and PCL
  14.  */
  15. namespace Pimcore\Model;
  16. use Pimcore\Model\Element\ElementInterface;
  17. use Pimcore\Model\Element\Service;
  18. /**
  19.  * @method \Pimcore\Model\Property\Dao getDao()
  20.  * @method void save()
  21.  */
  22. final class Property extends AbstractModel
  23. {
  24.     protected ?string $name null;
  25.     protected mixed $data null;
  26.     protected ?string $type null;
  27.     protected ?string $ctype null;
  28.     protected ?string $cpath null;
  29.     protected ?int $cid null;
  30.     protected bool $inheritable false;
  31.     protected bool $inherited false;
  32.     /**
  33.      * @internal
  34.      *
  35.      * @return $this
  36.      */
  37.     public function setDataFromEditmode(mixed $data): static
  38.     {
  39.         // IMPORTANT: if you use this method be sure that the type of the property is already set
  40.         if (in_array($this->getType(), ['document''asset''object'])) {
  41.             $el Element\Service::getElementByPath($this->getType(), (string)$data);
  42.             $this->data null;
  43.             if ($el) {
  44.                 $this->data $el->getId();
  45.             }
  46.         } elseif ($this->type == 'bool') {
  47.             $this->data false;
  48.             if (!empty($data)) {
  49.                 $this->data true;
  50.             }
  51.         } else {
  52.             // plain text
  53.             $this->data $data;
  54.         }
  55.         return $this;
  56.     }
  57.     /**
  58.      * @internal
  59.      *
  60.      * @return $this
  61.      */
  62.     public function setDataFromResource(mixed $data): static
  63.     {
  64.         // IMPORTANT: if you use this method be sure that the type of the property is already set
  65.         // do not set data for object, asset and document here, this is loaded dynamically when calling $this->getData();
  66.         if ($this->type == 'date') {
  67.             $this->data \Pimcore\Tool\Serialize::unserialize($data);
  68.         } elseif ($this->type == 'bool') {
  69.             $this->data false;
  70.             if (!empty($data)) {
  71.                 $this->data true;
  72.             }
  73.         } else {
  74.             // plain text
  75.             $this->data $data;
  76.         }
  77.         return $this;
  78.     }
  79.     public function getCid(): ?int
  80.     {
  81.         return $this->cid;
  82.     }
  83.     /**
  84.      * enum('document','asset','object')
  85.      */
  86.     public function getCtype(): ?string
  87.     {
  88.         return $this->ctype;
  89.     }
  90.     public function getData(): mixed
  91.     {
  92.         // lazy-load data of type asset, document, object
  93.         if (in_array($this->getType(), ['document''asset''object']) && !$this->data instanceof ElementInterface && is_numeric($this->data)) {
  94.             return Element\Service::getElementById($this->getType(), (int) $this->data);
  95.         }
  96.         return $this->data;
  97.     }
  98.     public function getName(): ?string
  99.     {
  100.         return $this->name;
  101.     }
  102.     /**
  103.      * enum('text','document','asset','object','bool','select')
  104.      */
  105.     public function getType(): ?string
  106.     {
  107.         return $this->type;
  108.     }
  109.     /**
  110.      * @return $this
  111.      */
  112.     public function setCid(int $cid): static
  113.     {
  114.         $this->cid $cid;
  115.         return $this;
  116.     }
  117.     /**
  118.      * enum('document','asset','object')
  119.      *
  120.      *
  121.      * @return $this
  122.      */
  123.     public function setCtype(string $ctype): static
  124.     {
  125.         $this->ctype $ctype;
  126.         return $this;
  127.     }
  128.     /**
  129.      * @return $this
  130.      */
  131.     public function setData(mixed $data): static
  132.     {
  133.         if ($data instanceof ElementInterface) {
  134.             $this->setType(Service::getElementType($data));
  135.             $data $data->getId();
  136.         }
  137.         $this->data $data;
  138.         return $this;
  139.     }
  140.     /**
  141.      * @return $this
  142.      */
  143.     public function setName(string $name): static
  144.     {
  145.         $this->name $name;
  146.         return $this;
  147.     }
  148.     /**
  149.      * enum('text','document','asset','object','bool','select')
  150.      *
  151.      *
  152.      * @return $this
  153.      */
  154.     public function setType(string $type): static
  155.     {
  156.         $this->type $type;
  157.         return $this;
  158.     }
  159.     public function getCpath(): ?string
  160.     {
  161.         return $this->cpath;
  162.     }
  163.     public function getInherited(): bool
  164.     {
  165.         return $this->inherited;
  166.     }
  167.     /**
  168.      * Alias for getInherited()
  169.      *
  170.      */
  171.     public function isInherited(): bool
  172.     {
  173.         return $this->getInherited();
  174.     }
  175.     /**
  176.      * @return $this
  177.      */
  178.     public function setCpath(?string $cpath): static
  179.     {
  180.         $this->cpath $cpath;
  181.         return $this;
  182.     }
  183.     /**
  184.      * @return $this
  185.      */
  186.     public function setInherited(bool $inherited): static
  187.     {
  188.         $this->inherited $inherited;
  189.         return $this;
  190.     }
  191.     public function getInheritable(): bool
  192.     {
  193.         return $this->inheritable;
  194.     }
  195.     /**
  196.      * @return $this
  197.      */
  198.     public function setInheritable(bool $inheritable): static
  199.     {
  200.         $this->inheritable $inheritable;
  201.         return $this;
  202.     }
  203.     /**
  204.      * @internal
  205.      *
  206.      */
  207.     public function resolveDependencies(): array
  208.     {
  209.         $dependencies = [];
  210.         if ($this->getData() instanceof ElementInterface) {
  211.             $elementType Element\Service::getElementType($this->getData());
  212.             $key $elementType '_' $this->getData()->getId();
  213.             $dependencies[$key] = [
  214.                 'id' => $this->getData()->getId(),
  215.                 'type' => $elementType,
  216.             ];
  217.         }
  218.         return $dependencies;
  219.     }
  220.     /**
  221.      * Rewrites id from source to target, $idMapping contains
  222.      * array(
  223.      *  "document" => array(
  224.      *      SOURCE_ID => TARGET_ID,
  225.      *      SOURCE_ID => TARGET_ID
  226.      *  ),
  227.      *  "object" => array(...),
  228.      *  "asset" => array(...)
  229.      * )
  230.      *
  231.      *
  232.      * @internal
  233.      */
  234.     public function rewriteIds(array $idMapping): void
  235.     {
  236.         if (!$this->isInherited()) {
  237.             if (array_key_exists($this->getType(), $idMapping)) {
  238.                 if ($this->getData() instanceof ElementInterface) {
  239.                     if (array_key_exists((int) $this->getData()->getId(), $idMapping[$this->getType()])) {
  240.                         $this->setData(Element\Service::getElementById($this->getType(), $idMapping[$this->getType()][$this->getData()->getId()]));
  241.                     }
  242.                 }
  243.             }
  244.         }
  245.     }
  246.     /**
  247.      * @internal
  248.      *
  249.      */
  250.     public function serialize(): array
  251.     {
  252.         return [
  253.           'name' => $this->getName(),
  254.           'type' => $this->getType(),
  255.           'data' => $this->getData(),
  256.         ];
  257.     }
  258. }