PageRenderTime 98ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/www/libs/Zend/Filter/HtmlEntities.php

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