/Filter/HtmlEntities.php

https://github.com/massiveart/ZF-ZOOLU · PHP · 200 lines · 73 code · 20 blank · 107 comment · 6 complexity · ce7c32109bdeb080c862bd47585fde2a MD5 · raw file

  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Filter
  17. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id: HtmlEntities.php 20104 2010-01-06 21:26:01Z matthew $
  20. */
  21. /**
  22. * @see Zend_Filter_Interface
  23. */
  24. require_once 'Zend/Filter/Interface.php';
  25. /**
  26. * @category Zend
  27. * @package Zend_Filter
  28. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  29. * @license http://framework.zend.com/license/new-bsd New BSD License
  30. */
  31. class Zend_Filter_HtmlEntities implements Zend_Filter_Interface
  32. {
  33. /**
  34. * Corresponds to the second htmlentities() argument
  35. *
  36. * @var integer
  37. */
  38. protected $_quoteStyle;
  39. /**
  40. * Corresponds to the third htmlentities() argument
  41. *
  42. * @var string
  43. */
  44. protected $_encoding;
  45. /**
  46. * Corresponds to the forth htmlentities() argument
  47. *
  48. * @var unknown_type
  49. */
  50. protected $_doubleQuote;
  51. /**
  52. * Sets filter options
  53. *
  54. * @param integer|array $quoteStyle
  55. * @param string $charSet
  56. * @return void
  57. */
  58. public function __construct($options = array())
  59. {
  60. if (!is_array($options)) {
  61. $options = func_get_args();
  62. $temp['quotestyle'] = array_shift($options);
  63. if (!empty($options)) {
  64. $temp['charset'] = array_shift($options);
  65. }
  66. $options = $temp;
  67. }
  68. if (!isset($options['quotestyle'])) {
  69. $options['quotestyle'] = ENT_COMPAT;
  70. }
  71. if (!isset($options['encoding'])) {
  72. $options['encoding'] = 'UTF-8';
  73. }
  74. if (isset($options['charset'])) {
  75. $options['encoding'] = $options['charset'];
  76. }
  77. if (!isset($options['doublequote'])) {
  78. $options['doublequote'] = true;
  79. }
  80. $this->setQuoteStyle($options['quotestyle']);
  81. $this->setEncoding($options['encoding']);
  82. $this->setDoubleQuote($options['doublequote']);
  83. }
  84. /**
  85. * Returns the quoteStyle option
  86. *
  87. * @return integer
  88. */
  89. public function getQuoteStyle()
  90. {
  91. return $this->_quoteStyle;
  92. }
  93. /**
  94. * Sets the quoteStyle option
  95. *
  96. * @param integer $quoteStyle
  97. * @return Zend_Filter_HtmlEntities Provides a fluent interface
  98. */
  99. public function setQuoteStyle($quoteStyle)
  100. {
  101. $this->_quoteStyle = $quoteStyle;
  102. return $this;
  103. }
  104. /**
  105. * Get encoding
  106. *
  107. * @return string
  108. */
  109. public function getEncoding()
  110. {
  111. return $this->_encoding;
  112. }
  113. /**
  114. * Set encoding
  115. *
  116. * @param string $value
  117. * @return Zend_Filter_HtmlEntities
  118. */
  119. public function setEncoding($value)
  120. {
  121. $this->_encoding = (string) $value;
  122. return $this;
  123. }
  124. /**
  125. * Returns the charSet option
  126. *
  127. * Proxies to {@link getEncoding()}
  128. *
  129. * @return string
  130. */
  131. public function getCharSet()
  132. {
  133. return $this->getEncoding();
  134. }
  135. /**
  136. * Sets the charSet option
  137. *
  138. * Proxies to {@link setEncoding()}
  139. *
  140. * @param string $charSet
  141. * @return Zend_Filter_HtmlEntities Provides a fluent interface
  142. */
  143. public function setCharSet($charSet)
  144. {
  145. return $this->setEncoding($charSet);
  146. }
  147. /**
  148. * Returns the doubleQuote option
  149. *
  150. * @return boolean
  151. */
  152. public function getDoubleQuote()
  153. {
  154. return $this->_doubleQuote;
  155. }
  156. /**
  157. * Sets the doubleQuote option
  158. *
  159. * @param boolean $doubleQuote
  160. * @return Zend_Filter_HtmlEntities Provides a fluent interface
  161. */
  162. public function setDoubleQuote($doubleQuote)
  163. {
  164. $this->_doubleQuote = (boolean) $doubleQuote;
  165. return $this;
  166. }
  167. /**
  168. * Defined by Zend_Filter_Interface
  169. *
  170. * Returns the string $value, converting characters to their corresponding HTML entity
  171. * equivalents where they exist
  172. *
  173. * @param string $value
  174. * @return string
  175. */
  176. public function filter($value)
  177. {
  178. return htmlentities((string) $value, $this->getQuoteStyle(), $this->getEncoding(), $this->getDoubleQuote());
  179. }
  180. }