‪TYPO3CMS  9.5
ObjectStorage.php
Go to the documentation of this file.
1 <?php
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
18 
25 class ‪ObjectStorage implements \Countable, \Iterator, \ArrayAccess, ‪ObjectMonitoringInterface
26 {
35  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.';
36 
51  protected ‪$storage = [];
52 
58  protected ‪$isModified = false;
59 
66  protected ‪$addedObjectsPositions = [];
67 
74  protected ‪$removedObjectsPositions = [];
75 
82  protected ‪$positionCounter = 0;
83 
87  public function ‪rewind()
88  {
89  reset($this->storage);
90  }
91 
97  public function ‪valid()
98  {
99  return ‪current($this->storage) !== false;
100  }
101 
109  public function ‪key()
110  {
111  return ‪key($this->storage);
112  }
113 
119  public function ‪current()
120  {
121  $item = ‪current($this->storage);
122  return $item['obj'];
123  }
124 
128  public function ‪next()
129  {
130  ‪next($this->storage);
131  }
132 
138  public function ‪count()
139  {
140  return ‪count($this->storage);
141  }
142 
149  public function ‪offsetSet($object, $information)
150  {
151  $this->isModified = true;
152  $this->storage[spl_object_hash($object)] = ['obj' => $object, 'inf' => $information];
153 
154  $this->positionCounter++;
155  $this->addedObjectsPositions[spl_object_hash($object)] = ‪$this->positionCounter;
156  }
157 
164  public function ‪offsetExists($value)
165  {
166  return is_object($value) && isset($this->storage[spl_object_hash($value)])
167  || ‪MathUtility::canBeInterpretedAsInteger($value) && isset(array_values($this->storage)[$value]);
168  }
169 
175  public function ‪offsetUnset($value)
176  {
177  $this->isModified = true;
178 
179  $object = $value;
180 
182  $object = $this->‪offsetGet($value);
183  }
184 
185  unset($this->storage[spl_object_hash($object)]);
186 
187  if (empty($this->storage)) {
188  $this->positionCounter = 0;
189  }
190 
191  $this->removedObjectsPositions[spl_object_hash($object)] = $this->addedObjectsPositions[spl_object_hash($object)] ?? null;
192  unset($this->addedObjectsPositions[spl_object_hash($object)]);
193  }
194 
202  public function ‪offsetGet($value)
203  {
205  return array_values($this->storage)[$value]['obj'];
206  }
207 
208  return $this->storage[spl_object_hash($value)]['inf'];
209  }
210 
217  public function ‪contains($object)
218  {
219  return $this->‪offsetExists($object);
220  }
221 
228  public function ‪attach($object, $information = null)
229  {
230  $this->‪offsetSet($object, $information);
231  }
232 
238  public function ‪detach($object)
239  {
240  $this->‪offsetUnset($object);
241  }
242 
248  public function ‪getInfo()
249  {
250  $item = ‪current($this->storage);
251  return $item['inf'];
252  }
253 
259  public function ‪setInfo($data)
260  {
261  $this->isModified = true;
262  $key = ‪key($this->storage);
263  $this->storage[$key]['inf'] = $data;
264  }
265 
271  public function ‪addAll(‪ObjectStorage $objectStorage)
272  {
273  foreach ($objectStorage as $object) {
274  $this->‪attach($object, $objectStorage->‪getInfo());
275  }
276  }
277 
283  public function ‪removeAll(‪ObjectStorage $objectStorage)
284  {
285  foreach ($objectStorage as $object) {
286  $this->‪detach($object);
287  }
288  }
289 
295  public function ‪toArray()
296  {
297  $array = [];
298  ‪$storage = array_values($this->storage);
299  foreach (‪$storage as $item) {
300  $array[] = $item['obj'];
301  }
302  return $array;
303  }
304 
312  public function ‪getArray()
313  {
314  return $this->‪toArray();
315  }
316 
322  public function ‪serialize()
323  {
324  throw new \RuntimeException('An ObjectStorage instance cannot be serialized.', 1267700868);
325  }
326 
332  public function ‪unserialize()
333  {
334  throw new \RuntimeException('A ObjectStorage instance cannot be unserialized.', 1267700870);
335  }
336 
340  public function ‪_memorizeCleanState()
341  {
342  $this->isModified = false;
343  }
344 
350  public function ‪_isDirty()
351  {
352  return ‪$this->isModified;
353  }
354 
361  public function ‪isRelationDirty($object)
362  {
363  return isset($this->addedObjectsPositions[spl_object_hash($object)])
364  && isset($this->removedObjectsPositions[spl_object_hash($object)])
365  && ($this->addedObjectsPositions[spl_object_hash($object)] !== $this->removedObjectsPositions[spl_object_hash($object)]);
366  }
367 
372  public function ‪getPosition($object)
373  {
374  if (!isset($this->addedObjectsPositions[spl_object_hash($object)])) {
375  return null;
376  }
377 
378  return $this->addedObjectsPositions[spl_object_hash($object)];
379  }
380 }
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\setInfo
‪setInfo($data)
Definition: ObjectStorage.php:253
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\$removedObjectsPositions
‪array $removedObjectsPositions
Definition: ObjectStorage.php:69
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\toArray
‪array toArray()
Definition: ObjectStorage.php:289
‪TYPO3\CMS\Core\Utility\MathUtility\canBeInterpretedAsInteger
‪static bool canBeInterpretedAsInteger($var)
Definition: MathUtility.php:73
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\unserialize
‪unserialize()
Definition: ObjectStorage.php:326
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\getInfo
‪mixed getInfo()
Definition: ObjectStorage.php:242
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\contains
‪bool contains($object)
Definition: ObjectStorage.php:211
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\getPosition
‪int null getPosition($object)
Definition: ObjectStorage.php:366
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\_isDirty
‪bool _isDirty()
Definition: ObjectStorage.php:344
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\valid
‪bool valid()
Definition: ObjectStorage.php:91
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\offsetGet
‪mixed offsetGet($value)
Definition: ObjectStorage.php:196
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\key
‪string key()
Definition: ObjectStorage.php:103
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\$positionCounter
‪int $positionCounter
Definition: ObjectStorage.php:76
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage
Definition: ObjectStorage.php:26
‪TYPO3\CMS\Extbase\Persistence
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\offsetSet
‪offsetSet($object, $information)
Definition: ObjectStorage.php:143
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\getArray
‪array getArray()
Definition: ObjectStorage.php:306
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\detach
‪detach($object)
Definition: ObjectStorage.php:232
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\current
‪object current()
Definition: ObjectStorage.php:113
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\$storage
‪array $storage
Definition: ObjectStorage.php:49
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\_memorizeCleanState
‪_memorizeCleanState()
Definition: ObjectStorage.php:334
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\$addedObjectsPositions
‪array $addedObjectsPositions
Definition: ObjectStorage.php:62
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\attach
‪attach($object, $information=null)
Definition: ObjectStorage.php:222
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\rewind
‪rewind()
Definition: ObjectStorage.php:81
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\$isModified
‪bool $isModified
Definition: ObjectStorage.php:55
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\isRelationDirty
‪bool isRelationDirty($object)
Definition: ObjectStorage.php:355
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\offsetExists
‪bool offsetExists($value)
Definition: ObjectStorage.php:158
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\count
‪int count()
Definition: ObjectStorage.php:132
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\next
‪next()
Definition: ObjectStorage.php:122
‪TYPO3\CMS\Core\Utility\MathUtility
Definition: MathUtility.php:21
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\addAll
‪addAll(ObjectStorage $objectStorage)
Definition: ObjectStorage.php:265
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\$warning
‪string $warning
Definition: ObjectStorage.php:34
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\serialize
‪serialize()
Definition: ObjectStorage.php:316
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\removeAll
‪removeAll(ObjectStorage $objectStorage)
Definition: ObjectStorage.php:277
‪TYPO3\CMS\Extbase\Persistence\ObjectMonitoringInterface
Definition: ObjectMonitoringInterface.php:24
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage\offsetUnset
‪offsetUnset($value)
Definition: ObjectStorage.php:169