|
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: Abstract.php 20096 2010-01-06 02:05:09Z bkarwin $ |
|
20 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
21 */ |
|
22 |
|
23 /** |
|
24 * Abstract class representing container for placeholder values |
|
25 * |
|
26 * @package Zend_View |
|
27 * @subpackage Helper |
|
28 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
|
29 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
30 */ |
|
31 abstract class Zend_View_Helper_Placeholder_Container_Abstract extends ArrayObject |
|
32 { |
|
33 /** |
|
34 * Whether or not to override all contents of placeholder |
|
35 * @const string |
|
36 */ |
|
37 const SET = 'SET'; |
|
38 |
|
39 /** |
|
40 * Whether or not to append contents to placeholder |
|
41 * @const string |
|
42 */ |
|
43 const APPEND = 'APPEND'; |
|
44 |
|
45 /** |
|
46 * Whether or not to prepend contents to placeholder |
|
47 * @const string |
|
48 */ |
|
49 const PREPEND = 'PREPEND'; |
|
50 |
|
51 /** |
|
52 * What text to prefix the placeholder with when rendering |
|
53 * @var string |
|
54 */ |
|
55 protected $_prefix = ''; |
|
56 |
|
57 /** |
|
58 * What text to append the placeholder with when rendering |
|
59 * @var string |
|
60 */ |
|
61 protected $_postfix = ''; |
|
62 |
|
63 /** |
|
64 * What string to use between individual items in the placeholder when rendering |
|
65 * @var string |
|
66 */ |
|
67 protected $_separator = ''; |
|
68 |
|
69 /** |
|
70 * What string to use as the indentation of output, this will typically be spaces. Eg: ' ' |
|
71 * @var string |
|
72 */ |
|
73 protected $_indent = ''; |
|
74 |
|
75 /** |
|
76 * Whether or not we're already capturing for this given container |
|
77 * @var bool |
|
78 */ |
|
79 protected $_captureLock = false; |
|
80 |
|
81 /** |
|
82 * What type of capture (overwrite (set), append, prepend) to use |
|
83 * @var string |
|
84 */ |
|
85 protected $_captureType; |
|
86 |
|
87 /** |
|
88 * Key to which to capture content |
|
89 * @var string |
|
90 */ |
|
91 protected $_captureKey; |
|
92 |
|
93 /** |
|
94 * Constructor - This is needed so that we can attach a class member as the ArrayObject container |
|
95 * |
|
96 * @return void |
|
97 */ |
|
98 public function __construct() |
|
99 { |
|
100 parent::__construct(array(), parent::ARRAY_AS_PROPS); |
|
101 } |
|
102 |
|
103 /** |
|
104 * Set a single value |
|
105 * |
|
106 * @param mixed $value |
|
107 * @return void |
|
108 */ |
|
109 public function set($value) |
|
110 { |
|
111 $this->exchangeArray(array($value)); |
|
112 } |
|
113 |
|
114 /** |
|
115 * Prepend a value to the top of the container |
|
116 * |
|
117 * @param mixed $value |
|
118 * @return void |
|
119 */ |
|
120 public function prepend($value) |
|
121 { |
|
122 $values = $this->getArrayCopy(); |
|
123 array_unshift($values, $value); |
|
124 $this->exchangeArray($values); |
|
125 } |
|
126 |
|
127 /** |
|
128 * Retrieve container value |
|
129 * |
|
130 * If single element registered, returns that element; otherwise, |
|
131 * serializes to array. |
|
132 * |
|
133 * @return mixed |
|
134 */ |
|
135 public function getValue() |
|
136 { |
|
137 if (1 == count($this)) { |
|
138 $keys = $this->getKeys(); |
|
139 $key = array_shift($keys); |
|
140 return $this[$key]; |
|
141 } |
|
142 |
|
143 return $this->getArrayCopy(); |
|
144 } |
|
145 |
|
146 /** |
|
147 * Set prefix for __toString() serialization |
|
148 * |
|
149 * @param string $prefix |
|
150 * @return Zend_View_Helper_Placeholder_Container |
|
151 */ |
|
152 public function setPrefix($prefix) |
|
153 { |
|
154 $this->_prefix = (string) $prefix; |
|
155 return $this; |
|
156 } |
|
157 |
|
158 /** |
|
159 * Retrieve prefix |
|
160 * |
|
161 * @return string |
|
162 */ |
|
163 public function getPrefix() |
|
164 { |
|
165 return $this->_prefix; |
|
166 } |
|
167 |
|
168 /** |
|
169 * Set postfix for __toString() serialization |
|
170 * |
|
171 * @param string $postfix |
|
172 * @return Zend_View_Helper_Placeholder_Container |
|
173 */ |
|
174 public function setPostfix($postfix) |
|
175 { |
|
176 $this->_postfix = (string) $postfix; |
|
177 return $this; |
|
178 } |
|
179 |
|
180 /** |
|
181 * Retrieve postfix |
|
182 * |
|
183 * @return string |
|
184 */ |
|
185 public function getPostfix() |
|
186 { |
|
187 return $this->_postfix; |
|
188 } |
|
189 |
|
190 /** |
|
191 * Set separator for __toString() serialization |
|
192 * |
|
193 * Used to implode elements in container |
|
194 * |
|
195 * @param string $separator |
|
196 * @return Zend_View_Helper_Placeholder_Container |
|
197 */ |
|
198 public function setSeparator($separator) |
|
199 { |
|
200 $this->_separator = (string) $separator; |
|
201 return $this; |
|
202 } |
|
203 |
|
204 /** |
|
205 * Retrieve separator |
|
206 * |
|
207 * @return string |
|
208 */ |
|
209 public function getSeparator() |
|
210 { |
|
211 return $this->_separator; |
|
212 } |
|
213 |
|
214 /** |
|
215 * Set the indentation string for __toString() serialization, |
|
216 * optionally, if a number is passed, it will be the number of spaces |
|
217 * |
|
218 * @param string|int $indent |
|
219 * @return Zend_View_Helper_Placeholder_Container_Abstract |
|
220 */ |
|
221 public function setIndent($indent) |
|
222 { |
|
223 $this->_indent = $this->getWhitespace($indent); |
|
224 return $this; |
|
225 } |
|
226 |
|
227 /** |
|
228 * Retrieve indentation |
|
229 * |
|
230 * @return string |
|
231 */ |
|
232 public function getIndent() |
|
233 { |
|
234 return $this->_indent; |
|
235 } |
|
236 |
|
237 /** |
|
238 * Retrieve whitespace representation of $indent |
|
239 * |
|
240 * @param int|string $indent |
|
241 * @return string |
|
242 */ |
|
243 public function getWhitespace($indent) |
|
244 { |
|
245 if (is_int($indent)) { |
|
246 $indent = str_repeat(' ', $indent); |
|
247 } |
|
248 |
|
249 return (string) $indent; |
|
250 } |
|
251 |
|
252 /** |
|
253 * Start capturing content to push into placeholder |
|
254 * |
|
255 * @param int $type How to capture content into placeholder; append, prepend, or set |
|
256 * @return void |
|
257 * @throws Zend_View_Helper_Placeholder_Exception if nested captures detected |
|
258 */ |
|
259 public function captureStart($type = Zend_View_Helper_Placeholder_Container_Abstract::APPEND, $key = null) |
|
260 { |
|
261 if ($this->_captureLock) { |
|
262 require_once 'Zend/View/Helper/Placeholder/Container/Exception.php'; |
|
263 $e = new Zend_View_Helper_Placeholder_Container_Exception('Cannot nest placeholder captures for the same placeholder'); |
|
264 $e->setView($this->view); |
|
265 throw $e; |
|
266 } |
|
267 |
|
268 $this->_captureLock = true; |
|
269 $this->_captureType = $type; |
|
270 if ((null !== $key) && is_scalar($key)) { |
|
271 $this->_captureKey = (string) $key; |
|
272 } |
|
273 ob_start(); |
|
274 } |
|
275 |
|
276 /** |
|
277 * End content capture |
|
278 * |
|
279 * @return void |
|
280 */ |
|
281 public function captureEnd() |
|
282 { |
|
283 $data = ob_get_clean(); |
|
284 $key = null; |
|
285 $this->_captureLock = false; |
|
286 if (null !== $this->_captureKey) { |
|
287 $key = $this->_captureKey; |
|
288 } |
|
289 switch ($this->_captureType) { |
|
290 case self::SET: |
|
291 if (null !== $key) { |
|
292 $this[$key] = $data; |
|
293 } else { |
|
294 $this->exchangeArray(array($data)); |
|
295 } |
|
296 break; |
|
297 case self::PREPEND: |
|
298 if (null !== $key) { |
|
299 $array = array($key => $data); |
|
300 $values = $this->getArrayCopy(); |
|
301 $final = $array + $values; |
|
302 $this->exchangeArray($final); |
|
303 } else { |
|
304 $this->prepend($data); |
|
305 } |
|
306 break; |
|
307 case self::APPEND: |
|
308 default: |
|
309 if (null !== $key) { |
|
310 if (empty($this[$key])) { |
|
311 $this[$key] = $data; |
|
312 } else { |
|
313 $this[$key] .= $data; |
|
314 } |
|
315 } else { |
|
316 $this[$this->nextIndex()] = $data; |
|
317 } |
|
318 break; |
|
319 } |
|
320 } |
|
321 |
|
322 /** |
|
323 * Get keys |
|
324 * |
|
325 * @return array |
|
326 */ |
|
327 public function getKeys() |
|
328 { |
|
329 $array = $this->getArrayCopy(); |
|
330 return array_keys($array); |
|
331 } |
|
332 |
|
333 /** |
|
334 * Next Index |
|
335 * |
|
336 * as defined by the PHP manual |
|
337 * @return int |
|
338 */ |
|
339 public function nextIndex() |
|
340 { |
|
341 $keys = $this->getKeys(); |
|
342 if (0 == count($keys)) { |
|
343 return 0; |
|
344 } |
|
345 |
|
346 return $nextIndex = max($keys) + 1; |
|
347 } |
|
348 |
|
349 /** |
|
350 * Render the placeholder |
|
351 * |
|
352 * @return string |
|
353 */ |
|
354 public function toString($indent = null) |
|
355 { |
|
356 $indent = ($indent !== null) |
|
357 ? $this->getWhitespace($indent) |
|
358 : $this->getIndent(); |
|
359 |
|
360 $items = $this->getArrayCopy(); |
|
361 $return = $indent |
|
362 . $this->getPrefix() |
|
363 . implode($this->getSeparator(), $items) |
|
364 . $this->getPostfix(); |
|
365 $return = preg_replace("/(\r\n?|\n)/", '$1' . $indent, $return); |
|
366 return $return; |
|
367 } |
|
368 |
|
369 /** |
|
370 * Serialize object to string |
|
371 * |
|
372 * @return string |
|
373 */ |
|
374 public function __toString() |
|
375 { |
|
376 return $this->toString(); |
|
377 } |
|
378 } |