Source of file AbstractContainer.php
Size: 8,721 Bytes - Last Modified: 2014-03-12T23:21:18+01:00
/home/theseer/Downloads/ZendFramework-2.3.0/library/Zend/View/Helper/Placeholder/Container/AbstractContainer.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392 | <?php /** * Zend Framework (http://framework.zend.com/) * * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ namespace Zend\View\Helper\Placeholder\Container; use ArrayObject; use Zend\View\Exception; /** * Abstract class representing container for placeholder values */ abstract class AbstractContainer extends ArrayObject { /** * Whether or not to override all contents of placeholder * * @const string */ const SET = 'SET'; /** * Whether or not to append contents to placeholder * * @const string */ const APPEND = 'APPEND'; /** * Whether or not to prepend contents to placeholder * * @const string */ const PREPEND = 'PREPEND'; /** * Key to which to capture content * * @var string */ protected $captureKey; /** * Whether or not we're already capturing for this given container * * @var bool */ protected $captureLock = false; /** * What type of capture (overwrite (set), append, prepend) to use * * @var string */ protected $captureType; /** * What string to use as the indentation of output, this will typically be spaces. Eg: ' ' * * @var string */ protected $indent = ''; /** * What text to append the placeholder with when rendering * * @var string */ protected $postfix = ''; /** * What text to prefix the placeholder with when rendering * * @var string */ protected $prefix = ''; /** * What string to use between individual items in the placeholder when rendering * * @var string */ protected $separator = ''; /** * Constructor - This is needed so that we can attach a class member as the ArrayObject container */ public function __construct() { parent::__construct(array(), parent::ARRAY_AS_PROPS); } /** * Serialize object to string * * @return string */ public function __toString() { return $this->toString(); } /** * Render the placeholder * * @param null|int|string $indent * @return string */ public function toString($indent = null) { $indent = ($indent !== null) ? $this->getWhitespace($indent) : $this->getIndent(); $items = $this->getArrayCopy(); $return = $indent . $this->getPrefix() . implode($this->getSeparator(), $items) . $this->getPostfix(); $return = preg_replace("/(\r\n?|\n)/", '$1' . $indent, $return); return $return; } /** * Start capturing content to push into placeholder * * @param string $type How to capture content into placeholder; append, prepend, or set * @param mixed $key Key to which to capture content * @throws Exception\RuntimeException if nested captures detected * @return void */ public function captureStart($type = AbstractContainer::APPEND, $key = null) { if ($this->captureLock) { throw new Exception\RuntimeException( 'Cannot nest placeholder captures for the same placeholder' ); } $this->captureLock = true; $this->captureType = $type; if ((null !== $key) && is_scalar($key)) { $this->captureKey = (string) $key; } ob_start(); } /** * End content capture * * @return void */ public function captureEnd() { $data = ob_get_clean(); $key = null; $this->captureLock = false; if (null !== $this->captureKey) { $key = $this->captureKey; } switch ($this->captureType) { case self::SET: if (null !== $key) { $this[$key] = $data; } else { $this->exchangeArray(array($data)); } break; case self::PREPEND: if (null !== $key) { $array = array($key => $data); $values = $this->getArrayCopy(); $final = $array + $values; $this->exchangeArray($final); } else { $this->prepend($data); } break; case self::APPEND: default: if (null !== $key) { if (empty($this[$key])) { $this[$key] = $data; } else { $this[$key] .= $data; } } else { $this[$this->nextIndex()] = $data; } break; } } /** * Get keys * * @return array */ public function getKeys() { $array = $this->getArrayCopy(); return array_keys($array); } /** * Retrieve container value * * If single element registered, returns that element; otherwise, * serializes to array. * * @return mixed */ public function getValue() { if (1 == count($this)) { $keys = $this->getKeys(); $key = array_shift($keys); return $this[$key]; } return $this->getArrayCopy(); } /** * Retrieve whitespace representation of $indent * * @param int|string $indent * @return string */ public function getWhitespace($indent) { if (is_int($indent)) { $indent = str_repeat(' ', $indent); } return (string) $indent; } /** * Set a single value * * @param mixed $value * @return void */ public function set($value) { $this->exchangeArray(array($value)); return $this; } /** * Prepend a value to the top of the container * * @param mixed $value * @return self */ public function prepend($value) { $values = $this->getArrayCopy(); array_unshift($values, $value); $this->exchangeArray($values); return $this; } /** * Append a value to the end of the container * * @param mixed $value * @return self */ public function append($value) { parent::append($value); return $this; } /** * Next Index as defined by the PHP manual * * @return int */ public function nextIndex() { $keys = $this->getKeys(); if (0 == count($keys)) { return 0; } return $nextIndex = max($keys) + 1; } /** * Set the indentation string for __toString() serialization, * optionally, if a number is passed, it will be the number of spaces * * @param string|int $indent * @return self */ public function setIndent($indent) { $this->indent = $this->getWhitespace($indent); return $this; } /** * Retrieve indentation * * @return string */ public function getIndent() { return $this->indent; } /** * Set postfix for __toString() serialization * * @param string $postfix * @return self */ public function setPostfix($postfix) { $this->postfix = (string) $postfix; return $this; } /** * Retrieve postfix * * @return string */ public function getPostfix() { return $this->postfix; } /** * Set prefix for __toString() serialization * * @param string $prefix * @return self */ public function setPrefix($prefix) { $this->prefix = (string) $prefix; return $this; } /** * Retrieve prefix * * @return string */ public function getPrefix() { return $this->prefix; } /** * Set separator for __toString() serialization * * Used to implode elements in container * * @param string $separator * @return self */ public function setSeparator($separator) { $this->separator = (string) $separator; return $this; } /** * Retrieve separator * * @return string */ public function getSeparator() { return $this->separator; } } |