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