2
0

Standalone.php 7.4 KB

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