|
SPL-StandardPHPLibrary
|
00001 <?php 00002 00028 class CachingIterator implements OuterIterator 00029 { 00030 const CALL_TOSTRING = 0x00000001; 00031 const CATCH_GET_CHILD = 0x00000002; 00032 const TOSTRING_USE_KEY = 0x00000010; 00033 const TOSTRING_USE_CURRENT = 0x00000020; 00034 00035 private $it; 00036 private $current; 00037 private $key; 00038 private $valid; 00039 private $strValue; 00040 00047 function __construct(Iterator $it, $flags = self::CALL_TOSTRING) 00048 { 00049 if ((($flags & self::CALL_TOSTRING) && ($flags & (self::TOSTRING_USE_KEY|self::TOSTRING_USE_CURRENT))) 00050 || ((flags & (self::CIT_TOSTRING_USE_KEY|self::CIT_TOSTRING_USE_CURRENT)) == (self::CIT_TOSTRING_USE_KEY|self::CIT_TOSTRING_USE_CURRENT))) 00051 { 00052 throw new InvalidArgumentException('Flags must contain only one of CIT_CALL_TOSTRING, CIT_TOSTRING_USE_KEY, CIT_TOSTRING_USE_CURRENT'); 00053 } 00054 $this->it = $it; 00055 $this->flags = $flags & (0x0000FFFF); 00056 $this->next(); 00057 } 00058 00061 function rewind() 00062 { 00063 $this->it->rewind(); 00064 $this->next(); 00065 } 00066 00069 function next() 00070 { 00071 if ($this->valid = $this->it->valid()) { 00072 $this->current = $this->it->current(); 00073 $this->key = $this->it->key(); 00074 if ($this->flags & self::CALL_TOSTRING) { 00075 if (is_object($this->current)) { 00076 $this->strValue = $this->current->__toString(); 00077 } else { 00078 $this->strValue = (string)$this->current; 00079 } 00080 } 00081 } else { 00082 $this->current = NULL; 00083 $this->key = NULL; 00084 $this->strValue = NULL; 00085 } 00086 $this->it->next(); 00087 } 00088 00091 function valid() 00092 { 00093 return $this->valid; 00094 } 00095 00098 function hasNext() 00099 { 00100 return $this->it->valid(); 00101 } 00102 00105 function current() 00106 { 00107 return $this->current; 00108 } 00109 00112 function key() 00113 { 00114 return $this->key; 00115 } 00116 00122 function __call($func, $params) 00123 { 00124 return call_user_func_array(array($this->it, $func), $params); 00125 } 00126 00131 function __toString() 00132 { 00133 if ($this->flags & self::TOSTRING_USE_KEY) 00134 { 00135 return $this->key; 00136 } 00137 else if ($this->flags & self::TOSTRING_USE_CURRENT) 00138 { 00139 return $this->current; 00140 } 00141 if (!$this->flags & self::CALL_TOSTRING) 00142 { 00143 throw new exception('CachingIterator does not fetch string value (see CachingIterator::__construct)'); 00144 } 00145 return $this->strValue; 00146 } 00147 00151 function getInnerIterator() 00152 { 00153 return $this->it; 00154 } 00155 } 00156 00157 ?>
1.7.5.1