‪TYPO3CMS  11.5
ObjectStorage.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 
20 
31 class ‪ObjectStorage implements \Countable, \Iterator, \ArrayAccess, ‪ObjectMonitoringInterface
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\\ObjectStorage. To retrieve the first result, you can use the rewind() and current() methods.';
42 
57  protected ‪$storage = [];
58 
64  protected ‪$isModified = false;
65 
72  protected ‪$addedObjectsPositions = [];
73 
80  protected ‪$removedObjectsPositions = [];
81 
88  protected ‪$positionCounter = 0;
89 
93  #[\ReturnTypeWillChange]
94  public function ‪rewind()
95  {
96  reset($this->storage);
97  }
98 
104  #[\ReturnTypeWillChange]
105  public function ‪valid()
106  {
107  return ‪current($this->storage) !== false;
108  }
109 
117  #[\ReturnTypeWillChange]
118  public function ‪key()
119  {
120  return ‪key($this->storage);
121  }
122 
128  #[\ReturnTypeWillChange]
129  public function ‪current()
130  {
131  $item = ‪current($this->storage);
132 
133  return $item['obj'] ?? null;
134  }
135 
139  #[\ReturnTypeWillChange]
140  public function ‪next()
141  {
142  ‪next($this->storage);
143  }
144 
150  #[\ReturnTypeWillChange]
151  public function ‪count()
152  {
153  return ‪count($this->storage);
154  }
155 
162  #[\ReturnTypeWillChange]
163  public function ‪offsetSet($object, $information)
164  {
165  $this->isModified = true;
166  $this->storage[spl_object_hash($object)] = ['obj' => $object, 'inf' => $information];
167 
168  $this->positionCounter++;
169  $this->addedObjectsPositions[spl_object_hash($object)] = ‪$this->positionCounter;
170  }
171 
178  #[\ReturnTypeWillChange]
179  public function ‪offsetExists($value)
180  {
181  return (is_object($value) && isset($this->storage[spl_object_hash($value)]))
182  || (‪MathUtility::canBeInterpretedAsInteger($value) && isset(array_values($this->storage)[$value]));
183  }
184 
190  #[\ReturnTypeWillChange]
191  public function ‪offsetUnset($value)
192  {
193  $this->isModified = true;
194 
195  $object = $value;
196 
198  $object = $this->‪offsetGet($value);
199  }
200 
201  unset($this->storage[spl_object_hash($object)]);
202 
203  if (empty($this->storage)) {
204  $this->positionCounter = 0;
205  }
206 
207  $this->removedObjectsPositions[spl_object_hash($object)] = $this->addedObjectsPositions[spl_object_hash($object)] ?? null;
208  unset($this->addedObjectsPositions[spl_object_hash($object)]);
209  }
210 
218  #[\ReturnTypeWillChange]
219  public function ‪offsetGet($value)
220  {
222  return array_values($this->storage)[$value]['obj'] ?? null;
223  }
224 
226  return $this->storage[spl_object_hash($value)]['inf'] ?? null;
227  }
228 
235  public function ‪contains($object)
236  {
237  return $this->‪offsetExists($object);
238  }
239 
246  public function ‪attach($object, $information = null)
247  {
248  $this->‪offsetSet($object, $information);
249  }
250 
256  public function ‪detach($object)
257  {
258  $this->‪offsetUnset($object);
259  }
260 
266  public function ‪getInfo()
267  {
268  $item = ‪current($this->storage);
269 
270  return $item['inf'] ?? null;
271  }
272 
278  public function ‪setInfo($data)
279  {
280  $this->isModified = true;
281  $key = ‪key($this->storage);
282  $this->storage[$key]['inf'] = $data;
283  }
284 
290  public function ‪addAll(‪ObjectStorage $objectStorage)
291  {
292  foreach ($objectStorage as $object) {
293  $this->‪attach($object, $objectStorage->‪getInfo());
294  }
295  }
296 
302  public function ‪removeAll(‪ObjectStorage $objectStorage)
303  {
304  foreach ($objectStorage as $object) {
305  $this->‪detach($object);
306  }
307  }
308 
314  public function ‪toArray()
315  {
316  $array = [];
317  ‪$storage = array_values($this->storage);
318  foreach (‪$storage as $item) {
319  $array[] = $item['obj'];
320  }
321  return $array;
322  }
323 
331  public function ‪getArray()
332  {
333  return $this->‪toArray();
334  }
335 
342  public function ‪serialize()
343  {
344  throw new \RuntimeException('An ObjectStorage instance cannot be serialized.', 1267700868);
345  }
346 
353  public function ‪unserialize()
354  {
355  throw new \RuntimeException('A ObjectStorage instance cannot be unserialized.', 1267700870);
356  }
357 
361  public function ‪_memorizeCleanState()
362  {
363  $this->isModified = false;
364  }
365 
371  public function ‪_isDirty()
372  {
373  return ‪$this->isModified;
374  }
375 
382  public function ‪isRelationDirty($object)
383  {
384  return isset($this->addedObjectsPositions[spl_object_hash($object)])
385  && isset($this->removedObjectsPositions[spl_object_hash($object)])
386  && ($this->addedObjectsPositions[spl_object_hash($object)] !== $this->removedObjectsPositions[spl_object_hash($object)]);
387  }
388 
393  public function ‪getPosition($object)
394  {
395  if (!isset($this->addedObjectsPositions[spl_object_hash($object)])) {
396  return null;
397  }
398 
399  return $this->addedObjectsPositions[spl_object_hash($object)];
400  }
401 }
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\setInfo
‪setInfo($data)
Definition: ObjectStorage.php:272
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\getArray
‪list< TEntity > getArray()
Definition: ObjectStorage.php:325
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\toArray
‪list< TEntity > toArray()
Definition: ObjectStorage.php:308
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\$removedObjectsPositions
‪array $removedObjectsPositions
Definition: ObjectStorage.php:75
‪TYPO3\CMS\Core\Utility\MathUtility\canBeInterpretedAsInteger
‪static bool canBeInterpretedAsInteger($var)
Definition: MathUtility.php:74
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\unserialize
‪unserialize()
Definition: ObjectStorage.php:347
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\getInfo
‪mixed getInfo()
Definition: ObjectStorage.php:260
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\contains
‪bool contains($object)
Definition: ObjectStorage.php:229
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\current
‪TEntity null current()
Definition: ObjectStorage.php:123
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\getPosition
‪int null getPosition($object)
Definition: ObjectStorage.php:387
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\_isDirty
‪bool _isDirty()
Definition: ObjectStorage.php:365
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\valid
‪bool valid()
Definition: ObjectStorage.php:99
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\offsetGet
‪mixed offsetGet($value)
Definition: ObjectStorage.php:213
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\key
‪string key()
Definition: ObjectStorage.php:112
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\$positionCounter
‪int $positionCounter
Definition: ObjectStorage.php:82
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage
Definition: ObjectStorage.php:32
‪TYPO3\CMS\Extbase\Persistence
Definition: ClassesConfiguration.php:18
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\offsetSet
‪offsetSet($object, $information)
Definition: ObjectStorage.php:157
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\detach
‪detach($object)
Definition: ObjectStorage.php:250
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\$storage
‪array $storage
Definition: ObjectStorage.php:55
‪TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface
Definition: DomainObjectInterface.php:29
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\_memorizeCleanState
‪_memorizeCleanState()
Definition: ObjectStorage.php:355
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\$addedObjectsPositions
‪array $addedObjectsPositions
Definition: ObjectStorage.php:68
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\attach
‪attach($object, $information=null)
Definition: ObjectStorage.php:240
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\rewind
‪rewind()
Definition: ObjectStorage.php:88
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\$isModified
‪bool $isModified
Definition: ObjectStorage.php:61
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\isRelationDirty
‪bool isRelationDirty($object)
Definition: ObjectStorage.php:376
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\offsetExists
‪bool offsetExists($value)
Definition: ObjectStorage.php:173
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\count
‪int count()
Definition: ObjectStorage.php:145
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\next
‪next()
Definition: ObjectStorage.php:134
‪TYPO3\CMS\Core\Utility\MathUtility
Definition: MathUtility.php:22
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\addAll
‪addAll(ObjectStorage $objectStorage)
Definition: ObjectStorage.php:284
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\$warning
‪string $warning
Definition: ObjectStorage.php:40
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\serialize
‪serialize()
Definition: ObjectStorage.php:336
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\removeAll
‪removeAll(ObjectStorage $objectStorage)
Definition: ObjectStorage.php:296
‪TYPO3\CMS\Extbase\Persistence\ObjectMonitoringInterface
Definition: ObjectMonitoringInterface.php:25
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\offsetUnset
‪offsetUnset($value)
Definition: ObjectStorage.php:185