PageRenderTime 53ms CodeModel.GetById 4ms RepoModel.GetById 1ms app.codeStats 0ms

/src/application/libraries/Zend/View/Helper/Cycle.php

https://bitbucket.org/masnug/grc276-blog-laravel
PHP | 225 lines | 85 code | 23 blank | 117 comment | 7 complexity | 76390fa456b90098555352652c36f54a MD5 | raw file
  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-2011 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @version $Id: Cycle.php 23953 2011-05-03 05:47:39Z ralph $
  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-2011 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 string $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. * @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. }