Cycle.php 4.7 KB

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