‪TYPO3CMS  10.4
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 
27 class ‪ObjectStorage implements \Countable, \Iterator, \ArrayAccess, ‪ObjectMonitoringInterface
28 {
37  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.';
38 
53  protected ‪$storage = [];
54 
60  protected ‪$isModified = false;
61 
68  protected ‪$addedObjectsPositions = [];
69 
76  protected ‪$removedObjectsPositions = [];
77 
84  protected ‪$positionCounter = 0;
85 
89  public function ‪rewind()
90  {
91  reset($this->storage);
92  }
93 
99  public function ‪valid()
100  {
101  return ‪current($this->storage) !== false;
102  }
103 
111  public function ‪key()
112  {
113  return ‪key($this->storage);
114  }
115 
121  public function ‪current()
122  {
123  $item = ‪current($this->storage);
124  return $item['obj'];
125  }
126 
130  public function ‪next()
131  {
132  ‪next($this->storage);
133  }
134 
140  public function ‪count()
141  {
142  return ‪count($this->storage);
143  }
144 
151  public function ‪offsetSet($object, $information)
152  {
153  $this->isModified = true;
154  $this->storage[spl_object_hash($object)] = ['obj' => $object, 'inf' => $information];
155 
156  $this->positionCounter++;
157  $this->addedObjectsPositions[spl_object_hash($object)] = ‪$this->positionCounter;
158  }
159 
166  public function ‪offsetExists($value)
167  {
168  return is_object($value) && isset($this->storage[spl_object_hash($value)])
169  || ‪MathUtility::canBeInterpretedAsInteger($value) && isset(array_values($this->storage)[$value]);
170  }
171 
177  public function ‪offsetUnset($value)
178  {
179  $this->isModified = true;
180 
181  $object = $value;
182 
184  $object = $this->‪offsetGet($value);
185  }
186 
187  unset($this->storage[spl_object_hash($object)]);
188 
189  if (empty($this->storage)) {
190  $this->positionCounter = 0;
191  }
192 
193  $this->removedObjectsPositions[spl_object_hash($object)] = $this->addedObjectsPositions[spl_object_hash($object)] ?? null;
194  unset($this->addedObjectsPositions[spl_object_hash($object)]);
195  }
196 
204  public function ‪offsetGet($value)
205  {
207  return array_values($this->storage)[$value]['obj'];
208  }
209 
211  return $this->storage[spl_object_hash($value)]['inf'];
212  }
213 
220  public function ‪contains($object)
221  {
222  return $this->‪offsetExists($object);
223  }
224 
231  public function ‪attach($object, $information = null)
232  {
233  $this->‪offsetSet($object, $information);
234  }
235 
241  public function ‪detach($object)
242  {
243  $this->‪offsetUnset($object);
244  }
245 
251  public function ‪getInfo()
252  {
253  $item = ‪current($this->storage);
254  return $item['inf'];
255  }
256 
262  public function ‪setInfo($data)
263  {
264  $this->isModified = true;
265  $key = ‪key($this->storage);
266  $this->storage[$key]['inf'] = $data;
267  }
268 
274  public function ‪addAll(‪ObjectStorage $objectStorage)
275  {
276  foreach ($objectStorage as $object) {
277  $this->‪attach($object, $objectStorage->‪getInfo());
278  }
279  }
280 
286  public function ‪removeAll(‪ObjectStorage $objectStorage)
287  {
288  foreach ($objectStorage as $object) {
289  $this->‪detach($object);
290  }
291  }
292 
298  public function ‪toArray()
299  {
300  $array = [];
301  ‪$storage = array_values($this->storage);
302  foreach (‪$storage as $item) {
303  $array[] = $item['obj'];
304  }
305  return $array;
306  }
307 
315  public function ‪getArray()
316  {
317  return $this->‪toArray();
318  }
319 
325  public function ‪serialize()
326  {
327  throw new \RuntimeException('An ObjectStorage instance cannot be serialized.', 1267700868);
328  }
329 
335  public function ‪unserialize()
336  {
337  throw new \RuntimeException('A ObjectStorage instance cannot be unserialized.', 1267700870);
338  }
339 
343  public function ‪_memorizeCleanState()
344  {
345  $this->isModified = false;
346  }
347 
353  public function ‪_isDirty()
354  {
355  return ‪$this->isModified;
356  }
357 
364  public function ‪isRelationDirty($object)
365  {
366  return isset($this->addedObjectsPositions[spl_object_hash($object)])
367  && isset($this->removedObjectsPositions[spl_object_hash($object)])
368  && ($this->addedObjectsPositions[spl_object_hash($object)] !== $this->removedObjectsPositions[spl_object_hash($object)]);
369  }
370 
375  public function ‪getPosition($object)
376  {
377  if (!isset($this->addedObjectsPositions[spl_object_hash($object)])) {
378  return null;
379  }
380 
381  return $this->addedObjectsPositions[spl_object_hash($object)];
382  }
383 }
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\setInfo
‪setInfo($data)
Definition: ObjectStorage.php:256
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\$removedObjectsPositions
‪array $removedObjectsPositions
Definition: ObjectStorage.php:71
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\toArray
‪array toArray()
Definition: ObjectStorage.php:292
‪TYPO3\CMS\Core\Utility\MathUtility\canBeInterpretedAsInteger
‪static bool canBeInterpretedAsInteger($var)
Definition: MathUtility.php:74
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\unserialize
‪unserialize()
Definition: ObjectStorage.php:329
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\getInfo
‪mixed getInfo()
Definition: ObjectStorage.php:245
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\contains
‪bool contains($object)
Definition: ObjectStorage.php:214
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\getPosition
‪int null getPosition($object)
Definition: ObjectStorage.php:369
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\_isDirty
‪bool _isDirty()
Definition: ObjectStorage.php:347
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\valid
‪bool valid()
Definition: ObjectStorage.php:93
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\offsetGet
‪mixed offsetGet($value)
Definition: ObjectStorage.php:198
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\key
‪string key()
Definition: ObjectStorage.php:105
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\$positionCounter
‪int $positionCounter
Definition: ObjectStorage.php:78
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage
Definition: ObjectStorage.php:28
‪TYPO3\CMS\Extbase\Persistence
Definition: ClassesConfiguration.php:18
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\offsetSet
‪offsetSet($object, $information)
Definition: ObjectStorage.php:145
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\getArray
‪array getArray()
Definition: ObjectStorage.php:309
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\detach
‪detach($object)
Definition: ObjectStorage.php:235
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\$storage
‪array $storage
Definition: ObjectStorage.php:51
‪TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface
Definition: DomainObjectInterface.php:29
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\_memorizeCleanState
‪_memorizeCleanState()
Definition: ObjectStorage.php:337
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\$addedObjectsPositions
‪array $addedObjectsPositions
Definition: ObjectStorage.php:64
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\attach
‪attach($object, $information=null)
Definition: ObjectStorage.php:225
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\rewind
‪rewind()
Definition: ObjectStorage.php:83
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\$isModified
‪bool $isModified
Definition: ObjectStorage.php:57
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\isRelationDirty
‪bool isRelationDirty($object)
Definition: ObjectStorage.php:358
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\offsetExists
‪bool offsetExists($value)
Definition: ObjectStorage.php:160
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\count
‪int count()
Definition: ObjectStorage.php:134
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\next
‪next()
Definition: ObjectStorage.php:124
‪TYPO3\CMS\Core\Utility\MathUtility
Definition: MathUtility.php:22
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\addAll
‪addAll(ObjectStorage $objectStorage)
Definition: ObjectStorage.php:268
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\$warning
‪string $warning
Definition: ObjectStorage.php:36
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\serialize
‪serialize()
Definition: ObjectStorage.php:319
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\removeAll
‪removeAll(ObjectStorage $objectStorage)
Definition: ObjectStorage.php:280
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\current
‪DomainObjectInterface current()
Definition: ObjectStorage.php:115
‪TYPO3\CMS\Extbase\Persistence\ObjectMonitoringInterface
Definition: ObjectMonitoringInterface.php:25
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\offsetUnset
‪offsetUnset($value)
Definition: ObjectStorage.php:171