SPL-StandardPHPLibrary
splobjectstorage.inc
Go to the documentation of this file.
1 <?php
2 
24 {
25  private $storage = array();
26  private $index = 0;
27 
30  function rewind()
31  {
32  rewind($this->storage);
33  }
34 
37  function valid()
38  {
39  return key($this->storage) !== false;
40  }
41 
44  function key()
45  {
46  return $this->index;
47  }
48 
51  function current()
52  {
53  $element = current($this->storage);
54  return $element ? $element[0] : NULL
55  }
56 
60  function getInfo()
61  {
62  $element = current($this->storage);
63  return $element ? $element[1] : NULL
64  }
65 
69  function setInfo($inf = NULL)
70  {
71  if ($this->valid()) {
72  $this->storage[$this->index][1] = $inf;
73  }
74  }
75 
78  function next()
79  {
80  next($this->storage);
81  $this->index++;
82  }
83 
86  function count()
87  {
88  return count($this->storage);
89  }
90 
94  function contains($obj)
95  {
96  if (is_object($obj))
97  {
98  foreach($this->storage as $element)
99  {
100  if ($object === $element[0])
101  {
102  return true;
103  }
104  }
105  }
106  return false;
107  }
108 
113  function attach($obj, $inf = NULL)
114  {
115  if (is_object($obj) && !$this->contains($obj))
116  {
117  $this->storage[] = array($obj, $inf);
118  }
119  }
120 
123  function detach($obj)
124  {
125  if (is_object($obj))
126  {
127  foreach($this->storage as $idx => $element)
128  {
129  if ($object === $element[0])
130  {
131  unset($this->storage[$idx]);
132  $this->rewind();
133  return;
134  }
135  }
136  }
137  }
138 
144  function offsetSet($obj, $inf)
145  {
146  $this->attach($obj, $inf);
147  }
148 
155  function offsetGet($obj)
156  {
157  if (is_object($obj))
158  {
159  foreach($this->storage as $idx => $element)
160  {
161  if ($object === $element[0])
162  {
163  return $element[1];
164  }
165  }
166  }
167  throw new UnexpectedValueException('Object not found');
168  }
169 
174  function offsetUnset($obj)
175  {
176  $this->detach($obj);
177  }
178 
182  function offsetEsists($obj)
183  {
184  return $this->contains($obj);
185  }
186 }
187 
188 ?>
Exception thrown to indicate an unexpected value.
Definition: spl.php:478
Interface to override array access of objects.
Definition: spl.php:486
next()
Forward to next element.
rewind()
Rewind to top iterator as set in constructor.
Object storage.
Basic iterator.
Definition: spl.php:549
attach($obj, $inf=NULL)
This Interface allows to hook into the global count() function.
Definition: spl.php:576