1<?php 2 3/** @file splobjectstorage.inc 4 * @ingroup SPL 5 * @brief class SplObjectStorage 6 * @author Marcus Boerger 7 * @date 2003 - 2009 8 * 9 * SPL - Standard PHP Library 10 */ 11 12/** 13 * @brief Object storage 14 * @author Marcus Boerger 15 * @version 1.1 16 * @since PHP 5.1.2 17 * 18 * This container allows to store objects uniquly without the need to compare 19 * them one by one. This is only possible internally. The code representation 20 * here therefore has a complexity of O(n) while the actual implementation has 21 * complexity O(1). 22 */ 23class SplObjectStorage implements Iterator, Countable, ArrayAccess 24{ 25 private $storage = array(); 26 private $index = 0; 27 28 /** Rewind to top iterator as set in constructor 29 */ 30 function rewind() 31 { 32 rewind($this->storage); 33 } 34 35 /** @return whether iterator is valid 36 */ 37 function valid() 38 { 39 return key($this->storage) !== false; 40 } 41 42 /** @return current key 43 */ 44 function key() 45 { 46 return $this->index; 47 } 48 49 /** @return current object 50 */ 51 function current() 52 { 53 $element = current($this->storage); 54 return $element ? $element[0] : NULL 55 } 56 57 /** @return get current object's associated information 58 * @since 5.3.0 59 */ 60 function getInfo() 61 { 62 $element = current($this->storage); 63 return $element ? $element[1] : NULL 64 } 65 66 /** @return set current object's associated information 67 * @since 5.3.0 68 */ 69 function setInfo($inf = NULL) 70 { 71 if ($this->valid()) { 72 $this->storage[$this->index][1] = $inf; 73 } 74 } 75 76 /** Forward to next element 77 */ 78 function next() 79 { 80 next($this->storage); 81 $this->index++; 82 } 83 84 /** @return number of objects in storage 85 */ 86 function count() 87 { 88 return count($this->storage); 89 } 90 91 /** @param $obj object to look for 92 * @return whether $obj is contained in storage 93 */ 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 109 /** @param $obj new object to attach to storage or object whose 110 * associative information is to be replaced 111 * @param $inf associative information stored along the object 112 */ 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 121 /** @param $obj object to remove from storage 122 */ 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 139 /** @param $obj new object to attach to storage or object whose 140 * associative information is to be replaced 141 * @param $inf associative information stored along the object 142 * @since 5.3.0 143 */ 144 function offsetSet($obj, $inf) 145 { 146 $this->attach($obj, $inf); 147 } 148 149 /** @param $obj Exising object to look for 150 * @return associative information stored with object 151 * @throw UnexpectedValueException if Object $obj is not contained in 152 * storage 153 * @since 5.3.0 154 */ 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 170 /** @param $obj Exising object to look for 171 * @return associative information stored with object 172 * @since 5.3.0 173 */ 174 function offsetUnset($obj) 175 { 176 $this->detach($obj); 177 } 178 179 /** @param $obj object to look for 180 * @return whether $obj is contained in storage 181 */ 182 function offsetEsists($obj) 183 { 184 return $this->contains($obj); 185 } 186} 187 188?> 189