PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/ksekar/campus
PHP | 378 lines | 165 code | 34 blank | 179 comment | 18 complexity | f29501019779874912f3dd423e41ffb1 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.0, MIT
  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-2012 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @version $Id: Abstract.php 24594 2012-01-05 21:27:01Z matthew $
  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-2012 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. $e = new Zend_View_Helper_Placeholder_Container_Exception('Cannot nest placeholder captures for the same placeholder');
  238. $e->setView($this->view);
  239. throw $e;
  240. }
  241. $this->_captureLock = true;
  242. $this->_captureType = $type;
  243. if ((null !== $key) && is_scalar($key)) {
  244. $this->_captureKey = (string) $key;
  245. }
  246. ob_start();
  247. }
  248. /**
  249. * End content capture
  250. *
  251. * @return void
  252. */
  253. public function captureEnd()
  254. {
  255. $data = ob_get_clean();
  256. $key = null;
  257. $this->_captureLock = false;
  258. if (null !== $this->_captureKey) {
  259. $key = $this->_captureKey;
  260. }
  261. switch ($this->_captureType) {
  262. case self::SET:
  263. if (null !== $key) {
  264. $this[$key] = $data;
  265. } else {
  266. $this->exchangeArray(array($data));
  267. }
  268. break;
  269. case self::PREPEND:
  270. if (null !== $key) {
  271. $array = array($key => $data);
  272. $values = $this->getArrayCopy();
  273. $final = $array + $values;
  274. $this->exchangeArray($final);
  275. } else {
  276. $this->prepend($data);
  277. }
  278. break;
  279. case self::APPEND:
  280. default:
  281. if (null !== $key) {
  282. if (empty($this[$key])) {
  283. $this[$key] = $data;
  284. } else {
  285. $this[$key] .= $data;
  286. }
  287. } else {
  288. $this[$this->nextIndex()] = $data;
  289. }
  290. break;
  291. }
  292. }
  293. /**
  294. * Get keys
  295. *
  296. * @return array
  297. */
  298. public function getKeys()
  299. {
  300. $array = $this->getArrayCopy();
  301. return array_keys($array);
  302. }
  303. /**
  304. * Next Index
  305. *
  306. * as defined by the PHP manual
  307. * @return int
  308. */
  309. public function nextIndex()
  310. {
  311. $keys = $this->getKeys();
  312. if (0 == count($keys)) {
  313. return 0;
  314. }
  315. return $nextIndex = max($keys) + 1;
  316. }
  317. /**
  318. * Render the placeholder
  319. *
  320. * @return string
  321. */
  322. public function toString($indent = null)
  323. {
  324. $indent = ($indent !== null)
  325. ? $this->getWhitespace($indent)
  326. : $this->getIndent();
  327. $items = $this->getArrayCopy();
  328. $return = $indent
  329. . $this->getPrefix()
  330. . implode($this->getSeparator(), $items)
  331. . $this->getPostfix();
  332. $return = preg_replace("/(\r\n?|\n)/", '$1' . $indent, $return);
  333. return $return;
  334. }
  335. /**
  336. * Serialize object to string
  337. *
  338. * @return string
  339. */
  340. public function __toString()
  341. {
  342. return $this->toString();
  343. }
  344. }