TYPO3CMS  8
 All Classes Namespaces Files Functions Variables Pages
ObjectStorage.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Extbase\Persistence;
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 
308  public function getArray()
309  {
310  return $this->toArray();
311  }
312 
319  public function serialize()
320  {
321  throw new \RuntimeException('An ObjectStorage instance cannot be serialized.', 1267700868);
322  }
323 
331  public function unserialize($serialized)
332  {
333  throw new \RuntimeException('A ObjectStorage instance cannot be unserialized.', 1267700870);
334  }
335 
341  public function _memorizeCleanState()
342  {
343  $this->isModified = false;
344  }
345 
351  public function _isDirty()
352  {
353  return $this->isModified;
354  }
355 
362  public function isRelationDirty($object)
363  {
364  return isset($this->addedObjectsPositions[spl_object_hash($object)])
365  && isset($this->removedObjectsPositions[spl_object_hash($object)])
366  && ($this->addedObjectsPositions[spl_object_hash($object)] !== $this->removedObjectsPositions[spl_object_hash($object)]);
367  }
368 
373  public function getPosition($object)
374  {
375  if (!isset($this->addedObjectsPositions[spl_object_hash($object)])) {
376  return null;
377  }
378 
379  return $this->addedObjectsPositions[spl_object_hash($object)];
380  }
381 }
removeAll(ObjectStorage $objectStorage)
addAll(ObjectStorage $objectStorage)