TYPO3 CMS  TYPO3_7-6
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 
23 class ObjectStorage implements \Countable, \Iterator, \ArrayAccess, ObjectMonitoringInterface
24 {
33  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.';
34 
49  protected $storage = [];
50 
56  protected $isModified = false;
57 
64  protected $addedObjectsPositions = [];
65 
72  protected $removedObjectsPositions = [];
73 
80  protected $positionCounter = 0;
81 
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 
130  public function next()
131  {
132  next($this->storage);
133  }
134 
140  public function count()
141  {
142  return count($this->storage);
143  }
144 
152  public function offsetSet($object, $information)
153  {
154  $this->isModified = true;
155  $this->storage[spl_object_hash($object)] = ['obj' => $object, 'inf' => $information];
156 
157  $this->positionCounter++;
158  $this->addedObjectsPositions[spl_object_hash($object)] = $this->positionCounter;
159  }
160 
167  public function offsetExists($object)
168  {
169  return is_object($object) && isset($this->storage[spl_object_hash($object)]);
170  }
171 
178  public function offsetUnset($object)
179  {
180  $this->isModified = true;
181  unset($this->storage[spl_object_hash($object)]);
182 
183  if (empty($this->storage)) {
184  $this->positionCounter = 0;
185  }
186 
187  $this->removedObjectsPositions[spl_object_hash($object)] = $this->addedObjectsPositions[spl_object_hash($object)];
188  unset($this->addedObjectsPositions[spl_object_hash($object)]);
189  }
190 
197  public function offsetGet($object)
198  {
199  return $this->storage[spl_object_hash($object)]['inf'];
200  }
201 
208  public function contains($object)
209  {
210  return $this->offsetExists($object);
211  }
212 
220  public function attach($object, $information = null)
221  {
222  $this->offsetSet($object, $information);
223  }
224 
231  public function detach($object)
232  {
233  $this->offsetUnset($object);
234  }
235 
241  public function getInfo()
242  {
243  $item = current($this->storage);
244  return $item['inf'];
245  }
246 
253  public function setInfo($data)
254  {
255  $this->isModified = true;
256  $key = key($this->storage);
257  $this->storage[$key]['inf'] = $data;
258  }
259 
266  public function addAll(ObjectStorage $objectStorage)
267  {
268  foreach ($objectStorage as $object) {
269  $this->attach($object, $objectStorage->getInfo());
270  }
271  }
272 
279  public function removeAll(ObjectStorage $objectStorage)
280  {
281  foreach ($objectStorage as $object) {
282  $this->detach($object);
283  }
284  }
285 
291  public function toArray()
292  {
293  $array = [];
294  $storage = array_values($this->storage);
295  foreach ($storage as $item) {
296  $array[] = $item['obj'];
297  }
298  return $array;
299  }
300 
307  public function serialize()
308  {
309  throw new \RuntimeException('An ObjectStorage instance cannot be serialized.', 1267700868);
310  }
311 
319  public function unserialize($serialized)
320  {
321  throw new \RuntimeException('A ObjectStorage instance cannot be unserialized.', 1267700870);
322  }
323 
329  public function _memorizeCleanState()
330  {
331  $this->isModified = false;
332  }
333 
339  public function _isDirty()
340  {
341  return $this->isModified;
342  }
343 
350  public function isRelationDirty($object)
351  {
352  return isset($this->addedObjectsPositions[spl_object_hash($object)])
353  && isset($this->removedObjectsPositions[spl_object_hash($object)])
354  && ($this->addedObjectsPositions[spl_object_hash($object)] !== $this->removedObjectsPositions[spl_object_hash($object)]);
355  }
356 
361  public function getPosition($object)
362  {
363  if (!isset($this->addedObjectsPositions[spl_object_hash($object)])) {
364  return null;
365  }
366 
367  return $this->addedObjectsPositions[spl_object_hash($object)];
368  }
369 }
addAll(ObjectStorage $objectStorage)
removeAll(ObjectStorage $objectStorage)