Browse Source

[ZF-3516] Zend_Filter_HtmlEntities:

 - added support for doubleQuote

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@16185 44c647ce-9c0f-0410-b52a-842ac1e357ba
thomas 16 years ago
parent
commit
842cd54696
2 changed files with 82 additions and 14 deletions
  1. 60 10
      library/Zend/Filter/HtmlEntities.php
  2. 22 4
      tests/Zend/Filter/HtmlEntitiesTest.php

+ 60 - 10
library/Zend/Filter/HtmlEntities.php

@@ -1,5 +1,4 @@
 <?php
-
 /**
  * Zend Framework
  *
@@ -20,13 +19,11 @@
  * @version    $Id$
  */
 
-
 /**
  * @see Zend_Filter_Interface
  */
 require_once 'Zend/Filter/Interface.php';
 
-
 /**
  * @category   Zend
  * @package    Zend_Filter
@@ -36,30 +33,61 @@ require_once 'Zend/Filter/Interface.php';
 class Zend_Filter_HtmlEntities implements Zend_Filter_Interface
 {
     /**
-     * Corresponds to second htmlentities() argument
+     * Corresponds to the second htmlentities() argument
      *
      * @var integer
      */
     protected $_quoteStyle;
 
     /**
-     * Corresponds to third htmlentities() argument
+     * Corresponds to the third htmlentities() argument
      *
      * @var string
      */
     protected $_charSet;
 
     /**
+     * Corresponds to the forth htmlentities() argument
+     *
+     * @var unknown_type
+     */
+    protected $_doubleQuote;
+
+    /**
      * Sets filter options
      *
-     * @param  integer $quoteStyle
+     * @param  integer|array $quoteStyle
      * @param  string  $charSet
      * @return void
      */
-    public function __construct($quoteStyle = ENT_COMPAT, $charSet = 'ISO-8859-1')
+    public function __construct($options = array())
     {
-        $this->_quoteStyle = $quoteStyle;
-        $this->_charSet    = $charSet;
+        if (!is_array($options)) {
+            trigger_error('Support for multiple arguments is deprecated in favor of a single options array', E_USER_NOTICE);
+            $argv = func_get_args();
+            $temp['quotestyle'] = array_shift($options);
+            if (!empty($argv)) {
+                $temp['charset'] = array_shift($options);
+            }
+
+            $options = $temp;
+        }
+
+        if (!isset($options['quotestyle'])) {
+            $options['quotestyle'] = ENT_COMPAT;
+        }
+
+        if (!isset($options['charset'])) {
+            $options['charset'] = 'ISO-8859-1';
+        }
+
+        if (!isset($options['doublequote'])) {
+            $options['doublequote'] = true;
+        }
+
+        $this->setQuoteStyle($options['quotestyle']);
+        $this->setCharSet($options['charset']);
+        $this->setDoubleQuote($options['doublequote']);
     }
 
     /**
@@ -107,6 +135,28 @@ class Zend_Filter_HtmlEntities implements Zend_Filter_Interface
     }
 
     /**
+     * Returns the doubleQuote option
+     *
+     * @return boolean
+     */
+    public function getDoubleQuote()
+    {
+        return $this->_doubleQuote;
+    }
+
+    /**
+     * Sets the doubleQuote option
+     *
+     * @param boolean $doubleQuote
+     * @return Zend_Filter_HtmlEntities Provides a fluent interface
+     */
+    public function setDoubleQuote($doubleQuote)
+    {
+        $this->_doubleQuote = (boolean) $doubleQuote;
+        return $this;
+    }
+
+    /**
      * Defined by Zend_Filter_Interface
      *
      * Returns the string $value, converting characters to their corresponding HTML entity
@@ -117,6 +167,6 @@ class Zend_Filter_HtmlEntities implements Zend_Filter_Interface
      */
     public function filter($value)
     {
-        return htmlentities((string) $value, $this->_quoteStyle, $this->_charSet);
+        return htmlentities((string) $value, $this->_quoteStyle, $this->_charSet, $this->_doubleQuote);
     }
 }

+ 22 - 4
tests/Zend/Filter/HtmlEntitiesTest.php

@@ -1,5 +1,4 @@
 <?php
-
 /**
  * Zend Framework
  *
@@ -21,7 +20,6 @@
  * @version    $Id$
  */
 
-
 /**
  * Test helper
  */
@@ -32,7 +30,6 @@ require_once dirname(__FILE__) . '/../../TestHelper.php';
  */
 require_once 'Zend/Filter/HtmlEntities.php';
 
-
 /**
  * @category   Zend
  * @package    Zend_Filter
@@ -122,13 +119,34 @@ class Zend_Filter_HtmlEntitiesTest extends PHPUnit_Framework_TestCase
     }
 
     /**
+     * Ensures that getDoubleQuote() returns expected default value
+     *
+     * @return void
+     */
+    public function testGetDoubleQuote()
+    {
+        $this->assertEquals(true, $this->_filter->getDoubleQuote());
+    }
+
+    /**
+     * Ensures that setDoubleQuote() follows expected behavior
+     *
+     * @return void
+     */
+    public function testSetDoubleQuote()
+    {
+        $this->_filter->setDoubleQuote(false);
+        $this->assertEquals(false, $this->_filter->getDoubleQuote());
+    }
+
+    /**
      * Ensure that fluent interfaces are supported
      *
      * @group ZF-3172
      */
     public function testFluentInterface()
     {
-        $instance = $this->_filter->setCharSet('UTF-8')->setQuoteStyle(ENT_QUOTES);
+        $instance = $this->_filter->setCharSet('UTF-8')->setQuoteStyle(ENT_QUOTES)->setDoubleQuote(false);
         $this->assertTrue($instance instanceof Zend_Filter_HtmlEntities);
     }
 }