3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\View\Helper\Escaper
;
13 use Zend\View\Exception
;
17 * Helper for escaping values
19 abstract class AbstractHelper
extends Helper\AbstractHelper
22 * @const Recursion constants
24 const RECURSE_NONE
= 0x00;
25 const RECURSE_ARRAY
= 0x01;
26 const RECURSE_OBJECT
= 0x02;
29 * @var string Encoding
31 protected $encoding = 'UTF-8';
34 * @var Escaper\Escaper
36 protected $escaper = null;
39 * Invoke this helper: escape a value
42 * @param int $recurse Expects one of the recursion constants;
43 * used to decide whether or not to recurse the given value when escaping
44 * @throws Exception\InvalidArgumentException
45 * @return mixed Given a scalar, a scalar value is returned. Given an object, with the $recurse flag not
46 * allowing object recursion, returns a string. Otherwise, returns an array.
48 public function __invoke($value, $recurse = self
::RECURSE_NONE
)
50 if (is_string($value)) {
51 return $this->escape($value);
54 if (is_array($value)) {
55 if (!(self
::RECURSE_ARRAY
& $recurse)) {
56 throw new Exception\
InvalidArgumentException(
57 'Array provided to Escape helper, but flags do not allow recursion'
60 foreach ($value as $k => $v) {
61 $value[$k] = $this->__invoke($v, $recurse);
66 if (is_object($value)) {
67 if (!(self
::RECURSE_OBJECT
& $recurse)) {
68 // Attempt to cast it to a string
69 if (method_exists($value, '__toString')) {
70 return $this->escape((string) $value);
72 throw new Exception\
InvalidArgumentException(
73 'Object provided to Escape helper, but flags do not allow recursion'
76 if (method_exists($value, 'toArray')) {
77 return $this->__invoke($value->toArray(), $recurse | self
::RECURSE_ARRAY
);
79 return $this->__invoke((array) $value, $recurse | self
::RECURSE_ARRAY
);
86 * Escape a value for current escaping strategy
88 * @param string $value
91 abstract protected function escape($value);
94 * Set the encoding to use for escape operations
96 * @param string $encoding
97 * @throws Exception\InvalidArgumentException
98 * @return AbstractHelper
100 public function setEncoding($encoding)
102 if (null !== $this->escaper
) {
103 throw new Exception\
InvalidArgumentException(
104 'Character encoding settings cannot be changed once the Helper has been used or '
105 . ' if a Zend\Escaper\Escaper object (with preset encoding option) is set.'
109 $this->encoding
= $encoding;
115 * Get the encoding to use for escape operations
119 public function getEncoding()
121 return $this->encoding
;
125 * Set instance of Escaper
127 * @param Escaper\Escaper $escaper
128 * @return AbstractHelper
130 public function setEscaper(Escaper\Escaper
$escaper)
132 $this->escaper
= $escaper;
133 $this->encoding
= $escaper->getEncoding();
139 * Get instance of Escaper
141 * @return null|Escaper\Escaper
143 public function getEscaper()
145 if (null === $this->escaper
) {
146 $this->setEscaper(new Escaper\
Escaper($this->getEncoding()));
149 return $this->escaper
;