Source of file AbstractHelper.php
Size: 4,177 Bytes - Last Modified: 2014-03-12T23:21:18+01:00
/home/theseer/Downloads/ZendFramework-2.3.0/library/Zend/View/Helper/Escaper/AbstractHelper.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 | <?php /** * Zend Framework (http://framework.zend.com/) * * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ namespace Zend\View\Helper\Escaper; use Zend\Escaper; use Zend\View\Exception; use Zend\View\Helper; /** * Helper for escaping values */ abstract class AbstractHelper extends Helper\AbstractHelper { /** * @const Recursion constants */ const RECURSE_NONE = 0x00; const RECURSE_ARRAY = 0x01; const RECURSE_OBJECT = 0x02; /** * @var string Encoding */ protected $encoding = 'UTF-8'; /** * @var Escaper\Escaper */ protected $escaper = null; /** * Invoke this helper: escape a value * * @param mixed $value * @param int $recurse Expects one of the recursion constants; used to decide whether or not to recurse the given value when escaping * @throws Exception\InvalidArgumentException * @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. */ public function __invoke($value, $recurse = self::RECURSE_NONE) { if (is_string($value)) { return $this->escape($value); } if (is_array($value)) { if (!(self::RECURSE_ARRAY & $recurse)) { throw new Exception\InvalidArgumentException( 'Array provided to Escape helper, but flags do not allow recursion' ); } foreach ($value as $k => $v) { $value[$k] = $this->__invoke($v, $recurse); } return $value; } if (is_object($value)) { if (!(self::RECURSE_OBJECT & $recurse)) { // Attempt to cast it to a string if (method_exists($value, '__toString')) { return $this->escape((string) $value); } throw new Exception\InvalidArgumentException( 'Object provided to Escape helper, but flags do not allow recursion' ); } if (method_exists($value, 'toArray')) { return $this->__invoke($value->toArray(), $recurse | self::RECURSE_ARRAY); } return $this->__invoke((array) $value, $recurse | self::RECURSE_ARRAY); } return $value; } /** * Escape a value for current escaping strategy * * @param string $value * @return string */ abstract protected function escape($value); /** * Set the encoding to use for escape operations * * @param string $encoding * @throws Exception\InvalidArgumentException * @return AbstractHelper */ public function setEncoding($encoding) { if (null !== $this->escaper) { throw new Exception\InvalidArgumentException( 'Character encoding settings cannot be changed once the Helper has been used or ' . ' if a Zend\Escaper\Escaper object (with preset encoding option) is set.' ); } $this->encoding = $encoding; return $this; } /** * Get the encoding to use for escape operations * * @return string */ public function getEncoding() { return $this->encoding; } /** * Set instance of Escaper * * @param Escaper\Escaper $escaper * @return AbstractHelper */ public function setEscaper(Escaper\Escaper $escaper) { $this->escaper = $escaper; $this->encoding = $escaper->getEncoding(); return $this; } /** * Get instance of Escaper * * @return null|Escaper\Escaper */ public function getEscaper() { if (null === $this->escaper) { $this->setEscaper(new Escaper\Escaper($this->getEncoding())); } return $this->escaper; } } |