PageRenderTime 50ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Zend/View/Helper/Placeholder/Container/Abstract.php

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