00001 <?php 00031 abstract class Zend_View_Helper_Placeholder_Container_Abstract extends ArrayObject 00032 { 00037 const SET = 'SET'; 00038 00043 const APPEND = 'APPEND'; 00044 00049 const PREPEND = 'PREPEND'; 00050 00055 protected $_prefix = ''; 00056 00061 protected $_postfix = ''; 00062 00067 protected $_separator = ''; 00068 00073 protected $_indent = ''; 00074 00079 protected $_captureLock = false; 00080 00085 protected $_captureType; 00086 00091 protected $_captureKey; 00092 00098 public function __construct() 00099 { 00100 parent::__construct(array(), parent::ARRAY_AS_PROPS); 00101 } 00102 00109 public function set($value) 00110 { 00111 $this->exchangeArray(array($value)); 00112 } 00113 00120 public function prepend($value) 00121 { 00122 $values = $this->getArrayCopy(); 00123 array_unshift($values, $value); 00124 $this->exchangeArray($values); 00125 } 00126 00135 public function getValue() 00136 { 00137 if (1 == count($this)) { 00138 $keys = $this->getKeys(); 00139 $key = array_shift($keys); 00140 return $this[$key]; 00141 } 00142 00143 return $this->getArrayCopy(); 00144 } 00145 00152 public function setPrefix($prefix) 00153 { 00154 $this->_prefix = (string) $prefix; 00155 return $this; 00156 } 00157 00163 public function getPrefix() 00164 { 00165 return $this->_prefix; 00166 } 00167 00174 public function setPostfix($postfix) 00175 { 00176 $this->_postfix = (string) $postfix; 00177 return $this; 00178 } 00179 00185 public function getPostfix() 00186 { 00187 return $this->_postfix; 00188 } 00189 00198 public function setSeparator($separator) 00199 { 00200 $this->_separator = (string) $separator; 00201 return $this; 00202 } 00203 00209 public function getSeparator() 00210 { 00211 return $this->_separator; 00212 } 00213 00221 public function setIndent($indent) 00222 { 00223 $this->_indent = $this->getWhitespace($indent); 00224 return $this; 00225 } 00226 00232 public function getIndent() 00233 { 00234 return $this->_indent; 00235 } 00236 00243 public function getWhitespace($indent) 00244 { 00245 if (is_int($indent)) { 00246 $indent = str_repeat(' ', $indent); 00247 } 00248 00249 return (string) $indent; 00250 } 00251 00259 public function captureStart($type = Zend_View_Helper_Placeholder_Container_Abstract::APPEND, $key = null) 00260 { 00261 if ($this->_captureLock) { 00262 require_once 'Zend/View/Helper/Placeholder/Container/Exception.php'; 00263 throw new Zend_View_Helper_Placeholder_Container_Exception('Cannot nest placeholder captures for the same placeholder'); 00264 } 00265 00266 $this->_captureLock = true; 00267 $this->_captureType = $type; 00268 if ((null !== $key) && is_scalar($key)) { 00269 $this->_captureKey = (string) $key; 00270 } 00271 ob_start(); 00272 } 00273 00279 public function captureEnd() 00280 { 00281 $data = ob_get_clean(); 00282 $key = null; 00283 $this->_captureLock = false; 00284 if (null !== $this->_captureKey) { 00285 $key = $this->_captureKey; 00286 } 00287 switch ($this->_captureType) { 00288 case self::SET: 00289 if (null !== $key) { 00290 $this[$key] = $data; 00291 } else { 00292 $this->exchangeArray(array($data)); 00293 } 00294 break; 00295 case self::PREPEND: 00296 if (null !== $key) { 00297 $array = array($key => $data); 00298 $values = $this->getArrayCopy(); 00299 $final = $array + $values; 00300 $this->exchangeArray($final); 00301 } else { 00302 $this->prepend($data); 00303 } 00304 break; 00305 case self::APPEND: 00306 default: 00307 if (null !== $key) { 00308 if (empty($this[$key])) { 00309 $this[$key] = $data; 00310 } else { 00311 $this[$key] .= $data; 00312 } 00313 } else { 00314 $this[$this->nextIndex()] = $data; 00315 } 00316 break; 00317 } 00318 } 00319 00325 public function getKeys() 00326 { 00327 $array = $this->getArrayCopy(); 00328 return array_keys($array); 00329 } 00330 00337 public function nextIndex() 00338 { 00339 $keys = $this->getKeys(); 00340 if (0 == count($keys)) { 00341 return 0; 00342 } 00343 00344 return $nextIndex = max($keys) + 1; 00345 } 00346 00352 public function toString($indent = null) 00353 { 00354 $indent = ($indent !== null) 00355 ? $this->getWhitespace($indent) 00356 : $this->getIndent(); 00357 00358 $items = $this->getArrayCopy(); 00359 $return = $indent 00360 . $this->getPrefix() 00361 . implode($this->getSeparator(), $items) 00362 . $this->getPostfix(); 00363 $return = preg_replace("/(\r\n?|\n)/", '$1' . $indent, $return); 00364 return $return; 00365 } 00366 00372 public function __toString() 00373 { 00374 return $this->toString(); 00375 } 00376 }