00001 <?php 00031 class Zend_View_Helper_Cycle implements Iterator 00032 { 00033 00038 const DEFAULT_NAME = 'default'; 00039 00045 protected $_pointers = array(self::DEFAULT_NAME =>-1) ; 00046 00052 protected $_data = array(self::DEFAULT_NAME=>array()); 00053 00059 protected $_name = self::DEFAULT_NAME; 00060 00068 public function cycle(array $data = array(), $name = self::DEFAULT_NAME) 00069 { 00070 if(!empty($data)) 00071 $this->_data[$name] = $data; 00072 00073 $this->setName($name); 00074 return $this; 00075 } 00076 00084 public function assign(Array $data , $name = self::DEFAULT_NAME) 00085 { 00086 $this->setName($name); 00087 $this->_data[$name] = $data; 00088 $this->rewind(); 00089 return $this; 00090 } 00091 00098 public function setName($name = self::DEFAULT_NAME) 00099 { 00100 $this->_name = $name; 00101 00102 if(!isset($this->_data[$this->_name])) 00103 $this->_data[$this->_name] = array(); 00104 00105 if(!isset($this->_pointers[$this->_name])) 00106 $this->rewind(); 00107 00108 return $this; 00109 } 00110 00117 public function getName() 00118 { 00119 return $this->_name; 00120 } 00121 00122 00128 public function getAll() 00129 { 00130 return $this->_data[$this->_name]; 00131 } 00132 00138 public function toString() 00139 { 00140 return (string) $this->_data[$this->_name][$this->key()]; 00141 } 00142 00148 public function __toString() 00149 { 00150 return $this->toString(); 00151 } 00152 00158 public function next() 00159 { 00160 $count = count($this->_data[$this->_name]); 00161 if ($this->_pointers[$this->_name] == ($count - 1)) 00162 $this->_pointers[$this->_name] = 0; 00163 else 00164 $this->_pointers[$this->_name] = ++$this->_pointers[$this->_name]; 00165 return $this; 00166 } 00167 00173 public function prev() 00174 { 00175 $count = count($this->_data[$this->_name]); 00176 if ($this->_pointers[$this->_name] <= 0) 00177 $this->_pointers[$this->_name] = $count - 1; 00178 else 00179 $this->_pointers[$this->_name] = --$this->_pointers[$this->_name]; 00180 return $this; 00181 } 00182 00188 public function key() 00189 { 00190 if ($this->_pointers[$this->_name] < 0) 00191 return 0; 00192 else 00193 return $this->_pointers[$this->_name]; 00194 } 00195 00201 public function rewind() 00202 { 00203 $this->_pointers[$this->_name] = -1; 00204 return $this; 00205 } 00206 00212 public function valid() 00213 { 00214 return isset($this->_data[$this->_name][$this->key()]); 00215 } 00216 00222 public function current() 00223 { 00224 return $this->_data[$this->_name][$this->key()]; 00225 } 00226 }