‪TYPO3CMS  ‪main
ObjectStorage.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 
22 
33 class ‪ObjectStorage implements \Countable, \Iterator, \ArrayAccess, ‪ObjectMonitoringInterface
34 {
43  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\\ObjectStorage. To retrieve the first result, you can use the rewind() and current() methods.';
44 
58  protected array ‪$storage = [];
59 
63  protected bool ‪$isModified = false;
64 
70  protected array ‪$addedObjectsPositions = [];
71 
76  protected array ‪$removedObjectsPositions = [];
77 
83  protected int ‪$positionCounter = 0;
84 
88  public function ‪rewind(): void
89  {
90  reset($this->storage);
91  }
92 
96  public function ‪valid(): bool
97  {
98  return ‪current($this->storage) !== false;
99  }
100 
108  public function ‪key(): string
109  {
110  return ‪key($this->storage);
111  }
112 
118  public function ‪current(): ?object
119  {
120  $item = ‪current($this->storage);
121  return $item['obj'] ?? null;
122  }
123 
127  public function ‪next(): void
128  {
129  ‪next($this->storage);
130  }
131 
137  public function ‪count(): int
138  {
139  return ‪count($this->storage);
140  }
141 
148  public function ‪offsetSet(mixed $object, mixed $information): void
149  {
150  $this->isModified = true;
151  $this->storage[spl_object_hash($object)] = ['obj' => $object, 'inf' => $information];
152 
153  $this->positionCounter++;
154  $this->addedObjectsPositions[spl_object_hash($object)] = ‪$this->positionCounter;
155  }
156 
162  public function ‪offsetExists(mixed $value): bool
163  {
164  return (is_object($value) && isset($this->storage[spl_object_hash($value)]))
165  || (‪MathUtility::canBeInterpretedAsInteger($value) && isset(array_values($this->storage)[$value]));
166  }
167 
173  public function ‪offsetUnset(mixed $value): void
174  {
175  $this->isModified = true;
176 
177  $object = $value;
178 
180  $object = $this->‪offsetGet($value);
181  }
182 
183  unset($this->storage[spl_object_hash($object)]);
184 
185  if (empty($this->storage)) {
186  $this->positionCounter = 0;
187  }
188 
189  $this->removedObjectsPositions[spl_object_hash($object)] = $this->addedObjectsPositions[spl_object_hash($object)] ?? null;
190  unset($this->addedObjectsPositions[spl_object_hash($object)]);
191  }
192 
199  public function ‪offsetGet(mixed $value): mixed
200  {
202  return array_values($this->storage)[$value]['obj'] ?? null;
203  }
204 
206  return $this->storage[spl_object_hash($value)]['inf'] ?? null;
207  }
208 
214  public function ‪contains(object $object): bool
215  {
216  return $this->‪offsetExists($object);
217  }
218 
225  public function ‪attach(object $object, mixed $information = null): void
226  {
227  $this->‪offsetSet($object, $information);
228  }
229 
235  public function ‪detach(object $object): void
236  {
237  $this->‪offsetUnset($object);
238  }
239 
245  public function ‪getInfo(): mixed
246  {
247  $item = ‪current($this->storage);
248 
249  return $item['inf'] ?? null;
250  }
251 
255  public function ‪setInfo(mixed $information): void
256  {
257  $this->isModified = true;
258  $key = ‪key($this->storage);
259  $this->storage[$key]['inf'] = $information;
260  }
261 
267  public function ‪addAll(‪ObjectStorage ‪$storage): void
268  {
269  foreach (‪$storage as $object) {
270  $this->‪attach($object, ‪$storage->getInfo());
271  }
272  }
273 
280  {
281  foreach (‪$storage as $object) {
282  $this->‪detach($object);
283  }
284  }
285 
291  public function ‪toArray(): array
292  {
293  $array = [];
294  ‪$storage = array_values($this->storage);
295  foreach (‪$storage as $item) {
296  $array[] = $item['obj'];
297  }
298  return $array;
299  }
300 
308  public function ‪getArray(): array
309  {
310  return $this->‪toArray();
311  }
312 
318  public function ‪_memorizeCleanState(?string $propertyName = null): void
319  {
320  $this->isModified = false;
321  }
322 
328  public function ‪_isDirty(?string $propertyName = null): bool
329  {
330  return ‪$this->isModified;
331  }
332 
336  public function ‪isRelationDirty(object $object): bool
337  {
338  return isset($this->addedObjectsPositions[spl_object_hash($object)])
339  && isset($this->removedObjectsPositions[spl_object_hash($object)])
340  && ($this->addedObjectsPositions[spl_object_hash($object)] !== $this->removedObjectsPositions[spl_object_hash($object)]);
341  }
342 
343  public function ‪getPosition(object $object): ?int
344  {
345  if (!isset($this->addedObjectsPositions[spl_object_hash($object)])) {
346  return null;
347  }
348 
349  return $this->addedObjectsPositions[spl_object_hash($object)];
350  }
351 }
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\getArray
‪list< TEntity > getArray()
Definition: ObjectStorage.php:308
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\toArray
‪list< TEntity > toArray()
Definition: ObjectStorage.php:291
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\$removedObjectsPositions
‪array $removedObjectsPositions
Definition: ObjectStorage.php:76
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\attach
‪attach(object $object, mixed $information=null)
Definition: ObjectStorage.php:225
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\getInfo
‪mixed getInfo()
Definition: ObjectStorage.php:245
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\current
‪TEntity null current()
Definition: ObjectStorage.php:118
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\detach
‪detach(object $object)
Definition: ObjectStorage.php:235
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\getPosition
‪getPosition(object $object)
Definition: ObjectStorage.php:343
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\isRelationDirty
‪isRelationDirty(object $object)
Definition: ObjectStorage.php:336
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\offsetUnset
‪offsetUnset(mixed $value)
Definition: ObjectStorage.php:173
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\key
‪string key()
Definition: ObjectStorage.php:108
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\$positionCounter
‪int $positionCounter
Definition: ObjectStorage.php:83
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage
Definition: ObjectStorage.php:34
‪TYPO3\CMS\Extbase\Persistence
Definition: ClassesConfiguration.php:18
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\valid
‪valid()
Definition: ObjectStorage.php:96
‪TYPO3\CMS\Core\Utility\MathUtility\canBeInterpretedAsInteger
‪static bool canBeInterpretedAsInteger(mixed $var)
Definition: MathUtility.php:74
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\offsetExists
‪offsetExists(mixed $value)
Definition: ObjectStorage.php:162
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\offsetSet
‪offsetSet(mixed $object, mixed $information)
Definition: ObjectStorage.php:148
‪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\ObjectStorage\offsetGet
‪mixed offsetGet(mixed $value)
Definition: ObjectStorage.php:199
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\$addedObjectsPositions
‪array $addedObjectsPositions
Definition: ObjectStorage.php:70
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\contains
‪contains(object $object)
Definition: ObjectStorage.php:214
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\rewind
‪rewind()
Definition: ObjectStorage.php:88
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\setInfo
‪setInfo(mixed $information)
Definition: ObjectStorage.php:255
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\$isModified
‪bool $isModified
Definition: ObjectStorage.php:63
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\_memorizeCleanState
‪_memorizeCleanState(?string $propertyName=null)
Definition: ObjectStorage.php:318
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\count
‪positive int count()
Definition: ObjectStorage.php:137
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\next
‪next()
Definition: ObjectStorage.php:127
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\removeAll
‪removeAll(ObjectStorage $storage)
Definition: ObjectStorage.php:279
‪TYPO3\CMS\Core\Utility\MathUtility
Definition: MathUtility.php:24
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\_isDirty
‪_isDirty(?string $propertyName=null)
Definition: ObjectStorage.php:328
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\$warning
‪string $warning
Definition: ObjectStorage.php:43
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\addAll
‪addAll(ObjectStorage $storage)
Definition: ObjectStorage.php:267
‪TYPO3\CMS\Extbase\Persistence\ObjectMonitoringInterface
Definition: ObjectMonitoringInterface.php:27