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

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