TYPO3 CMS  TYPO3_6-2
ObjectStorage.php
Go to the documentation of this file.
1 <?php
3 
22 class ObjectStorage implements \Countable, \Iterator, \ArrayAccess, ObjectMonitoringInterface {
23 
32  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.';
33 
48  protected $storage = array();
49 
55  protected $isModified = FALSE;
56 
63  protected $addedObjectsPositions = array();
64 
71  protected $removedObjectsPositions = array();
72 
79  protected $positionCounter = 0;
80 
86  public function rewind() {
87  reset($this->storage);
88  }
89 
95  public function valid() {
96  return current($this->storage) !== FALSE;
97  }
98 
106  public function key() {
107  return key($this->storage);
108  }
109 
115  public function current() {
116  $item = current($this->storage);
117  return $item['obj'];
118  }
119 
125  public function next() {
126  next($this->storage);
127  }
128 
134  public function count() {
135  return count($this->storage);
136  }
137 
145  public function offsetSet($object, $information) {
146  $this->isModified = TRUE;
147  $this->storage[spl_object_hash($object)] = array('obj' => $object, 'inf' => $information);
148 
149  $this->positionCounter++;
150  $this->addedObjectsPositions[spl_object_hash($object)] = $this->positionCounter;
151  }
152 
159  public function offsetExists($object) {
160  return is_object($object) && isset($this->storage[spl_object_hash($object)]);
161  }
162 
169  public function offsetUnset($object) {
170  $this->isModified = TRUE;
171  unset($this->storage[spl_object_hash($object)]);
172 
173  if (empty($this->storage)) {
174  $this->positionCounter = 0;
175  }
176 
177  $this->removedObjectsPositions[spl_object_hash($object)] = $this->addedObjectsPositions[spl_object_hash($object)];
178  unset($this->addedObjectsPositions[spl_object_hash($object)]);
179  }
180 
187  public function offsetGet($object) {
188  return $this->storage[spl_object_hash($object)]['inf'];
189  }
190 
197  public function contains($object) {
198  return $this->offsetExists($object);
199  }
200 
208  public function attach($object, $information = NULL) {
209  $this->offsetSet($object, $information);
210  }
211 
218  public function detach($object) {
219  $this->offsetUnset($object);
220  }
221 
227  public function getInfo() {
228  $item = current($this->storage);
229  return $item['inf'];
230  }
231 
238  public function setInfo($data) {
239  $this->isModified = TRUE;
240  $key = key($this->storage);
241  $this->storage[$key]['inf'] = $data;
242  }
243 
250  public function addAll(ObjectStorage $objectStorage) {
251  foreach ($objectStorage as $object) {
252  $this->attach($object, $objectStorage->getInfo());
253  }
254  }
255 
262  public function removeAll(ObjectStorage $objectStorage) {
263  foreach ($objectStorage as $object) {
264  $this->detach($object);
265  }
266  }
267 
273  public function toArray() {
274  $array = array();
275  $storage = array_values($this->storage);
276  foreach ($storage as $item) {
277  $array[] = $item['obj'];
278  }
279  return $array;
280  }
281 
288  public function serialize() {
289  throw new \RuntimeException('An ObjectStorage instance cannot be serialized.', 1267700868);
290  }
291 
299  public function unserialize($serialized) {
300  throw new \RuntimeException('A ObjectStorage instance cannot be unserialized.', 1267700870);
301  }
302 
308  public function _memorizeCleanState() {
309  $this->isModified = FALSE;
310  }
311 
317  public function _isDirty() {
318  return $this->isModified;
319  }
320 
327  public function isRelationDirty($object) {
328  return (isset($this->addedObjectsPositions[spl_object_hash($object)])
329  && isset($this->removedObjectsPositions[spl_object_hash($object)])
330  && ($this->addedObjectsPositions[spl_object_hash($object)] !== $this->removedObjectsPositions[spl_object_hash($object)]));
331  }
332 
337  public function getPosition($object) {
338  if (!isset($this->addedObjectsPositions[spl_object_hash($object)])) {
339  return NULL;
340  }
341 
342  return $this->addedObjectsPositions[spl_object_hash($object)];
343  }
344 }
addAll(ObjectStorage $objectStorage)
removeAll(ObjectStorage $objectStorage)