PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/standard/tags/release-1.5.2/library/Zend/View/Helper/Placeholder/Container/Abstract.php

https://github.com/bhaumik25/zend-framework
PHP | 375 lines | 163 code | 34 blank | 178 comment | 18 complexity | dd74b2865e66dafdf89bc5529e062834 MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @package Zend_View
  16. * @subpackage Helper
  17. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @version $Id$
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. /**
  22. * Abstract class representing container for placeholder values
  23. *
  24. * @package Zend_View
  25. * @subpackage Helpers
  26. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  27. * @license http://framework.zend.com/license/new-bsd New BSD License
  28. */
  29. abstract class Zend_View_Helper_Placeholder_Container_Abstract extends ArrayObject
  30. {
  31. /**
  32. * Whether or not to override all contents of placeholder
  33. * @const string
  34. */
  35. const SET = 'SET';
  36. /**
  37. * Whether or not to append contents to placeholder
  38. * @const string
  39. */
  40. const APPEND = 'APPEND';
  41. /**
  42. * Whether or not to prepend contents to placeholder
  43. * @const string
  44. */
  45. const PREPEND = 'PREPEND';
  46. /**
  47. * What text to prefix the placeholder with when rendering
  48. * @var string
  49. */
  50. protected $_prefix = '';
  51. /**
  52. * What text to append the placeholder with when rendering
  53. * @var string
  54. */
  55. protected $_postfix = '';
  56. /**
  57. * What string to use between individual items in the placeholder when rendering
  58. * @var string
  59. */
  60. protected $_separator = '';
  61. /**
  62. * What string to use as the indentation of output, this will typically be spaces. Eg: ' '
  63. * @var string
  64. */
  65. protected $_indent = '';
  66. /**
  67. * Whether or not we're already capturing for this given container
  68. * @var bool
  69. */
  70. protected $_captureLock = false;
  71. /**
  72. * What type of capture (overwrite (set), append, prepend) to use
  73. * @var string
  74. */
  75. protected $_captureType;
  76. /**
  77. * Key to which to capture content
  78. * @var string
  79. */
  80. protected $_captureKey;
  81. /**
  82. * Constructor - This is needed so that we can attach a class member as the ArrayObject container
  83. *
  84. * @return void
  85. */
  86. public function __construct()
  87. {
  88. parent::__construct(array(), parent::ARRAY_AS_PROPS);
  89. }
  90. /**
  91. * Set a single value
  92. *
  93. * @param mixed $value
  94. * @return void
  95. */
  96. public function set($value)
  97. {
  98. $this->exchangeArray(array($value));
  99. }
  100. /**
  101. * Prepend a value to the top of the container
  102. *
  103. * @param mixed $value
  104. * @return void
  105. */
  106. public function prepend($value)
  107. {
  108. $values = $this->getArrayCopy();
  109. array_unshift($values, $value);
  110. $this->exchangeArray($values);
  111. }
  112. /**
  113. * Retrieve container value
  114. *
  115. * If single element registered, returns that element; otherwise,
  116. * serializes to array.
  117. *
  118. * @return mixed
  119. */
  120. public function getValue()
  121. {
  122. if (1 == count($this)) {
  123. $keys = $this->getKeys();
  124. $key = array_shift($keys);
  125. return $this[$key];
  126. }
  127. return $this->getArrayCopy();
  128. }
  129. /**
  130. * Set prefix for __toString() serialization
  131. *
  132. * @param string $prefix
  133. * @return Zend_View_Helper_Placeholder_Container
  134. */
  135. public function setPrefix($prefix)
  136. {
  137. $this->_prefix = (string) $prefix;
  138. return $this;
  139. }
  140. /**
  141. * Retrieve prefix
  142. *
  143. * @return string
  144. */
  145. public function getPrefix()
  146. {
  147. return $this->_prefix;
  148. }
  149. /**
  150. * Set postfix for __toString() serialization
  151. *
  152. * @param string $postfix
  153. * @return Zend_View_Helper_Placeholder_Container
  154. */
  155. public function setPostfix($postfix)
  156. {
  157. $this->_postfix = (string) $postfix;
  158. return $this;
  159. }
  160. /**
  161. * Retrieve postfix
  162. *
  163. * @return string
  164. */
  165. public function getPostfix()
  166. {
  167. return $this->_postfix;
  168. }
  169. /**
  170. * Set separator for __toString() serialization
  171. *
  172. * Used to implode elements in container
  173. *
  174. * @param string $separator
  175. * @return Zend_View_Helper_Placeholder_Container
  176. */
  177. public function setSeparator($separator)
  178. {
  179. $this->_separator = (string) $separator;
  180. return $this;
  181. }
  182. /**
  183. * Retrieve separator
  184. *
  185. * @return string
  186. */
  187. public function getSeparator()
  188. {
  189. return $this->_separator;
  190. }
  191. /**
  192. * Set the indentation string for __toString() serialization,
  193. * optionally, if a number is passed, it will be the number of spaces
  194. *
  195. * @param string|int $indent
  196. * @return Zend_View_Helper_Placeholder_Container_Abstract
  197. */
  198. public function setIndent($indent)
  199. {
  200. $this->_indent = $this->getWhitespace($indent);
  201. return $this;
  202. }
  203. /**
  204. * Retrieve indentation
  205. *
  206. * @return string
  207. */
  208. public function getIndent()
  209. {
  210. return $this->_indent;
  211. }
  212. /**
  213. * Retrieve whitespace representation of $indent
  214. *
  215. * @param int|string $indent
  216. * @return string
  217. */
  218. public function getWhitespace($indent)
  219. {
  220. if (is_int($indent)) {
  221. $indent = str_repeat(' ', $indent);
  222. }
  223. return (string) $indent;
  224. }
  225. /**
  226. * Start capturing content to push into placeholder
  227. *
  228. * @param int $type How to capture content into placeholder; append, prepend, or set
  229. * @return void
  230. * @throws Zend_View_Helper_Placeholder_Exception if nested captures detected
  231. */
  232. public function captureStart($type = Zend_View_Helper_Placeholder_Container_Abstract::APPEND, $key = null)
  233. {
  234. if ($this->_captureLock) {
  235. require_once 'Zend/View/Helper/Placeholder/Container/Exception.php';
  236. throw new Zend_View_Helper_Placeholder_Container_Exception('Cannot nest placeholder captures for the same placeholder');
  237. }
  238. $this->_captureLock = true;
  239. $this->_captureType = $type;
  240. if ((null !== $key) && is_scalar($key)) {
  241. $this->_captureKey = (string) $key;
  242. }
  243. ob_start();
  244. }
  245. /**
  246. * End content capture
  247. *
  248. * @return void
  249. */
  250. public function captureEnd()
  251. {
  252. $data = ob_get_clean();
  253. $key = null;
  254. $this->_captureLock = false;
  255. if (null !== $this->_captureKey) {
  256. $key = $this->_captureKey;
  257. }
  258. switch ($this->_captureType) {
  259. case self::SET:
  260. if (null !== $key) {
  261. $this[$key] = $data;
  262. } else {
  263. $this->exchangeArray(array($data));
  264. }
  265. break;
  266. case self::PREPEND:
  267. if (null !== $key) {
  268. $array = array($key => $data);
  269. $values = $this->getArrayCopy();
  270. $final = $array + $values;
  271. $this->exchangeArray($final);
  272. } else {
  273. $this->prepend($data);
  274. }
  275. break;
  276. case self::APPEND:
  277. default:
  278. if (null !== $key) {
  279. if (empty($this[$key])) {
  280. $this[$key] = $data;
  281. } else {
  282. $this[$key] .= $data;
  283. }
  284. } else {
  285. $this[$this->nextIndex()] = $data;
  286. }
  287. break;
  288. }
  289. }
  290. /**
  291. * Get keys
  292. *
  293. * @return array
  294. */
  295. public function getKeys()
  296. {
  297. $array = $this->getArrayCopy();
  298. return array_keys($array);
  299. }
  300. /**
  301. * Next Index
  302. *
  303. * as defined by the PHP manual
  304. * @return int
  305. */
  306. public function nextIndex()
  307. {
  308. $keys = $this->getKeys();
  309. if (0 == count($keys)) {
  310. return 0;
  311. }
  312. return $nextIndex = max($keys) + 1;
  313. }
  314. /**
  315. * Render the placeholder
  316. *
  317. * @return string
  318. */
  319. public function toString($indent = null)
  320. {
  321. $indent = ($indent !== null)
  322. ? $this->getWhitespace($indent)
  323. : $this->getIndent();
  324. $items = $this->getArrayCopy();
  325. $return = $indent
  326. . $this->getPrefix()
  327. . implode($this->getSeparator(), $items)
  328. . $this->getPostfix();
  329. $return = preg_replace("/(\r\n?|\n)/", '$1' . $indent, $return);
  330. return $return;
  331. }
  332. /**
  333. * Serialize object to string
  334. *
  335. * @return string
  336. */
  337. public function __toString()
  338. {
  339. return $this->toString();
  340. }
  341. }