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

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

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