PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/library/Zend/View/Variables.php

https://github.com/mrbanzai/zf2
PHP | 337 lines | 149 code | 26 blank | 162 comment | 12 complexity | 8be558ad919eb201f5cb915f7d9a9838 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. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. */
  20. /**
  21. * @namespace
  22. */
  23. namespace Zend\View;
  24. use ArrayObject;
  25. /**
  26. * Abstract class for Zend_View to help enforce private constructs.
  27. *
  28. * @todo Allow specifying string names for broker, filter chain, variables
  29. * @todo Move escaping into variables object
  30. * @todo Move strict variables into variables object
  31. * @category Zend
  32. * @package Zend_View
  33. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  34. * @license http://framework.zend.com/license/new-bsd New BSD License
  35. */
  36. class Variables extends ArrayObject
  37. {
  38. /**
  39. * @var string Default encoding (used for escaping)
  40. */
  41. protected $encoding = 'UTF-8';
  42. /**
  43. * @var callback
  44. */
  45. protected $escapeCallback;
  46. /**
  47. * Raw values
  48. *
  49. * @var array
  50. */
  51. protected $rawValues = array();
  52. /**
  53. * Strict variables flag; when on, undefined variables accessed in the view
  54. * scripts will trigger notices
  55. *
  56. * @var bool
  57. */
  58. protected $strictVars = false;
  59. /**
  60. * Constructor
  61. *
  62. * @param array $variables
  63. * @param array $options
  64. * @return void
  65. */
  66. public function __construct(array $variables = array(), array $options = array())
  67. {
  68. parent::__construct(
  69. array(),
  70. ArrayObject::STD_PROP_LIST|ArrayObject::ARRAY_AS_PROPS,
  71. 'ArrayIterator'
  72. );
  73. // Load each variable into the object using offsetSet() so that they
  74. // are escaped correctly.
  75. foreach ($variables as $key => $value) {
  76. $this->$key = $value;
  77. }
  78. $this->setOptions($options);
  79. }
  80. /**
  81. * Configure object
  82. *
  83. * @param array $options
  84. * @return Variables
  85. */
  86. public function setOptions(array $options)
  87. {
  88. foreach ($options as $key => $value) {
  89. switch (strtolower($key)) {
  90. case 'strict_vars':
  91. $this->setStrictVars($value);
  92. break;
  93. case 'encoding':
  94. $this->setEncoding($value);
  95. break;
  96. case 'escape':
  97. $this->setEscapeCallback($value);
  98. break;
  99. default:
  100. // Unknown options are considered variables
  101. $this[$key] = $value;
  102. break;
  103. }
  104. }
  105. return $this;
  106. }
  107. /**
  108. * Set encoding (for escaping)
  109. *
  110. * @param string $encoding
  111. * @return Variables
  112. */
  113. public function setEncoding($encoding)
  114. {
  115. $this->encoding = (string) $encoding;
  116. return $this;
  117. }
  118. /**
  119. * Retrieve encoding
  120. *
  121. * @return string
  122. */
  123. public function getEncoding()
  124. {
  125. return $this->encoding;
  126. }
  127. /**
  128. * Set status of "strict vars" flag
  129. *
  130. * @param bool $flag
  131. * @return Variables
  132. */
  133. public function setStrictVars($flag)
  134. {
  135. $this->strictVars = (bool) $flag;
  136. return $this;
  137. }
  138. /**
  139. * Are we operating with strict variables?
  140. *
  141. * @return bool
  142. */
  143. public function isStrict()
  144. {
  145. return $this->strictVars;
  146. }
  147. /**
  148. * Set escape callback mechanism
  149. *
  150. * @param callback $spec
  151. * @return Variables
  152. * @throws Exception\InvalidArgumentException
  153. */
  154. public function setEscapeCallback($spec)
  155. {
  156. if (!is_callable($spec)) {
  157. throw new Exception\InvalidArgumentException('Escape callback must be callable');
  158. }
  159. $this->escapeCallback = $spec;
  160. }
  161. /**
  162. * Get callback used for escaping variables
  163. *
  164. * @return callback
  165. */
  166. public function getEscapeCallback()
  167. {
  168. if (null === $this->escapeCallback) {
  169. $view = $this;
  170. $this->setEscapeCallback(function($value) use ($view) {
  171. return htmlspecialchars($value, ENT_COMPAT, $view->getEncoding());
  172. });
  173. }
  174. return $this->escapeCallback;
  175. }
  176. /**
  177. * Escape a value
  178. *
  179. * If the value is not a string, it is immediately returned. Otherwise, it
  180. * is passed to the registered escape callback.
  181. *
  182. * @param string $value
  183. * @return string
  184. */
  185. public function escape($value)
  186. {
  187. if (!is_string($value)) {
  188. return $value;
  189. }
  190. $escaper = $this->getEscapeCallback();
  191. return call_user_func($escaper, $value);
  192. }
  193. /**
  194. * Assign many values at once
  195. *
  196. * @param array|object $spec
  197. * @return Variables
  198. * @throws Exception\InvalidArgumentException
  199. */
  200. public function assign($spec)
  201. {
  202. if (is_object($spec)) {
  203. if (method_exists($spec, 'toArray')) {
  204. $spec = $spec->toArray();
  205. } else {
  206. $spec = (array) $spec;
  207. }
  208. }
  209. if (!is_array($spec)) {
  210. throw new Exception\InvalidArgumentException(sprintf(
  211. 'assign() expects either an array or an object as an argument; received "%s"',
  212. gettype($spec)
  213. ));
  214. }
  215. foreach ($spec as $key => $value) {
  216. $this[$key] = $value;
  217. }
  218. return $this;
  219. }
  220. /**
  221. * Sets the value of the specified key
  222. *
  223. * If the value is a string, passes it to the escape mechanism before
  224. * storage. The raw value may be obtained by calling getRawValue().
  225. *
  226. * @param mixed $key
  227. * @param mixed $value
  228. * @return void
  229. */
  230. public function offsetSet($key, $value)
  231. {
  232. $this->rawValues[$key] = $value;
  233. return parent::offsetSet($key, $this->escape($value));
  234. }
  235. /**
  236. * Set a value that should never be escaped
  237. *
  238. * @param mixed $key
  239. * @param mixed $value
  240. * @return Variables
  241. */
  242. public function setCleanValue($key, $value)
  243. {
  244. $this->rawValues[$key] = $value;
  245. parent::offsetSet($key, $value);
  246. return $this;
  247. }
  248. /**
  249. * Get the variable value
  250. *
  251. * If the value has not been defined, a null value will be returned; if
  252. * strict vars on in place, a notice will also be raised.
  253. *
  254. * Otherwise, returns _escaped_ version of the value.
  255. *
  256. * @param mixed $key
  257. * @return void
  258. */
  259. public function offsetGet($key)
  260. {
  261. if (!$this->offsetExists($key)) {
  262. if ($this->isStrict()) {
  263. trigger_error(sprintf(
  264. 'View variable "%s" does not exist', $key
  265. ), E_USER_NOTICE);
  266. }
  267. return null;
  268. }
  269. return parent::offsetGet($key);
  270. }
  271. /**
  272. * Get the raw value associated with a variable key
  273. *
  274. * If the value has not been defined, a null value will be returned; if
  275. * strict vars on in place, a notice will also be raised.
  276. *
  277. * @param mixed $key
  278. * @return mixed
  279. */
  280. public function getRawValue($key)
  281. {
  282. if (!$this->offsetExists($key)) {
  283. if ($this->isStrict()) {
  284. trigger_error(sprintf(
  285. 'View variable "%s" does not exist', $key
  286. ), E_USER_NOTICE);
  287. }
  288. return null;
  289. }
  290. return $this->rawValues[$key];
  291. }
  292. /**
  293. * Get all raw values
  294. *
  295. * @return array
  296. */
  297. public function getRawValues()
  298. {
  299. return $this->rawValues;
  300. }
  301. /**
  302. * Clear all variables
  303. *
  304. * @return void
  305. */
  306. public function clear()
  307. {
  308. $this->exchangeArray(array());
  309. $this->rawValues = array();
  310. }
  311. }