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\Filter
;
13 use Zend\Stdlib\ArrayUtils
;
15 class HtmlEntities
extends AbstractFilter
18 * Corresponds to the second htmlentities() argument
22 protected $quoteStyle;
25 * Corresponds to the third htmlentities() argument
32 * Corresponds to the forth htmlentities() argument
36 protected $doubleQuote;
41 * @param array|Traversable $options
43 public function __construct($options = array())
45 if ($options instanceof Traversable
) {
46 $options = ArrayUtils
::iteratorToArray($options);
48 if (!is_array($options)) {
49 $options = func_get_args();
50 $temp['quotestyle'] = array_shift($options);
51 if (!empty($options)) {
52 $temp['charset'] = array_shift($options);
58 if (!isset($options['quotestyle'])) {
59 $options['quotestyle'] = ENT_QUOTES
;
62 if (!isset($options['encoding'])) {
63 $options['encoding'] = 'UTF-8';
65 if (isset($options['charset'])) {
66 $options['encoding'] = $options['charset'];
69 if (!isset($options['doublequote'])) {
70 $options['doublequote'] = true;
73 $this->setQuoteStyle($options['quotestyle']);
74 $this->setEncoding($options['encoding']);
75 $this->setDoubleQuote($options['doublequote']);
79 * Returns the quoteStyle option
83 public function getQuoteStyle()
85 return $this->quoteStyle
;
89 * Sets the quoteStyle option
91 * @param int $quoteStyle
92 * @return self Provides a fluent interface
94 public function setQuoteStyle($quoteStyle)
96 $this->quoteStyle
= $quoteStyle;
106 public function getEncoding()
108 return $this->encoding
;
114 * @param string $value
117 public function setEncoding($value)
119 $this->encoding
= (string) $value;
124 * Returns the charSet option
126 * Proxies to {@link getEncoding()}
130 public function getCharSet()
132 return $this->getEncoding();
136 * Sets the charSet option
138 * Proxies to {@link setEncoding()}
140 * @param string $charSet
141 * @return self Provides a fluent interface
143 public function setCharSet($charSet)
145 return $this->setEncoding($charSet);
149 * Returns the doubleQuote option
153 public function getDoubleQuote()
155 return $this->doubleQuote
;
159 * Sets the doubleQuote option
161 * @param bool $doubleQuote
162 * @return self Provides a fluent interface
164 public function setDoubleQuote($doubleQuote)
166 $this->doubleQuote
= (bool) $doubleQuote;
171 * Defined by Zend\Filter\FilterInterface
173 * Returns the string $value, converting characters to their corresponding HTML entity
174 * equivalents where they exist
176 * If the value provided is non-scalar, the value will remain unfiltered
177 * and an E_USER_WARNING will be raised indicating it's unfilterable.
179 * @param string $value
180 * @return string|mixed
181 * @throws Exception\DomainException on encoding mismatches
183 public function filter($value)
185 if (null === $value) {
189 if (!is_scalar($value)) {
192 '%s expects parameter to be scalar, "%s" given; cannot filter',
194 (is_object($value) ?
get_class($value) : gettype($value))
201 $filtered = htmlentities((string) $value, $this->getQuoteStyle(), $this->getEncoding(), $this->getDoubleQuote());
202 if (strlen((string) $value) && !strlen($filtered)) {
203 if (!function_exists('iconv')) {
204 throw new Exception\
DomainException('Encoding mismatch has resulted in htmlentities errors');
206 $enc = $this->getEncoding();
207 $value = iconv('', $this->getEncoding() . '//IGNORE', (string) $value);
208 $filtered = htmlentities($value, $this->getQuoteStyle(), $enc, $this->getDoubleQuote());
209 if (!strlen($filtered)) {
210 throw new Exception\
DomainException('Encoding mismatch has resulted in htmlentities errors');