PageRenderTime 38ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/zendframework/zendframework/library/Zend/Filter/HtmlEntities.php

https://bitbucket.org/Evgenii/zf2t
PHP | 196 lines | 90 code | 21 blank | 85 comment | 11 complexity | 150d558339edc711d0184b224129cdaa MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework (http://framework.zend.com/)
  4. *
  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
  8. */
  9. namespace Zend\Filter;
  10. use Traversable;
  11. use Zend\Stdlib\ArrayUtils;
  12. class HtmlEntities extends AbstractFilter
  13. {
  14. /**
  15. * Corresponds to the second htmlentities() argument
  16. *
  17. * @var int
  18. */
  19. protected $quoteStyle;
  20. /**
  21. * Corresponds to the third htmlentities() argument
  22. *
  23. * @var string
  24. */
  25. protected $encoding;
  26. /**
  27. * Corresponds to the forth htmlentities() argument
  28. *
  29. * @var bool
  30. */
  31. protected $doubleQuote;
  32. /**
  33. * Sets filter options
  34. *
  35. * @param array|Traversable $options
  36. */
  37. public function __construct($options = array())
  38. {
  39. if ($options instanceof Traversable) {
  40. $options = ArrayUtils::iteratorToArray($options);
  41. }
  42. if (!is_array($options)) {
  43. $options = func_get_args();
  44. $temp['quotestyle'] = array_shift($options);
  45. if (!empty($options)) {
  46. $temp['charset'] = array_shift($options);
  47. }
  48. $options = $temp;
  49. }
  50. if (!isset($options['quotestyle'])) {
  51. $options['quotestyle'] = ENT_QUOTES;
  52. }
  53. if (!isset($options['encoding'])) {
  54. $options['encoding'] = 'UTF-8';
  55. }
  56. if (isset($options['charset'])) {
  57. $options['encoding'] = $options['charset'];
  58. }
  59. if (!isset($options['doublequote'])) {
  60. $options['doublequote'] = true;
  61. }
  62. $this->setQuoteStyle($options['quotestyle']);
  63. $this->setEncoding($options['encoding']);
  64. $this->setDoubleQuote($options['doublequote']);
  65. }
  66. /**
  67. * Returns the quoteStyle option
  68. *
  69. * @return int
  70. */
  71. public function getQuoteStyle()
  72. {
  73. return $this->quoteStyle;
  74. }
  75. /**
  76. * Sets the quoteStyle option
  77. *
  78. * @param int $quoteStyle
  79. * @return HtmlEntities Provides a fluent interface
  80. */
  81. public function setQuoteStyle($quoteStyle)
  82. {
  83. $this->quoteStyle = $quoteStyle;
  84. return $this;
  85. }
  86. /**
  87. * Get encoding
  88. *
  89. * @return string
  90. */
  91. public function getEncoding()
  92. {
  93. return $this->encoding;
  94. }
  95. /**
  96. * Set encoding
  97. *
  98. * @param string $value
  99. * @return HtmlEntities
  100. */
  101. public function setEncoding($value)
  102. {
  103. $this->encoding = (string) $value;
  104. return $this;
  105. }
  106. /**
  107. * Returns the charSet option
  108. *
  109. * Proxies to {@link getEncoding()}
  110. *
  111. * @return string
  112. */
  113. public function getCharSet()
  114. {
  115. return $this->getEncoding();
  116. }
  117. /**
  118. * Sets the charSet option
  119. *
  120. * Proxies to {@link setEncoding()}
  121. *
  122. * @param string $charSet
  123. * @return HtmlEntities Provides a fluent interface
  124. */
  125. public function setCharSet($charSet)
  126. {
  127. return $this->setEncoding($charSet);
  128. }
  129. /**
  130. * Returns the doubleQuote option
  131. *
  132. * @return bool
  133. */
  134. public function getDoubleQuote()
  135. {
  136. return $this->doubleQuote;
  137. }
  138. /**
  139. * Sets the doubleQuote option
  140. *
  141. * @param bool $doubleQuote
  142. * @return HtmlEntities Provides a fluent interface
  143. */
  144. public function setDoubleQuote($doubleQuote)
  145. {
  146. $this->doubleQuote = (bool) $doubleQuote;
  147. return $this;
  148. }
  149. /**
  150. * Defined by Zend\Filter\FilterInterface
  151. *
  152. * Returns the string $value, converting characters to their corresponding HTML entity
  153. * equivalents where they exist
  154. *
  155. * @param string $value
  156. * @throws Exception\DomainException
  157. * @return string
  158. */
  159. public function filter($value)
  160. {
  161. $filtered = htmlentities((string) $value, $this->getQuoteStyle(), $this->getEncoding(), $this->getDoubleQuote());
  162. if (strlen((string) $value) && !strlen($filtered)) {
  163. if (!function_exists('iconv')) {
  164. throw new Exception\DomainException('Encoding mismatch has resulted in htmlentities errors');
  165. }
  166. $enc = $this->getEncoding();
  167. $value = iconv('', $this->getEncoding() . '//IGNORE', (string) $value);
  168. $filtered = htmlentities($value, $this->getQuoteStyle(), $enc, $this->getDoubleQuote());
  169. if (!strlen($filtered)) {
  170. throw new Exception\DomainException('Encoding mismatch has resulted in htmlentities errors');
  171. }
  172. }
  173. return $filtered;
  174. }
  175. }