Cycle.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @package Zend_View
  16. * @subpackage Helper
  17. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @version $Id$
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. /**
  22. * Helper for alternating between set of values
  23. *
  24. * @package Zend_View
  25. * @subpackage Helper
  26. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  27. * @license http://framework.zend.com/license/new-bsd New BSD License
  28. */
  29. class Zend_View_Helper_Cycle implements Iterator
  30. {
  31. /**
  32. * Default name
  33. * @var string
  34. */
  35. const DEFAULT_NAME = 'default';
  36. /**
  37. * Pointers
  38. *
  39. * @var array
  40. */
  41. protected $_pointers = array(self::DEFAULT_NAME =>-1) ;
  42. /**
  43. * Array of values
  44. *
  45. * @var array
  46. */
  47. protected $_data = array(self::DEFAULT_NAME=>array());
  48. /**
  49. * Actual name of cycle
  50. *
  51. * @var string
  52. */
  53. protected $_name = self::DEFAULT_NAME;
  54. /**
  55. * Add elements to alternate
  56. *
  57. * @param array $data
  58. * @param string $name
  59. * @return Zend_View_Helper_Cycle
  60. */
  61. public function cycle(array $data = array(), $name = self::DEFAULT_NAME)
  62. {
  63. if(!empty($data))
  64. $this->_data[$name] = $data;
  65. $this->setName($name);
  66. return $this;
  67. }
  68. /**
  69. * Add elements to alternate
  70. *
  71. * @param array $data
  72. * @param string $name
  73. * @return Zend_View_Helper_Cycle
  74. */
  75. public function assign(Array $data , $name = self::DEFAULT_NAME)
  76. {
  77. $this->setName($name);
  78. $this->_data[$name] = $data;
  79. $this->rewind();
  80. return $this;
  81. }
  82. /**
  83. * Sets actual name of cycle
  84. *
  85. * @param $name
  86. * @return Zend_View_Helper_Cycle
  87. */
  88. public function setName($name = self::DEFAULT_NAME)
  89. {
  90. $this->_name = $name;
  91. if(!isset($this->_data[$this->_name]))
  92. $this->_data[$this->_name] = array();
  93. if(!isset($this->_pointers[$this->_name]))
  94. $this->rewind();
  95. return $this;
  96. }
  97. /**
  98. * Gets actual name of cycle
  99. *
  100. * @param $name
  101. * @return string
  102. */
  103. public function getName()
  104. {
  105. return $this->_name;
  106. }
  107. /**
  108. * Return all elements
  109. *
  110. * @return array
  111. */
  112. public function getAll()
  113. {
  114. return $this->_data[$this->_name];
  115. }
  116. /**
  117. * Turn helper into string
  118. *
  119. * @return string
  120. */
  121. public function toString()
  122. {
  123. return (string) $this->_data[$this->_name][$this->key()];
  124. }
  125. /**
  126. * Cast to string
  127. *
  128. * @return string
  129. */
  130. public function __toString()
  131. {
  132. return $this->toString();
  133. }
  134. /**
  135. * Move to next value
  136. *
  137. * @return Zend_View_Helper_Cycle
  138. */
  139. public function next()
  140. {
  141. $count = count($this->_data[$this->_name]);
  142. if ($this->_pointers[$this->_name] == ($count - 1))
  143. $this->_pointers[$this->_name] = 0;
  144. else
  145. $this->_pointers[$this->_name] = ++$this->_pointers[$this->_name];
  146. return $this;
  147. }
  148. /**
  149. * Move to previous value
  150. *
  151. * @return Zend_View_Helper_Cycle
  152. */
  153. public function prev()
  154. {
  155. $count = count($this->_data[$this->_name]);
  156. if ($this->_pointers[$this->_name] <= 0)
  157. $this->_pointers[$this->_name] = $count - 1;
  158. else
  159. $this->_pointers[$this->_name] = --$this->_pointers[$this->_name];
  160. return $this;
  161. }
  162. /**
  163. * Return iteration number
  164. *
  165. * @return int
  166. */
  167. public function key()
  168. {
  169. if ($this->_pointers[$this->_name] < 0)
  170. return 0;
  171. else
  172. return $this->_pointers[$this->_name];
  173. }
  174. /**
  175. * Rewind pointer
  176. *
  177. * @return Zend_View_Helper_Cycle
  178. */
  179. public function rewind()
  180. {
  181. $this->_pointers[$this->_name] = -1;
  182. return $this;
  183. }
  184. /**
  185. * Check if element is valid
  186. *
  187. * @return bool
  188. */
  189. public function valid()
  190. {
  191. return isset($this->_data[$this->_name][$this->key()]);
  192. }
  193. /**
  194. * Return current element
  195. *
  196. * @return mixed
  197. */
  198. public function current()
  199. {
  200. return $this->_data[$this->_name][$this->key()];
  201. }
  202. }