splobjectstorage.inc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. /** @file splobjectstorage.inc
  3. * @ingroup SPL
  4. * @brief class SplObjectStorage
  5. * @author Marcus Boerger
  6. * @date 2003 - 2009
  7. *
  8. * SPL - Standard PHP Library
  9. */
  10. /**
  11. * @brief Object storage
  12. * @author Marcus Boerger
  13. * @version 1.1
  14. * @since PHP 5.1.2
  15. *
  16. * This container allows to store objects uniquly without the need to compare
  17. * them one by one. This is only possible internally. The code representation
  18. * here therefore has a complexity of O(n) while the actual implementation has
  19. * complexity O(1).
  20. */
  21. class SplObjectStorage implements Iterator, Countable, ArrayAccess
  22. {
  23. private $storage = array();
  24. private $index = 0;
  25. /** Rewind to top iterator as set in constructor
  26. */
  27. function rewind()
  28. {
  29. rewind($this->storage);
  30. }
  31. /** @return whether iterator is valid
  32. */
  33. function valid()
  34. {
  35. return key($this->storage) !== false;
  36. }
  37. /** @return current key
  38. */
  39. function key()
  40. {
  41. return $this->index;
  42. }
  43. /** @return current object
  44. */
  45. function current()
  46. {
  47. $element = current($this->storage);
  48. return $element ? $element[0] : NULL
  49. }
  50. /** @return get current object's associated information
  51. * @since 5.3.0
  52. */
  53. function getInfo()
  54. {
  55. $element = current($this->storage);
  56. return $element ? $element[1] : NULL
  57. }
  58. /** @return set current object's associated information
  59. * @since 5.3.0
  60. */
  61. function setInfo($inf = NULL)
  62. {
  63. if ($this->valid()) {
  64. $this->storage[$this->index][1] = $inf;
  65. }
  66. }
  67. /** Forward to next element
  68. */
  69. function next()
  70. {
  71. next($this->storage);
  72. $this->index++;
  73. }
  74. /** @return number of objects in storage
  75. */
  76. function count()
  77. {
  78. return count($this->storage);
  79. }
  80. /** @param $obj object to look for
  81. * @return whether $obj is contained in storage
  82. */
  83. function contains($obj)
  84. {
  85. if (is_object($obj))
  86. {
  87. foreach($this->storage as $element)
  88. {
  89. if ($object === $element[0])
  90. {
  91. return true;
  92. }
  93. }
  94. }
  95. return false;
  96. }
  97. /** @param $obj new object to attach to storage or object whose
  98. * associative information is to be replaced
  99. * @param $inf associative information stored along the object
  100. */
  101. function attach($obj, $inf = NULL)
  102. {
  103. if (is_object($obj) && !$this->contains($obj))
  104. {
  105. $this->storage[] = array($obj, $inf);
  106. }
  107. }
  108. /** @param $obj object to remove from storage
  109. */
  110. function detach($obj)
  111. {
  112. if (is_object($obj))
  113. {
  114. foreach($this->storage as $idx => $element)
  115. {
  116. if ($object === $element[0])
  117. {
  118. unset($this->storage[$idx]);
  119. $this->rewind();
  120. return;
  121. }
  122. }
  123. }
  124. }
  125. /** @param $obj new object to attach to storage or object whose
  126. * associative information is to be replaced
  127. * @param $inf associative information stored along the object
  128. * @since 5.3.0
  129. */
  130. function offsetSet($obj, $inf)
  131. {
  132. $this->attach($obj, $inf);
  133. }
  134. /** @param $obj Exising object to look for
  135. * @return associative information stored with object
  136. * @throw UnexpectedValueException if Object $obj is not contained in
  137. * storage
  138. * @since 5.3.0
  139. */
  140. function offsetGet($obj)
  141. {
  142. if (is_object($obj))
  143. {
  144. foreach($this->storage as $idx => $element)
  145. {
  146. if ($object === $element[0])
  147. {
  148. return $element[1];
  149. }
  150. }
  151. }
  152. throw new UnexpectedValueException('Object not found');
  153. }
  154. /** @param $obj Exising object to look for
  155. * @return associative information stored with object
  156. * @since 5.3.0
  157. */
  158. function offsetUnset($obj)
  159. {
  160. $this->detach($obj);
  161. }
  162. /** @param $obj object to look for
  163. * @return whether $obj is contained in storage
  164. */
  165. function offsetEsists($obj)
  166. {
  167. return $this->contains($obj);
  168. }
  169. }
  170. ?>