‪TYPO3CMS  ‪main
LazyObjectStorage.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
25 
36 {
45  private string ‪$warning = 'You should never see this warning. If you do, you probably used PHP array functions like current() on the TYPO3\\CMS\\Extbase\\Persistence\\Generic\\LazyObjectStorage. To retrieve the first result, you can use the rewind() and current() methods.';
46 
48 
53 
57  protected string ‪$propertyName;
58 
62  protected mixed ‪$fieldValue;
63 
64  protected bool ‪$isInitialized = false;
65 
66  public function ‪isInitialized(): bool
67  {
69  }
70 
77  {
78  $this->parentObject = ‪$parentObject;
79  $this->propertyName = ‪$propertyName;
80  $this->fieldValue = ‪$fieldValue;
81  reset($this->storage);
82  if (‪$dataMapper === null) {
83  ‪$dataMapper = GeneralUtility::makeInstance(DataMapper::class);
84  }
85  $this->dataMapper = ‪$dataMapper;
86  }
87 
91  protected function ‪initialize(): void
92  {
93  if ($this->‪isInitialized) {
94  return;
95  }
96 
97  $this->‪isInitialized = true;
98  $objects = $this->dataMapper->fetchRelated($this->parentObject, $this->propertyName, $this->fieldValue, false);
99  foreach ($objects as $object) {
100  parent::attach($object);
101  }
102  $this->‪_memorizeCleanState();
104  $this->parentObject->_memorizeCleanState($this->propertyName);
105  }
106  }
107 
109  {
110  return $this->parentObject->_getCleanProperty($this->propertyName) === $this;
111  }
112 
113  // Delegation to the ObjectStorage methods below
114 
118  public function ‪addAll(‪ObjectStorage ‪$storage): void
119  {
120  $this->‪initialize();
121  parent::addAll(‪$storage);
122  }
123 
130  public function ‪attach(object $object, mixed $information = null): void
131  {
132  $this->‪initialize();
133  parent::attach($object, $information);
134  }
135 
141  public function ‪contains(object $object): bool
142  {
143  $this->‪initialize();
144  return parent::contains($object);
145  }
146 
153  public function ‪count(): int
154  {
155  $columnMap = $this->dataMapper->getDataMap(get_class($this->parentObject))->getColumnMap($this->propertyName);
156  if (!$this->‪isInitialized && $columnMap->getTypeOfRelation() === Relation::HAS_MANY) {
157  $numberOfElements = $this->dataMapper->countRelated($this->parentObject, $this->propertyName, $this->fieldValue);
158  } else {
159  $this->‪initialize();
160  $numberOfElements = ‪count($this->storage);
161  }
162  return $numberOfElements;
163  }
164 
169  public function ‪current(): ?object
170  {
171  $this->‪initialize();
172  return parent::current();
173  }
174 
180  public function ‪detach(object $object): void
181  {
182  $this->‪initialize();
183  parent::detach($object);
184  }
185 
191  public function ‪key(): string
192  {
193  $this->‪initialize();
194  return parent::key();
195  }
196 
200  public function ‪next(): void
201  {
202  $this->‪initialize();
203  parent::next();
204  }
205 
211  public function ‪offsetExists(mixed $value): bool
212  {
213  $this->‪initialize();
214  return parent::offsetExists($value);
215  }
216 
222  public function ‪offsetGet(mixed $value): mixed
223  {
224  $this->‪initialize();
225  return parent::offsetGet($value);
226  }
227 
234  public function ‪offsetSet(mixed $object, mixed $information): void
235  {
236  $this->‪initialize();
237  parent::offsetSet($object, $information);
238  }
239 
245  public function ‪offsetUnset(mixed $value): void
246  {
247  $this->‪initialize();
248  parent::offsetUnset($value);
249  }
250 
257  {
258  $this->‪initialize();
259  parent::removeAll(‪$storage);
260  }
261 
265  public function ‪rewind(): void
266  {
267  $this->‪initialize();
268  parent::rewind();
269  }
270 
274  public function ‪valid(): bool
275  {
276  $this->‪initialize();
277  return parent::valid();
278  }
279 
283  public function ‪toArray(): array
284  {
285  $this->‪initialize();
286  return parent::toArray();
287  }
288 
292  public function ‪getPosition($object): ?int
293  {
294  $this->‪initialize();
295  return parent::getPosition($object);
296  }
297 
298  public function ‪__serialize(): array
299  {
300  $properties = get_object_vars($this);
301  unset(
302  $properties['warning'],
303  $properties['dataMapper']
304  );
305  return $properties;
306  }
307 
308  public function ‪__unserialize(array $data): void
309  {
310  foreach ($data as ‪$propertyName => $propertyValue) {
311  $this->{$propertyName} = $propertyValue;
312  }
313 
314  $this->dataMapper = GeneralUtility::getContainer()->get(DataMapper::class);
315  }
316 }
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage\__serialize
‪__serialize()
Definition: LazyObjectStorage.php:298
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage\__unserialize
‪__unserialize(array $data)
Definition: LazyObjectStorage.php:308
‪TYPO3\CMS\Extbase\Persistence\Generic\LoadingStrategyInterface
Definition: LoadingStrategyInterface.php:21
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage\isInitialized
‪isInitialized()
Definition: LazyObjectStorage.php:66
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage\$dataMapper
‪DataMapper $dataMapper
Definition: LazyObjectStorage.php:47
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage\detach
‪detach(object $object)
Definition: LazyObjectStorage.php:180
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage\$fieldValue
‪mixed $fieldValue
Definition: LazyObjectStorage.php:62
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage\rewind
‪rewind()
Definition: LazyObjectStorage.php:265
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage\valid
‪valid()
Definition: LazyObjectStorage.php:274
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage\current
‪TEntity null current()
Definition: LazyObjectStorage.php:169
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage\$isInitialized
‪bool $isInitialized
Definition: LazyObjectStorage.php:64
‪TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper
Definition: DataMapper.php:58
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage\count
‪positive int count()
Definition: LazyObjectStorage.php:153
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage
Definition: ObjectStorage.php:34
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage\offsetExists
‪offsetExists(mixed $value)
Definition: LazyObjectStorage.php:211
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage\isStorageAlreadyMemorizedInParentCleanState
‪isStorageAlreadyMemorizedInParentCleanState()
Definition: LazyObjectStorage.php:108
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\$storage
‪array $storage
Definition: ObjectStorage.php:58
‪TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface
Definition: DomainObjectInterface.php:33
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage\removeAll
‪removeAll(ObjectStorage $storage)
Definition: LazyObjectStorage.php:256
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage\next
‪next()
Definition: LazyObjectStorage.php:200
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage\contains
‪contains(object $object)
Definition: LazyObjectStorage.php:141
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage\addAll
‪addAll(ObjectStorage $storage)
Definition: LazyObjectStorage.php:118
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage\getPosition
‪getPosition($object)
Definition: LazyObjectStorage.php:292
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage\offsetGet
‪offsetGet(mixed $value)
Definition: LazyObjectStorage.php:222
‪TYPO3\CMS\Extbase\Persistence\Generic
Definition: Backend.php:18
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\_memorizeCleanState
‪_memorizeCleanState(?string $propertyName=null)
Definition: ObjectStorage.php:318
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage\$warning
‪string $warning
Definition: LazyObjectStorage.php:45
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage\initialize
‪initialize()
Definition: LazyObjectStorage.php:91
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage\attach
‪attach(object $object, mixed $information=null)
Definition: LazyObjectStorage.php:130
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage\key
‪string key()
Definition: LazyObjectStorage.php:191
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage
Definition: LazyObjectStorage.php:36
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage\$propertyName
‪string $propertyName
Definition: LazyObjectStorage.php:57
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage\offsetUnset
‪offsetUnset(mixed $value)
Definition: LazyObjectStorage.php:245
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage\$parentObject
‪DomainObjectInterface $parentObject
Definition: LazyObjectStorage.php:52
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage\offsetSet
‪offsetSet(mixed $object, mixed $information)
Definition: LazyObjectStorage.php:234
‪TYPO3\CMS\Extbase\Persistence\Generic\Mapper\ColumnMap\Relation
‪Relation
Definition: Relation.php:24
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage\toArray
‪toArray()
Definition: LazyObjectStorage.php:283
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage\__construct
‪__construct(object $parentObject, string $propertyName, mixed $fieldValue, ?DataMapper $dataMapper=null)
Definition: LazyObjectStorage.php:76