Added the zend framework 2 library, the path is specified in line no.26 in zend_modul...
[openemr.git] / interface / modules / zend_modules / library / Zend / View / Helper / Placeholder / Container / AbstractContainer.php
blob24e76882719849dc87fc2b185235992eb3a356ea
1 <?php
2 /**
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
8 */
10 namespace Zend\View\Helper\Placeholder\Container;
12 use ArrayObject;
13 use Zend\View\Exception;
15 /**
16 * Abstract class representing container for placeholder values
18 abstract class AbstractContainer extends ArrayObject
20 /**
21 * Whether or not to override all contents of placeholder
23 * @const string
25 const SET = 'SET';
27 /**
28 * Whether or not to append contents to placeholder
30 * @const string
32 const APPEND = 'APPEND';
34 /**
35 * Whether or not to prepend contents to placeholder
37 * @const string
39 const PREPEND = 'PREPEND';
41 /**
42 * Key to which to capture content
44 * @var string
46 protected $captureKey;
48 /**
49 * Whether or not we're already capturing for this given container
51 * @var bool
53 protected $captureLock = false;
55 /**
56 * What type of capture (overwrite (set), append, prepend) to use
58 * @var string
60 protected $captureType;
62 /**
63 * What string to use as the indentation of output, this will typically be spaces. Eg: ' '
65 * @var string
67 protected $indent = '';
69 /**
70 * What text to append the placeholder with when rendering
72 * @var string
74 protected $postfix = '';
76 /**
77 * What text to prefix the placeholder with when rendering
79 * @var string
81 protected $prefix = '';
83 /**
84 * What string to use between individual items in the placeholder when rendering
86 * @var string
88 protected $separator = '';
90 /**
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);
98 /**
99 * Serialize object to string
101 * @return string
103 public function __toString()
105 return $this->toString();
109 * Render the placeholder
111 * @param null|int|string $indent
112 * @return string
114 public function toString($indent = null)
116 $indent = ($indent !== null)
117 ? $this->getWhitespace($indent)
118 : $this->getIndent();
120 $items = $this->getArrayCopy();
121 $return = $indent
122 . $this->getPrefix()
123 . implode($this->getSeparator(), $items)
124 . $this->getPostfix();
125 $return = preg_replace("/(\r\n?|\n)/", '$1' . $indent, $return);
127 return $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
136 * @return void
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;
151 ob_start();
155 * End content capture
157 * @return void
159 public function captureEnd()
161 $data = ob_get_clean();
162 $key = null;
163 $this->captureLock = false;
164 if (null !== $this->captureKey) {
165 $key = $this->captureKey;
167 switch ($this->captureType) {
168 case self::SET:
169 if (null !== $key) {
170 $this[$key] = $data;
171 } else {
172 $this->exchangeArray(array($data));
174 break;
175 case self::PREPEND:
176 if (null !== $key) {
177 $array = array($key => $data);
178 $values = $this->getArrayCopy();
179 $final = $array + $values;
180 $this->exchangeArray($final);
181 } else {
182 $this->prepend($data);
184 break;
185 case self::APPEND:
186 default:
187 if (null !== $key) {
188 if (empty($this[$key])) {
189 $this[$key] = $data;
190 } else {
191 $this[$key] .= $data;
193 } else {
194 $this[$this->nextIndex()] = $data;
196 break;
201 * Get keys
203 * @return array
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.
218 * @return mixed
220 public function getValue()
222 if (1 == count($this)) {
223 $keys = $this->getKeys();
224 $key = array_shift($keys);
225 return $this[$key];
228 return $this->getArrayCopy();
232 * Retrieve whitespace representation of $indent
234 * @param int|string $indent
235 * @return string
237 public function getWhitespace($indent)
239 if (is_int($indent)) {
240 $indent = str_repeat(' ', $indent);
243 return (string) $indent;
247 * Set a single value
249 * @param mixed $value
250 * @return void
252 public function set($value)
254 $this->exchangeArray(array($value));
256 return $this;
260 * Prepend a value to the top of the container
262 * @param mixed $value
263 * @return self
265 public function prepend($value)
267 $values = $this->getArrayCopy();
268 array_unshift($values, $value);
269 $this->exchangeArray($values);
271 return $this;
275 * Append a value to the end of the container
277 * @param mixed $value
278 * @return self
280 public function append($value)
282 parent::append($value);
283 return $this;
287 * Next Index as defined by the PHP manual
289 * @return int
291 public function nextIndex()
293 $keys = $this->getKeys();
294 if (0 == count($keys)) {
295 return 0;
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
306 * @return self
308 public function setIndent($indent)
310 $this->indent = $this->getWhitespace($indent);
311 return $this;
315 * Retrieve indentation
317 * @return string
319 public function getIndent()
321 return $this->indent;
325 * Set postfix for __toString() serialization
327 * @param string $postfix
328 * @return self
330 public function setPostfix($postfix)
332 $this->postfix = (string) $postfix;
333 return $this;
337 * Retrieve postfix
339 * @return string
341 public function getPostfix()
343 return $this->postfix;
347 * Set prefix for __toString() serialization
349 * @param string $prefix
350 * @return self
352 public function setPrefix($prefix)
354 $this->prefix = (string) $prefix;
355 return $this;
359 * Retrieve prefix
361 * @return string
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
374 * @return self
376 public function setSeparator($separator)
378 $this->separator = (string) $separator;
379 return $this;
383 * Retrieve separator
385 * @return string
387 public function getSeparator()
389 return $this->separator;