File indexing completed on 2025-03-02 05:29:58
0001 <?php 0002 /** 0003 * Zend Framework 0004 * 0005 * LICENSE 0006 * 0007 * This source file is subject to the new BSD license that is bundled 0008 * with this package in the file LICENSE.txt. 0009 * It is also available through the world-wide-web at this URL: 0010 * http://framework.zend.com/license/new-bsd 0011 * If you did not receive a copy of the license and are unable to 0012 * obtain it through the world-wide-web, please send an email 0013 * to license@zend.com so we can send you a copy immediately. 0014 * 0015 * @category Zend 0016 * @package Zend_View 0017 * @subpackage Helper 0018 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0019 * @version $Id$ 0020 * @license http://framework.zend.com/license/new-bsd New BSD License 0021 */ 0022 0023 /** 0024 * Abstract class representing container for placeholder values 0025 * 0026 * @package Zend_View 0027 * @subpackage Helper 0028 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0029 * @license http://framework.zend.com/license/new-bsd New BSD License 0030 */ 0031 abstract class Zend_View_Helper_Placeholder_Container_Abstract extends ArrayObject 0032 { 0033 /** 0034 * Whether or not to override all contents of placeholder 0035 * @const string 0036 */ 0037 const SET = 'SET'; 0038 0039 /** 0040 * Whether or not to append contents to placeholder 0041 * @const string 0042 */ 0043 const APPEND = 'APPEND'; 0044 0045 /** 0046 * Whether or not to prepend contents to placeholder 0047 * @const string 0048 */ 0049 const PREPEND = 'PREPEND'; 0050 0051 /** 0052 * What text to prefix the placeholder with when rendering 0053 * @var string 0054 */ 0055 protected $_prefix = ''; 0056 0057 /** 0058 * What text to append the placeholder with when rendering 0059 * @var string 0060 */ 0061 protected $_postfix = ''; 0062 0063 /** 0064 * What string to use between individual items in the placeholder when rendering 0065 * @var string 0066 */ 0067 protected $_separator = ''; 0068 0069 /** 0070 * What string to use as the indentation of output, this will typically be spaces. Eg: ' ' 0071 * @var string 0072 */ 0073 protected $_indent = ''; 0074 0075 /** 0076 * Whether or not we're already capturing for this given container 0077 * @var bool 0078 */ 0079 protected $_captureLock = false; 0080 0081 /** 0082 * What type of capture (overwrite (set), append, prepend) to use 0083 * @var string 0084 */ 0085 protected $_captureType; 0086 0087 /** 0088 * Key to which to capture content 0089 * @var string 0090 */ 0091 protected $_captureKey; 0092 0093 /** 0094 * Constructor - This is needed so that we can attach a class member as the ArrayObject container 0095 * 0096 * @return \Zend_View_Helper_Placeholder_Container_Abstract 0097 */ 0098 public function __construct() 0099 { 0100 parent::__construct(array(), parent::ARRAY_AS_PROPS); 0101 } 0102 0103 /** 0104 * Set a single value 0105 * 0106 * @param mixed $value 0107 * @return void 0108 */ 0109 public function set($value) 0110 { 0111 $this->exchangeArray(array($value)); 0112 } 0113 0114 /** 0115 * Prepend a value to the top of the container 0116 * 0117 * @param mixed $value 0118 * @return void 0119 */ 0120 public function prepend($value) 0121 { 0122 $values = $this->getArrayCopy(); 0123 array_unshift($values, $value); 0124 $this->exchangeArray($values); 0125 } 0126 0127 /** 0128 * Retrieve container value 0129 * 0130 * If single element registered, returns that element; otherwise, 0131 * serializes to array. 0132 * 0133 * @return mixed 0134 */ 0135 public function getValue() 0136 { 0137 if (1 == count($this)) { 0138 $keys = $this->getKeys(); 0139 $key = array_shift($keys); 0140 return $this[$key]; 0141 } 0142 0143 return $this->getArrayCopy(); 0144 } 0145 0146 /** 0147 * Set prefix for __toString() serialization 0148 * 0149 * @param string $prefix 0150 * @return Zend_View_Helper_Placeholder_Container 0151 */ 0152 public function setPrefix($prefix) 0153 { 0154 $this->_prefix = (string) $prefix; 0155 return $this; 0156 } 0157 0158 /** 0159 * Retrieve prefix 0160 * 0161 * @return string 0162 */ 0163 public function getPrefix() 0164 { 0165 return $this->_prefix; 0166 } 0167 0168 /** 0169 * Set postfix for __toString() serialization 0170 * 0171 * @param string $postfix 0172 * @return Zend_View_Helper_Placeholder_Container 0173 */ 0174 public function setPostfix($postfix) 0175 { 0176 $this->_postfix = (string) $postfix; 0177 return $this; 0178 } 0179 0180 /** 0181 * Retrieve postfix 0182 * 0183 * @return string 0184 */ 0185 public function getPostfix() 0186 { 0187 return $this->_postfix; 0188 } 0189 0190 /** 0191 * Set separator for __toString() serialization 0192 * 0193 * Used to implode elements in container 0194 * 0195 * @param string $separator 0196 * @return Zend_View_Helper_Placeholder_Container 0197 */ 0198 public function setSeparator($separator) 0199 { 0200 $this->_separator = (string) $separator; 0201 return $this; 0202 } 0203 0204 /** 0205 * Retrieve separator 0206 * 0207 * @return string 0208 */ 0209 public function getSeparator() 0210 { 0211 return $this->_separator; 0212 } 0213 0214 /** 0215 * Set the indentation string for __toString() serialization, 0216 * optionally, if a number is passed, it will be the number of spaces 0217 * 0218 * @param string|int $indent 0219 * @return Zend_View_Helper_Placeholder_Container_Abstract 0220 */ 0221 public function setIndent($indent) 0222 { 0223 $this->_indent = $this->getWhitespace($indent); 0224 return $this; 0225 } 0226 0227 /** 0228 * Retrieve indentation 0229 * 0230 * @return string 0231 */ 0232 public function getIndent() 0233 { 0234 return $this->_indent; 0235 } 0236 0237 /** 0238 * Retrieve whitespace representation of $indent 0239 * 0240 * @param int|string $indent 0241 * @return string 0242 */ 0243 public function getWhitespace($indent) 0244 { 0245 if (is_int($indent)) { 0246 $indent = str_repeat(' ', $indent); 0247 } 0248 0249 return (string) $indent; 0250 } 0251 0252 /** 0253 * Start capturing content to push into placeholder 0254 * 0255 * @param int|string $type How to capture content into placeholder; append, prepend, or set 0256 * @param null $key 0257 * @throws Zend_View_Helper_Placeholder_Container_Exception 0258 * @return void 0259 */ 0260 public function captureStart($type = Zend_View_Helper_Placeholder_Container_Abstract::APPEND, $key = null) 0261 { 0262 if ($this->_captureLock) { 0263 // require_once 'Zend/View/Helper/Placeholder/Container/Exception.php'; 0264 $e = new Zend_View_Helper_Placeholder_Container_Exception('Cannot nest placeholder captures for the same placeholder'); 0265 $e->setView($this->view); 0266 throw $e; 0267 } 0268 0269 $this->_captureLock = true; 0270 $this->_captureType = $type; 0271 if ((null !== $key) && is_scalar($key)) { 0272 $this->_captureKey = (string) $key; 0273 } 0274 ob_start(); 0275 } 0276 0277 /** 0278 * End content capture 0279 * 0280 * @return void 0281 */ 0282 public function captureEnd() 0283 { 0284 $data = ob_get_clean(); 0285 $key = null; 0286 $this->_captureLock = false; 0287 if (null !== $this->_captureKey) { 0288 $key = $this->_captureKey; 0289 } 0290 switch ($this->_captureType) { 0291 case self::SET: 0292 if (null !== $key) { 0293 $this[$key] = $data; 0294 } else { 0295 $this->exchangeArray(array($data)); 0296 } 0297 break; 0298 case self::PREPEND: 0299 if (null !== $key) { 0300 $array = array($key => $data); 0301 $values = $this->getArrayCopy(); 0302 $final = $array + $values; 0303 $this->exchangeArray($final); 0304 } else { 0305 $this->prepend($data); 0306 } 0307 break; 0308 case self::APPEND: 0309 default: 0310 if (null !== $key) { 0311 if (empty($this[$key])) { 0312 $this[$key] = $data; 0313 } else { 0314 $this[$key] .= $data; 0315 } 0316 } else { 0317 $this[$this->nextIndex()] = $data; 0318 } 0319 break; 0320 } 0321 } 0322 0323 /** 0324 * Get keys 0325 * 0326 * @return array 0327 */ 0328 public function getKeys() 0329 { 0330 $array = $this->getArrayCopy(); 0331 return array_keys($array); 0332 } 0333 0334 /** 0335 * Next Index 0336 * 0337 * as defined by the PHP manual 0338 * @return int 0339 */ 0340 public function nextIndex() 0341 { 0342 $keys = $this->getKeys(); 0343 if (0 == count($keys)) { 0344 return 0; 0345 } 0346 0347 return $nextIndex = max($keys) + 1; 0348 } 0349 0350 /** 0351 * Render the placeholder 0352 * 0353 * @param null $indent 0354 * @return string 0355 */ 0356 public function toString($indent = null) 0357 { 0358 // Check items 0359 if (0 === $this->count()) { 0360 return ''; 0361 } 0362 0363 $indent = ($indent !== null) 0364 ? $this->getWhitespace($indent) 0365 : $this->getIndent(); 0366 0367 $items = $this->getArrayCopy(); 0368 $return = $indent 0369 . $this->getPrefix() 0370 . implode($this->getSeparator(), $items) 0371 . $this->getPostfix(); 0372 $return = preg_replace("/(\r\n?|\n)/", '$1' . $indent, $return); 0373 return $return; 0374 } 0375 0376 /** 0377 * Serialize object to string 0378 * 0379 * @return string 0380 */ 0381 public function __toString() 0382 { 0383 return $this->toString(); 0384 } 0385 }