3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 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; used to decide whether or not to recurse the given value when escaping
43 * @throws Exception\InvalidArgumentException
44 * @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.
46 public function __invoke($value, $recurse = self
::RECURSE_NONE
)
48 if (is_string($value)) {
49 return $this->escape($value);
52 if (is_array($value)) {
53 if (!(self
::RECURSE_ARRAY
& $recurse)) {
54 throw new Exception\
InvalidArgumentException(
55 'Array provided to Escape helper, but flags do not allow recursion'
58 foreach ($value as $k => $v) {
59 $value[$k] = $this->__invoke($v, $recurse);
64 if (is_object($value)) {
65 if (!(self
::RECURSE_OBJECT
& $recurse)) {
66 // Attempt to cast it to a string
67 if (method_exists($value, '__toString')) {
68 return $this->escape((string) $value);
70 throw new Exception\
InvalidArgumentException(
71 'Object provided to Escape helper, but flags do not allow recursion'
74 if (method_exists($value, 'toArray')) {
75 return $this->__invoke($value->toArray(), $recurse | self
::RECURSE_ARRAY
);
77 return $this->__invoke((array) $value, $recurse | self
::RECURSE_ARRAY
);
84 * Escape a value for current escaping strategy
86 * @param string $value
89 abstract protected function escape($value);
92 * Set the encoding to use for escape operations
94 * @param string $encoding
95 * @throws Exception\InvalidArgumentException
96 * @return AbstractHelper
98 public function setEncoding($encoding)
100 if (null !== $this->escaper
) {
101 throw new Exception\
InvalidArgumentException(
102 'Character encoding settings cannot be changed once the Helper has been used or '
103 . ' if a Zend\Escaper\Escaper object (with preset encoding option) is set.'
107 $this->encoding
= $encoding;
113 * Get the encoding to use for escape operations
117 public function getEncoding()
119 return $this->encoding
;
123 * Set instance of Escaper
125 * @param Escaper\Escaper $escaper
126 * @return AbstractHelper
128 public function setEscaper(Escaper\Escaper
$escaper)
130 $this->escaper
= $escaper;
131 $this->encoding
= $escaper->getEncoding();
137 * Get instance of Escaper
139 * @return null|Escaper\Escaper
141 public function getEscaper()
143 if (null === $this->escaper
) {
144 $this->setEscaper(new Escaper\
Escaper($this->getEncoding()));
147 return $this->escaper
;