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

https://github.com/Exercise/zf2 · PHP · 331 lines · 126 code · 29 blank · 176 comment · 6 complexity · fc473e9a884618ec102d0bd5608ae49c 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-2010 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @version $Id$
  20. * @license http://framework.zend.com/license/new-bsd New BSD License
  21. */
  22. /**
  23. * @namespace
  24. */
  25. namespace Zend\View\Helper\Placeholder\Container;
  26. use Zend\View\Helper\Placeholder\Registry;
  27. /**
  28. * Base class for targetted placeholder helpers
  29. *
  30. * @uses ArrayAccess
  31. * @uses Countable
  32. * @uses IteratorAggregate
  33. * @uses \Zend\View\Exception
  34. * @uses \Zend\View\Helper\AbstractHelper
  35. * @uses \Zend\View\Helper\Placeholder\Registry
  36. * @package Zend_View
  37. * @subpackage Helper
  38. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  39. * @license http://framework.zend.com/license/new-bsd New BSD License
  40. */
  41. abstract class Standalone
  42. extends \Zend\View\Helper\AbstractHelper
  43. implements \IteratorAggregate, \Countable, \ArrayAccess
  44. {
  45. /**
  46. * @var \Zend\View\Helper\Placeholder\Container\AbstractContainer
  47. */
  48. protected $_container;
  49. /**
  50. * @var \Zend\View\Helper\Placeholder\Registry
  51. */
  52. protected $_registry;
  53. /**
  54. * Registry key under which container registers itself
  55. * @var string
  56. */
  57. protected $_regKey;
  58. /**
  59. * Flag wheter to automatically escape output, must also be
  60. * enforced in the child class if __toString/toString is overriden
  61. * @var book
  62. */
  63. protected $_autoEscape = true;
  64. /**
  65. * Constructor
  66. *
  67. * @return void
  68. */
  69. public function __construct()
  70. {
  71. $this->setRegistry(Registry::getRegistry());
  72. $this->setContainer($this->getRegistry()->getContainer($this->_regKey));
  73. }
  74. /**
  75. * Retrieve registry
  76. *
  77. * @return \Zend\View\Helper\Placeholder\Registry
  78. */
  79. public function getRegistry()
  80. {
  81. return $this->_registry;
  82. }
  83. /**
  84. * Set registry object
  85. *
  86. * @param \Zend\View\Helper\Placeholder\Registry $registry
  87. * @return \Zend\View\Helper\Placeholder\Container\Standalone
  88. */
  89. public function setRegistry(Registry $registry)
  90. {
  91. $this->_registry = $registry;
  92. return $this;
  93. }
  94. /**
  95. * Set whether or not auto escaping should be used
  96. *
  97. * @param bool $autoEscape whether or not to auto escape output
  98. * @return \Zend\View\Helper\Placeholder\Container\Standalone
  99. */
  100. public function setAutoEscape($autoEscape = true)
  101. {
  102. $this->_autoEscape = ($autoEscape) ? true : false;
  103. return $this;
  104. }
  105. /**
  106. * Return whether autoEscaping is enabled or disabled
  107. *
  108. * return bool
  109. */
  110. public function getAutoEscape()
  111. {
  112. return $this->_autoEscape;
  113. }
  114. /**
  115. * Escape a string
  116. *
  117. * @param string $string
  118. * @return string
  119. */
  120. protected function _escape($string)
  121. {
  122. $enc = 'UTF-8';
  123. if ($this->view instanceof \Zend\View\ViewEngine
  124. && method_exists($this->view, 'getEncoding')
  125. ) {
  126. $enc = $this->view->getEncoding();
  127. }
  128. return htmlspecialchars((string) $string, ENT_COMPAT, $enc);
  129. }
  130. /**
  131. * Set container on which to operate
  132. *
  133. * @param \Zend\View\Helper\Placeholder\Container\AbstractContainer $container
  134. * @return \Zend\View\Helper\Placeholder\Container\Standalone
  135. */
  136. public function setContainer(AbstractContainer $container)
  137. {
  138. $this->_container = $container;
  139. return $this;
  140. }
  141. /**
  142. * Retrieve placeholder container
  143. *
  144. * @return \Zend\View\Helper\Placeholder\Container\AbstractContainer
  145. */
  146. public function getContainer()
  147. {
  148. return $this->_container;
  149. }
  150. /**
  151. * Overloading: set property value
  152. *
  153. * @param string $key
  154. * @param mixed $value
  155. * @return void
  156. */
  157. public function __set($key, $value)
  158. {
  159. $container = $this->getContainer();
  160. $container[$key] = $value;
  161. }
  162. /**
  163. * Overloading: retrieve property
  164. *
  165. * @param string $key
  166. * @return mixed
  167. */
  168. public function __get($key)
  169. {
  170. $container = $this->getContainer();
  171. if (isset($container[$key])) {
  172. return $container[$key];
  173. }
  174. return null;
  175. }
  176. /**
  177. * Overloading: check if property is set
  178. *
  179. * @param string $key
  180. * @return bool
  181. */
  182. public function __isset($key)
  183. {
  184. $container = $this->getContainer();
  185. return isset($container[$key]);
  186. }
  187. /**
  188. * Overloading: unset property
  189. *
  190. * @param string $key
  191. * @return void
  192. */
  193. public function __unset($key)
  194. {
  195. $container = $this->getContainer();
  196. if (isset($container[$key])) {
  197. unset($container[$key]);
  198. }
  199. }
  200. /**
  201. * Overload
  202. *
  203. * Proxy to container methods
  204. *
  205. * @param string $method
  206. * @param array $args
  207. * @return mixed
  208. */
  209. public function __call($method, $args)
  210. {
  211. $container = $this->getContainer();
  212. if (method_exists($container, $method)) {
  213. $return = call_user_func_array(array($container, $method), $args);
  214. if ($return === $container) {
  215. // If the container is returned, we really want the current object
  216. return $this;
  217. }
  218. return $return;
  219. }
  220. $e = new \Zend\View\Exception('Method "' . $method . '" does not exist');
  221. $e->setView($this->view);
  222. throw $e;
  223. }
  224. /**
  225. * String representation
  226. *
  227. * @return string
  228. */
  229. public function toString()
  230. {
  231. return $this->getContainer()->toString();
  232. }
  233. /**
  234. * Cast to string representation
  235. *
  236. * @return string
  237. */
  238. public function __toString()
  239. {
  240. return $this->toString();
  241. }
  242. /**
  243. * Countable
  244. *
  245. * @return int
  246. */
  247. public function count()
  248. {
  249. $container = $this->getContainer();
  250. return count($container);
  251. }
  252. /**
  253. * ArrayAccess: offsetExists
  254. *
  255. * @param string|int $offset
  256. * @return bool
  257. */
  258. public function offsetExists($offset)
  259. {
  260. return $this->getContainer()->offsetExists($offset);
  261. }
  262. /**
  263. * ArrayAccess: offsetGet
  264. *
  265. * @param string|int $offset
  266. * @return mixed
  267. */
  268. public function offsetGet($offset)
  269. {
  270. return $this->getContainer()->offsetGet($offset);
  271. }
  272. /**
  273. * ArrayAccess: offsetSet
  274. *
  275. * @param string|int $offset
  276. * @param mixed $value
  277. * @return void
  278. */
  279. public function offsetSet($offset, $value)
  280. {
  281. return $this->getContainer()->offsetSet($offset, $value);
  282. }
  283. /**
  284. * ArrayAccess: offsetUnset
  285. *
  286. * @param string|int $offset
  287. * @return void
  288. */
  289. public function offsetUnset($offset)
  290. {
  291. return $this->getContainer()->offsetUnset($offset);
  292. }
  293. /**
  294. * IteratorAggregate: get Iterator
  295. *
  296. * @return Iterator
  297. */
  298. public function getIterator()
  299. {
  300. return $this->getContainer()->getIterator();
  301. }
  302. }