vendor/pimcore/pimcore/models/DataObject/Fieldcollection/Data/AbstractData.php line 119

Open in your IDE?
  1. <?php
  2. /**
  3.  * Pimcore
  4.  *
  5.  * This source file is available under two different licenses:
  6.  * - GNU General Public License version 3 (GPLv3)
  7.  * - Pimcore Commercial License (PCL)
  8.  * Full copyright and license information is available in
  9.  * LICENSE.md which is distributed with this source code.
  10.  *
  11.  *  @copyright  Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  12.  *  @license    http://www.pimcore.org/license     GPLv3 and PCL
  13.  */
  14. namespace Pimcore\Model\DataObject\Fieldcollection\Data;
  15. use Pimcore\Model;
  16. use Pimcore\Model\DataObject\ClassDefinition\Data\LazyLoadingSupportInterface;
  17. use Pimcore\Model\DataObject\Concrete;
  18. use Pimcore\Model\DataObject\Localizedfield;
  19. use Pimcore\Model\DataObject\ObjectAwareFieldInterface;
  20. /**
  21.  * @method Dao getDao()
  22.  */
  23. abstract class AbstractData extends Model\AbstractModel implements Model\DataObject\LazyLoadedFieldsInterfaceModel\Element\ElementDumpStateInterfaceModel\Element\DirtyIndicatorInterfaceObjectAwareFieldInterface
  24. {
  25.     use Model\Element\ElementDumpStateTrait;
  26.     use Model\DataObject\Traits\LazyLoadedRelationTrait;
  27.     use Model\Element\Traits\DirtyIndicatorTrait;
  28.     /**
  29.      * @var int
  30.      */
  31.     protected $index;
  32.     /**
  33.      * @var string|null
  34.      */
  35.     protected $fieldname;
  36.     /**
  37.      * @var Concrete|Model\Element\ElementDescriptor|null
  38.      */
  39.     protected $object;
  40.     /**
  41.      * @var int|null
  42.      */
  43.     protected ?int $objectId null;
  44.     /**
  45.      * @var string
  46.      */
  47.     protected $type;
  48.     /**
  49.      * @return int
  50.      */
  51.     public function getIndex()
  52.     {
  53.         return $this->index;
  54.     }
  55.     /**
  56.      * @param int $index
  57.      *
  58.      * @return $this
  59.      */
  60.     public function setIndex($index)
  61.     {
  62.         $this->index = (int) $index;
  63.         return $this;
  64.     }
  65.     /**
  66.      * @return string|null
  67.      */
  68.     public function getFieldname()
  69.     {
  70.         return $this->fieldname;
  71.     }
  72.     /**
  73.      * @param string|null $fieldname
  74.      *
  75.      * @return $this
  76.      */
  77.     public function setFieldname($fieldname)
  78.     {
  79.         $this->fieldname $fieldname;
  80.         return $this;
  81.     }
  82.     /**
  83.      * @return string
  84.      */
  85.     public function getType()
  86.     {
  87.         return $this->type;
  88.     }
  89.     /**
  90.      * @return Model\DataObject\Fieldcollection\Definition
  91.      */
  92.     public function getDefinition()
  93.     {
  94.         return Model\DataObject\Fieldcollection\Definition::getByKey($this->getType());
  95.     }
  96.     /**
  97.      * @param Concrete|null $object
  98.      *
  99.      * @return $this
  100.      */
  101.     public function setObject(?Concrete $object)
  102.     {
  103.         $this->objectId $object $object->getId() : null;
  104.         $this->object $object;
  105.         if (property_exists($this'localizedfields') && $this->localizedfields instanceof Localizedfield) {
  106.             $this->localizedfields->setObject($objectfalse);
  107.         }
  108.         return $this;
  109.     }
  110.     /**
  111.      * @return Concrete|null
  112.      */
  113.     public function getObject(): ?Concrete
  114.     {
  115.         if ($this->objectId && !$this->object) {
  116.             $this->setObject(Concrete::getById($this->objectId));
  117.         }
  118.         return $this->object;
  119.     }
  120.     /**
  121.      * @param string $fieldName
  122.      * @param string|null $language
  123.      *
  124.      * @return mixed
  125.      */
  126.     public function get($fieldName$language null)
  127.     {
  128.         return $this->{'get'.ucfirst($fieldName)}($language);
  129.     }
  130.     /**
  131.      * @param string $fieldName
  132.      * @param mixed $value
  133.      * @param string|null $language
  134.      *
  135.      * @return mixed
  136.      */
  137.     public function set($fieldName$value$language null)
  138.     {
  139.         return $this->{'set'.ucfirst($fieldName)}($value$language);
  140.     }
  141.     /**
  142.      * @internal
  143.      *
  144.      * @return array
  145.      */
  146.     protected function getLazyLoadedFieldNames(): array
  147.     {
  148.         $lazyLoadedFieldNames = [];
  149.         $fields $this->getDefinition()->getFieldDefinitions(['suppressEnrichment' => true]);
  150.         foreach ($fields as $field) {
  151.             if ($field instanceof LazyLoadingSupportInterface && $field->getLazyLoading()) {
  152.                 $lazyLoadedFieldNames[] = $field->getName();
  153.             }
  154.         }
  155.         return $lazyLoadedFieldNames;
  156.     }
  157.     /**
  158.      * {@inheritdoc}
  159.      */
  160.     public function isAllLazyKeysMarkedAsLoaded(): bool
  161.     {
  162.         $object $this->getObject();
  163.         if ($object instanceof Concrete) {
  164.             return $this->getObject()->isAllLazyKeysMarkedAsLoaded();
  165.         }
  166.         return true;
  167.     }
  168.     /**
  169.      * @return array
  170.      */
  171.     public function __sleep()
  172.     {
  173.         $parentVars parent::__sleep();
  174.         $blockedVars = ['loadedLazyKeys''object'];
  175.         $finalVars = [];
  176.         if (!$this->isInDumpState()) {
  177.             //Remove all lazy loaded fields if item gets serialized for the cache (not for versions)
  178.             $blockedVars array_merge($this->getLazyLoadedFieldNames(), $blockedVars);
  179.         }
  180.         foreach ($parentVars as $key) {
  181.             if (!in_array($key$blockedVars)) {
  182.                 $finalVars[] = $key;
  183.             }
  184.         }
  185.         return $finalVars;
  186.     }
  187.     public function __wakeup()
  188.     {
  189.         if ($this->object) {
  190.             $this->objectId $this->object->getId();
  191.         }
  192.     }
  193. }