TYPO3 CMS  TYPO3_8-7
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 
85  public function rewind()
86  {
87  reset($this->storage);
88  }
89 
95  public function valid()
96  {
97  return current($this->storage) !== false;
98  }
99 
107  public function key()
108  {
109  return key($this->storage);
110  }
111 
117  public function current()
118  {
119  $item = current($this->storage);
120  return $item['obj'];
121  }
122 
126  public function next()
127  {
128  next($this->storage);
129  }
130 
136  public function count()
137  {
138  return count($this->storage);
139  }
140 
147  public function offsetSet($object, $information)
148  {
149  $this->isModified = true;
150  $this->storage[spl_object_hash($object)] = ['obj' => $object, 'inf' => $information];
151 
152  $this->positionCounter++;
153  $this->addedObjectsPositions[spl_object_hash($object)] = $this->positionCounter;
154  }
155 
162  public function offsetExists($object)
163  {
164  return is_object($object) && isset($this->storage[spl_object_hash($object)]);
165  }
166 
172  public function offsetUnset($object)
173  {
174  $this->isModified = true;
175  unset($this->storage[spl_object_hash($object)]);
176 
177  if (empty($this->storage)) {
178  $this->positionCounter = 0;
179  }
180 
181  $this->removedObjectsPositions[spl_object_hash($object)] = $this->addedObjectsPositions[spl_object_hash($object)];
182  unset($this->addedObjectsPositions[spl_object_hash($object)]);
183  }
184 
191  public function offsetGet($object)
192  {
193  return $this->storage[spl_object_hash($object)]['inf'];
194  }
195 
202  public function contains($object)
203  {
204  return $this->offsetExists($object);
205  }
206 
213  public function attach($object, $information = null)
214  {
215  $this->offsetSet($object, $information);
216  }
217 
223  public function detach($object)
224  {
225  $this->offsetUnset($object);
226  }
227 
233  public function getInfo()
234  {
235  $item = current($this->storage);
236  return $item['inf'];
237  }
238 
244  public function setInfo($data)
245  {
246  $this->isModified = true;
247  $key = key($this->storage);
248  $this->storage[$key]['inf'] = $data;
249  }
250 
256  public function addAll(ObjectStorage $objectStorage)
257  {
258  foreach ($objectStorage as $object) {
259  $this->attach($object, $objectStorage->getInfo());
260  }
261  }
262 
268  public function removeAll(ObjectStorage $objectStorage)
269  {
270  foreach ($objectStorage as $object) {
271  $this->detach($object);
272  }
273  }
274 
280  public function toArray()
281  {
282  $array = [];
283  $storage = array_values($this->storage);
284  foreach ($storage as $item) {
285  $array[] = $item['obj'];
286  }
287  return $array;
288  }
289 
297  public function getArray()
298  {
299  return $this->toArray();
300  }
301 
307  public function serialize()
308  {
309  throw new \RuntimeException('An ObjectStorage instance cannot be serialized.', 1267700868);
310  }
311 
318  public function unserialize($serialized)
319  {
320  throw new \RuntimeException('A ObjectStorage instance cannot be unserialized.', 1267700870);
321  }
322 
326  public function _memorizeCleanState()
327  {
328  $this->isModified = false;
329  }
330 
336  public function _isDirty()
337  {
338  return $this->isModified;
339  }
340 
347  public function isRelationDirty($object)
348  {
349  return isset($this->addedObjectsPositions[spl_object_hash($object)])
350  && isset($this->removedObjectsPositions[spl_object_hash($object)])
351  && ($this->addedObjectsPositions[spl_object_hash($object)] !== $this->removedObjectsPositions[spl_object_hash($object)]);
352  }
353 
358  public function getPosition($object)
359  {
360  if (!isset($this->addedObjectsPositions[spl_object_hash($object)])) {
361  return null;
362  }
363 
364  return $this->addedObjectsPositions[spl_object_hash($object)];
365  }
366 }
addAll(ObjectStorage $objectStorage)
removeAll(ObjectStorage $objectStorage)