‪TYPO3CMS  11.5
LazyObjectStorage.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
22 
32 {
41  private ‪$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.';
42 
43  protected ‪DataMapper ‪$dataMapper;
44 
50  protected ‪$parentObject;
51 
57  protected ‪$propertyName;
58 
64  protected ‪$fieldValue;
65 
69  protected ‪$isInitialized = false;
70 
76  public function ‪isInitialized()
77  {
79  }
80 
90  {
91  $this->parentObject = ‪$parentObject;
92  $this->propertyName = ‪$propertyName;
93  $this->fieldValue = ‪$fieldValue;
94  reset($this->storage);
95  if (‪$dataMapper === null) {
96  ‪$dataMapper = GeneralUtility::makeInstance(DataMapper::class);
97  }
98  $this->dataMapper = ‪$dataMapper;
99  }
100 
104  protected function ‪initialize()
105  {
106  if ($this->‪isInitialized) {
107  return;
108  }
109 
110  $this->‪isInitialized = true;
111  $objects = $this->dataMapper->fetchRelated($this->parentObject, $this->propertyName, $this->fieldValue, false);
112  foreach ($objects as $object) {
113  parent::attach($object);
114  }
115  $this->‪_memorizeCleanState();
117  $this->parentObject->_memorizeCleanState($this->propertyName);
118  }
119  }
120 
124  protected function ‪isStorageAlreadyMemorizedInParentCleanState(): bool
125  {
126  return $this->parentObject->_getCleanProperty($this->propertyName) === $this;
127  }
128 
129  // Delegation to the ObjectStorage methods below
135  public function ‪addAll(‪ObjectStorage ‪$storage): void
136  {
137  $this->‪initialize();
138  parent::addAll(‪$storage);
139  }
140 
147  public function ‪attach($object, $data = null): void
148  {
149  $this->‪initialize();
150  parent::attach($object, $data);
151  }
152 
159  public function ‪contains($object): bool
160  {
161  $this->‪initialize();
162  return parent::contains($object);
163  }
164 
171  public function ‪count(): int
172  {
173  $columnMap = $this->dataMapper->getDataMap(get_class($this->parentObject))->getColumnMap($this->propertyName);
174  if (!$this->‪isInitialized && $columnMap->getTypeOfRelation() === ‪ColumnMap::RELATION_HAS_MANY) {
175  $numberOfElements = $this->dataMapper->countRelated($this->parentObject, $this->propertyName, $this->fieldValue);
176  } else {
177  $this->‪initialize();
178  $numberOfElements = ‪count($this->storage);
179  }
180  return $numberOfElements;
181  }
182 
188  #[\ReturnTypeWillChange]
189  public function ‪current()
190  {
191  $this->‪initialize();
192  return parent::current();
193  }
194 
200  public function ‪detach($object): void
201  {
202  $this->‪initialize();
203  parent::detach($object);
204  }
205 
211  public function ‪key(): string
212  {
213  $this->‪initialize();
214  return parent::key();
215  }
216 
220  public function ‪next(): void
221  {
222  $this->‪initialize();
223  parent::next();
224  }
225 
232  public function ‪offsetExists($value): bool
233  {
234  $this->‪initialize();
235  return parent::offsetExists($value);
236  }
237 
244  #[\ReturnTypeWillChange]
245  public function ‪offsetGet($value)
246  {
247  $this->‪initialize();
248  return parent::offsetGet($value);
249  }
250 
257  public function ‪offsetSet($object, $info): void
258  {
259  $this->‪initialize();
260  parent::offsetSet($object, $info);
261  }
262 
268  public function ‪offsetUnset($value): void
269  {
270  $this->‪initialize();
271  parent::offsetUnset($value);
272  }
273 
279  public function ‪removeAll(ObjectStorage ‪$storage): void
280  {
281  $this->‪initialize();
282  parent::removeAll(‪$storage);
283  }
284 
288  public function ‪rewind(): void
289  {
290  $this->‪initialize();
291  parent::rewind();
292  }
293 
299  public function ‪valid(): bool
300  {
301  $this->‪initialize();
302  return parent::valid();
303  }
304 
310  public function ‪toArray(): array
311  {
312  $this->‪initialize();
313  return parent::toArray();
314  }
315 
320  public function ‪getPosition($object): ?int
321  {
322  $this->‪initialize();
323  return parent::getPosition($object);
324  }
325 
326  public function ‪__serialize(): array
327  {
328  $properties = get_object_vars($this);
329  unset(
330  $properties['warning'],
331  $properties['dataMapper']
332  );
333  return $properties;
334  }
335 
336  public function ‪__unserialize(array $data): void
337  {
338  foreach ($data as ‪$propertyName => $propertyValue) {
339  $this->{‪$propertyName} = $propertyValue;
340  }
341 
342  $this->dataMapper = GeneralUtility::getContainer()->get(DataMapper::class);
343  }
344 }
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage\__serialize
‪__serialize()
Definition: LazyObjectStorage.php:321
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage\offsetUnset
‪offsetUnset($value)
Definition: LazyObjectStorage.php:263
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage\__unserialize
‪__unserialize(array $data)
Definition: LazyObjectStorage.php:331
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage\offsetGet
‪mixed offsetGet($value)
Definition: LazyObjectStorage.php:240
‪TYPO3\CMS\Extbase\Persistence\Generic\LoadingStrategyInterface
Definition: LoadingStrategyInterface.php:21
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage\$dataMapper
‪DataMapper $dataMapper
Definition: LazyObjectStorage.php:42
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage\$fieldValue
‪mixed $fieldValue
Definition: LazyObjectStorage.php:60
‪TYPO3\CMS\Extbase\Persistence\Generic\Mapper\ColumnMap
Definition: ColumnMap.php:28
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage\rewind
‪rewind()
Definition: LazyObjectStorage.php:283
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage\current
‪TEntity null current()
Definition: LazyObjectStorage.php:184
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage\$isInitialized
‪bool $isInitialized
Definition: LazyObjectStorage.php:64
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage\$parentObject
‪TYPO3 CMS Extbase DomainObject DomainObjectInterface $parentObject
Definition: LazyObjectStorage.php:48
‪TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper
Definition: DataMapper.php:51
‪TYPO3\CMS\Extbase\Persistence\Generic\Mapper\ColumnMap\RELATION_HAS_MANY
‪const RELATION_HAS_MANY
Definition: ColumnMap.php:42
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage
Definition: ObjectStorage.php:32
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage\count
‪int count()
Definition: LazyObjectStorage.php:166
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage\toArray
‪array toArray()
Definition: LazyObjectStorage.php:305
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage\getPosition
‪int null getPosition($object)
Definition: LazyObjectStorage.php:315
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\$storage
‪array $storage
Definition: ObjectStorage.php:55
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage\removeAll
‪removeAll(ObjectStorage $storage)
Definition: LazyObjectStorage.php:274
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage\attach
‪attach($object, $data=null)
Definition: LazyObjectStorage.php:142
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\_memorizeCleanState
‪_memorizeCleanState()
Definition: ObjectStorage.php:355
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage\offsetSet
‪offsetSet($object, $info)
Definition: LazyObjectStorage.php:252
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage\next
‪next()
Definition: LazyObjectStorage.php:215
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage\addAll
‪addAll(ObjectStorage $storage)
Definition: LazyObjectStorage.php:130
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage\offsetExists
‪bool offsetExists($value)
Definition: LazyObjectStorage.php:227
‪TYPO3\CMS\Extbase\Persistence\Generic
Definition: Backend.php:16
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage\isInitialized
‪bool isInitialized()
Definition: LazyObjectStorage.php:71
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage\$warning
‪string $warning
Definition: LazyObjectStorage.php:40
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage\initialize
‪initialize()
Definition: LazyObjectStorage.php:99
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage\valid
‪bool valid()
Definition: LazyObjectStorage.php:294
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage\isStorageAlreadyMemorizedInParentCleanState
‪bool isStorageAlreadyMemorizedInParentCleanState()
Definition: LazyObjectStorage.php:119
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage\__construct
‪__construct($parentObject, $propertyName, $fieldValue, ?DataMapper $dataMapper=null)
Definition: LazyObjectStorage.php:84
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage\contains
‪bool contains($object)
Definition: LazyObjectStorage.php:154
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage\key
‪string key()
Definition: LazyObjectStorage.php:206
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage
Definition: LazyObjectStorage.php:32
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage\$propertyName
‪string $propertyName
Definition: LazyObjectStorage.php:54
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage\detach
‪detach($object)
Definition: LazyObjectStorage.php:195