PageRenderTime 49ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Zend/View/Helper/Escape.php

http://github.com/zendframework/zf2
PHP | 162 lines | 73 code | 12 blank | 77 comment | 9 complexity | 2c13448ba0ac5eb5b048c91110e955c2 MD5 | raw file
Possible License(s): BSD-3-Clause
  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-2012 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. /**
  22. * @namespace
  23. */
  24. namespace Zend\View\Helper;
  25. use Zend\View\Exception;
  26. /**
  27. * Helper for escaping values
  28. *
  29. * @uses Iterator
  30. * @package Zend_View
  31. * @subpackage Helper
  32. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  33. * @license http://framework.zend.com/license/new-bsd New BSD License
  34. */
  35. class Escape extends AbstractHelper
  36. {
  37. /**@+
  38. * @const Recursion constants
  39. */
  40. const RECURSE_NONE = 0x00;
  41. const RECURSE_ARRAY = 0x01;
  42. const RECURSE_OBJECT = 0x02;
  43. /**@-*/
  44. /**
  45. * @var callback
  46. */
  47. protected $callback;
  48. /**
  49. * @var string Encoding
  50. */
  51. protected $encoding = 'UTF-8';
  52. /**
  53. * Set the encoding to use for escape operations
  54. *
  55. * @param string $encoding
  56. * @return Escape
  57. */
  58. public function setEncoding($encoding)
  59. {
  60. $this->encoding = $encoding;
  61. return $this;
  62. }
  63. /**
  64. * Get the encoding to use for escape operations
  65. *
  66. * @return string
  67. */
  68. public function getEncoding()
  69. {
  70. return $this->encoding;
  71. }
  72. /**
  73. * Set a callback to use for escaping
  74. *
  75. * @param callback $callback
  76. * @return Escape
  77. * @throws Exception\InvalidArgumentException if provided callback is not callable
  78. */
  79. public function setCallback($callback)
  80. {
  81. if (!is_callable($callback)) {
  82. throw new Exception\InvalidArgumentException('Invalid callback provided to ' . get_called_class());
  83. }
  84. $this->callback = $callback;
  85. return $this;
  86. }
  87. /**
  88. * Get the attached callback
  89. *
  90. * If none defined, creates a closure wrapping htmlspecialchars, providing
  91. * the currently set encoding.
  92. *
  93. * @return callback
  94. */
  95. public function getCallback()
  96. {
  97. if (!is_callable($this->callback)) {
  98. $encoding = $this->getEncoding();
  99. $callback = function($value) use ($encoding) {
  100. return htmlspecialchars($value, ENT_COMPAT, $encoding, false);
  101. };
  102. $this->setCallback($callback);
  103. }
  104. return $this->callback;
  105. }
  106. /**
  107. * Invoke this helper: escape a value
  108. *
  109. * @param mixed $value
  110. * @param int $recurse Expects one of the recursion constants; used to decide whether or not to recurse the given value when escaping
  111. * @return mixed Given a scalar, a scalar value is returned. Given an object, with the $recurse flag not allowing object recursion, returns a string. Otherwise, returns an array.
  112. * @throws Exception\InvalidArgumentException
  113. */
  114. public function __invoke($value, $recurse = self::RECURSE_NONE)
  115. {
  116. if (is_string($value)) {
  117. $callback = $this->getCallback();
  118. return call_user_func($callback, $value);
  119. }
  120. if (is_array($value)) {
  121. if (!(self::RECURSE_ARRAY & $recurse)) {
  122. throw new Exception\InvalidArgumentException(
  123. 'Array provided to Escape helper, but flags do not allow recursion'
  124. );
  125. }
  126. foreach ($value as $k => $v) {
  127. $value[$k] = $this->__invoke($v, $recurse);
  128. }
  129. return $value;
  130. }
  131. if (is_object($value)) {
  132. if (!(self::RECURSE_OBJECT & $recurse)) {
  133. // Attempt to cast it to a string
  134. if (method_exists($value, '__toString')) {
  135. $callback = $this->getCallback();
  136. return call_user_func($callback, (string) $value);
  137. }
  138. throw new Exception\InvalidArgumentException(
  139. 'Object provided to Escape helper, but flags do not allow recursion'
  140. );
  141. }
  142. if (method_exists($value, 'toArray')) {
  143. return $this->__invoke($value->toArray(), $recurse | self::RECURSE_ARRAY);
  144. }
  145. return $this->__invoke((array) $value, $recurse | self::RECURSE_ARRAY);
  146. }
  147. // At this point, we have a scalar; simply return it
  148. return $value;
  149. }
  150. }