3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\View\Helper\Placeholder\Container
;
13 use Zend\View\Exception
;
16 * Abstract class representing container for placeholder values
18 abstract class AbstractContainer
extends ArrayObject
21 * Whether or not to override all contents of placeholder
28 * Whether or not to append contents to placeholder
32 const APPEND
= 'APPEND';
35 * Whether or not to prepend contents to placeholder
39 const PREPEND
= 'PREPEND';
42 * Key to which to capture content
46 protected $captureKey;
49 * Whether or not we're already capturing for this given container
53 protected $captureLock = false;
56 * What type of capture (overwrite (set), append, prepend) to use
60 protected $captureType;
63 * What string to use as the indentation of output, this will typically be spaces. Eg: ' '
67 protected $indent = '';
70 * What text to append the placeholder with when rendering
74 protected $postfix = '';
77 * What text to prefix the placeholder with when rendering
81 protected $prefix = '';
84 * What string to use between individual items in the placeholder when rendering
88 protected $separator = '';
91 * Constructor - This is needed so that we can attach a class member as the ArrayObject container
93 public function __construct()
95 parent
::__construct(array(), parent
::ARRAY_AS_PROPS
);
99 * Serialize object to string
103 public function __toString()
105 return $this->toString();
109 * Render the placeholder
111 * @param null|int|string $indent
114 public function toString($indent = null)
116 $indent = ($indent !== null)
117 ?
$this->getWhitespace($indent)
118 : $this->getIndent();
120 $items = $this->getArrayCopy();
123 . implode($this->getSeparator(), $items)
124 . $this->getPostfix();
125 $return = preg_replace("/(\r\n?|\n)/", '$1' . $indent, $return);
131 * Start capturing content to push into placeholder
133 * @param string $type How to capture content into placeholder; append, prepend, or set
134 * @param mixed $key Key to which to capture content
135 * @throws Exception\RuntimeException if nested captures detected
138 public function captureStart($type = AbstractContainer
::APPEND
, $key = null)
140 if ($this->captureLock
) {
141 throw new Exception\
RuntimeException(
142 'Cannot nest placeholder captures for the same placeholder'
146 $this->captureLock
= true;
147 $this->captureType
= $type;
148 if ((null !== $key) && is_scalar($key)) {
149 $this->captureKey
= (string) $key;
155 * End content capture
159 public function captureEnd()
161 $data = ob_get_clean();
163 $this->captureLock
= false;
164 if (null !== $this->captureKey
) {
165 $key = $this->captureKey
;
167 switch ($this->captureType
) {
172 $this->exchangeArray(array($data));
177 $array = array($key => $data);
178 $values = $this->getArrayCopy();
179 $final = $array +
$values;
180 $this->exchangeArray($final);
182 $this->prepend($data);
188 if (empty($this[$key])) {
191 $this[$key] .= $data;
194 $this[$this->nextIndex()] = $data;
205 public function getKeys()
207 $array = $this->getArrayCopy();
209 return array_keys($array);
213 * Retrieve container value
215 * If single element registered, returns that element; otherwise,
216 * serializes to array.
220 public function getValue()
222 if (1 == count($this)) {
223 $keys = $this->getKeys();
224 $key = array_shift($keys);
228 return $this->getArrayCopy();
232 * Retrieve whitespace representation of $indent
234 * @param int|string $indent
237 public function getWhitespace($indent)
239 if (is_int($indent)) {
240 $indent = str_repeat(' ', $indent);
243 return (string) $indent;
249 * @param mixed $value
252 public function set($value)
254 $this->exchangeArray(array($value));
260 * Prepend a value to the top of the container
262 * @param mixed $value
265 public function prepend($value)
267 $values = $this->getArrayCopy();
268 array_unshift($values, $value);
269 $this->exchangeArray($values);
275 * Append a value to the end of the container
277 * @param mixed $value
280 public function append($value)
282 parent
::append($value);
287 * Next Index as defined by the PHP manual
291 public function nextIndex()
293 $keys = $this->getKeys();
294 if (0 == count($keys)) {
298 return $nextIndex = max($keys) +
1;
302 * Set the indentation string for __toString() serialization,
303 * optionally, if a number is passed, it will be the number of spaces
305 * @param string|int $indent
308 public function setIndent($indent)
310 $this->indent
= $this->getWhitespace($indent);
315 * Retrieve indentation
319 public function getIndent()
321 return $this->indent
;
325 * Set postfix for __toString() serialization
327 * @param string $postfix
330 public function setPostfix($postfix)
332 $this->postfix
= (string) $postfix;
341 public function getPostfix()
343 return $this->postfix
;
347 * Set prefix for __toString() serialization
349 * @param string $prefix
352 public function setPrefix($prefix)
354 $this->prefix
= (string) $prefix;
363 public function getPrefix()
365 return $this->prefix
;
369 * Set separator for __toString() serialization
371 * Used to implode elements in container
373 * @param string $separator
376 public function setSeparator($separator)
378 $this->separator
= (string) $separator;
387 public function getSeparator()
389 return $this->separator
;