Jelajahi Sumber

[ZF-2899] Zend_Filter / Zend_Validate:

- added support for defaultNamespaces

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@16286 44c647ce-9c0f-0410-b52a-842ac1e357ba
thomas 16 tahun lalu
induk
melakukan
e904fea0ae

+ 69 - 0
documentation/manual/en/module_specs/Zend_Filter.xml

@@ -119,8 +119,77 @@ echo Zend_Filter::get('"', 'HtmlEntities', array(ENT_QUOTES));
             sets of input data. See <xref linkend="zend.filter.input" />.
         </para>
 
+        <sect3 id="zend.filter.introduction.static.namespaces">
+
+            <para>
+                When working with self defined filters you can give a forth parameter
+                to <methodname>Zend_Filter::get()</methodname> which is the namespace
+                where your filter can be found.
+            </para>
+
+            <programlisting language="php"><![CDATA[
+echo Zend_Filter::get(
+    '"',
+    'MyFilter',
+    array($parameters),
+    array('FirstNamespace', 'SecondNamespace')
+);
+]]></programlisting>
+
+            <para>
+                <classname>Zend_Filter</classname> allows also to set namespaces as default.
+                This means that you can set them once in your bootstrap and have not to give
+                them again for each call of <methodname>Zend_Filter::get()</methodname>. The
+                following code snippet is identical to the above one.
+            </para>
+
+            <programlisting language="php"><![CDATA[
+Zend_Filter::setDefaultNamespaces(array('FirstNamespace', 'SecondNamespace'));
+echo Zend_Filter::get('"', 'MyFilter', array($parameters));
+echo Zend_Filter::get('"', 'OtherFilter', array($parameters));
+]]></programlisting>
+
+            <para>
+                For your convinience there are following methods which allow the handling of
+                namespaces:
+            </para>
+
+            <itemizedlist>
+                <listitem>
+                    <para>
+                        <emphasis><methodname>Zend_Filter::getDefaultNamespaces()</methodname></emphasis>:
+                        Returns all set default namespaces as array.
+                    </para>
+                </listitem>
+
+                <listitem>
+                    <para>
+                        <emphasis><methodname>Zend_Filter::setDefaultNamespaces()</methodname></emphasis>:
+                        Sets new default namespaces and overrides any previous set. It accepts
+                        eighter a string for a single namespace of an array for multiple namespaces.
+                    </para>
+                </listitem>
+
+                <listitem>
+                    <para>
+                        <emphasis><methodname>Zend_Filter::addDefaultNamespaces()</methodname></emphasis>:
+                        Adds additional namespaces to already set ones. It accepts eighter a string
+                        for a single namespace of an array for multiple namespaces.
+                    </para>
+                </listitem>
+
+                <listitem>
+                    <para>
+                        <emphasis><methodname>Zend_Filter::hasDefaultNamespaces()</methodname></emphasis>:
+                        Returns true when one or more default namespaces are set, and false when no
+                        default namespaces are set.
+                    </para>
+                </listitem>
+            </itemizedlist>
+        </sect3>
     </sect2>
 
+
 </sect1>
 <!--
 vim:se ts=4 sw=4 et:

+ 71 - 0
documentation/manual/en/module_specs/Zend_Validate.xml

@@ -242,6 +242,77 @@ if (Zend_Validate::is($value, 'Between', array(1, 12))) {
             <xref linkend="zend.filter.input" />.
         </para>
 
+        <sect3 id="zend.validate.introduction.static.namespaces">
+
+            <para>
+                When working with self defined validators you can give a forth parameter
+                to <methodname>Zend_Validate::is()</methodname> which is the namespace
+                where your validator can be found.
+            </para>
+
+            <programlisting language="php"><![CDATA[
+if (Zend_Validate::is($value, 'MyValidator', array(1, 12),
+                      array('FirstNamespace', 'SecondNamespace')) {
+    // Yes, $value is ok
+}
+]]></programlisting>
+
+            <para>
+                <classname>Zend_Validate</classname> allows also to set namespaces as default.
+                This means that you can set them once in your bootstrap and have not to give
+                them again for each call of <methodname>Zend_Validate::is()</methodname>. The
+                following code snippet is identical to the above one.
+            </para>
+
+            <programlisting language="php"><![CDATA[
+Zend_Validate::setDefaultNamespaces(array('FirstNamespace', 'SecondNamespace'));
+if (Zend_Validate::is($value, 'MyValidator', array(1, 12)) {
+    // Yes, $value is ok
+}
+
+if (Zend_Validate::is($value, 'OtherValidator', array(1, 12)) {
+    // Yes, $value is ok
+}
+]]></programlisting>
+
+            <para>
+                For your convinience there are following methods which allow the handling of
+                namespaces:
+            </para>
+
+            <itemizedlist>
+                <listitem>
+                    <para>
+                        <emphasis><methodname>Zend_Validator::getDefaultNamespaces()</methodname></emphasis>:
+                        Returns all set default namespaces as array.
+                    </para>
+                </listitem>
+
+                <listitem>
+                    <para>
+                        <emphasis><methodname>Zend_Validator::setDefaultNamespaces()</methodname></emphasis>:
+                        Sets new default namespaces and overrides any previous set. It accepts
+                        eighter a string for a single namespace of an array for multiple namespaces.
+                    </para>
+                </listitem>
+
+                <listitem>
+                    <para>
+                        <emphasis><methodname>Zend_Validator::addDefaultNamespaces()</methodname></emphasis>:
+                        Adds additional namespaces to already set ones. It accepts eighter a string
+                        for a single namespace of an array for multiple namespaces.
+                    </para>
+                </listitem>
+
+                <listitem>
+                    <para>
+                        <emphasis><methodname>Zend_Validator::hasDefaultNamespaces()</methodname></emphasis>:
+                        Returns true when one or more default namespaces are set, and false when no
+                        default namespaces are set.
+                    </para>
+                </listitem>
+            </itemizedlist>
+        </sect3>
     </sect2>
 
     <sect2 id="zend.validate.introduction.translation">

+ 58 - 1
library/Zend/Filter.php

@@ -40,6 +40,13 @@ class Zend_Filter implements Zend_Filter_Interface
     protected $_filters = array();
 
     /**
+     * Default Namespaces
+     *
+     * @var array
+     */
+    protected static $_defaultNamespaces = array();
+
+    /**
      * Adds a filter to the end of the chain
      *
      * @param  Zend_Filter_Interface $filter
@@ -69,6 +76,56 @@ class Zend_Filter implements Zend_Filter_Interface
     }
 
     /**
+     * Returns the set default namespaces
+     *
+     * @return array
+     */
+    public static function getDefaultNamespaces()
+    {
+        return self::$_defaultNamespaces;
+    }
+
+    /**
+     * Sets new default namespaces
+     *
+     * @param array|string $namespace
+     * @return null
+     */
+    public static function setDefaultNamespaces($namespace)
+    {
+        if (!is_array($namespace)) {
+            $namespace = array((string) $namespace);
+        }
+
+        self::$_defaultNamespaces = $namespace;
+    }
+
+    /**
+     * Adds a new default namespace
+     *
+     * @param array|string $namespace
+     * @return null
+     */
+    public static function addDefaultNamespaces($namespace)
+    {
+        if (!is_array($namespace)) {
+            $namespace = array((string) $namespace);
+        }
+
+        self::$_defaultNamespaces = array_unique(array_merge(self::$_defaultNamespaces, $namespace));
+    }
+
+    /**
+     * Returns true when defaultNamespaces are set
+     *
+     * @return boolean
+     */
+    public static function hasDefaultNamespaces()
+    {
+        return (!empty(self::$_defaultNamespaces));
+    }
+
+    /**
      * Returns a value filtered through a specified filter class, without requiring separate
      * instantiation of the filter object.
      *
@@ -88,7 +145,7 @@ class Zend_Filter implements Zend_Filter_Interface
     public static function get($value, $classBaseName, array $args = array(), $namespaces = array())
     {
         require_once 'Zend/Loader.php';
-        $namespaces = array_merge((array) $namespaces, array('Zend_Filter'));
+        $namespaces = array_merge((array) $namespaces, self::$_defaultNamespaces, array('Zend_Filter'));
         foreach ($namespaces as $namespace) {
             $className = $namespace . '_' . ucfirst($classBaseName);
             if (!class_exists($className)) {

+ 79 - 1
library/Zend/Validate.php

@@ -47,6 +47,13 @@ class Zend_Validate implements Zend_Validate_Interface
     protected $_messages = array();
 
     /**
+     * Default Namespaces
+     *
+     * @var array
+     */
+    protected static $_defaultNamespaces = array();
+
+    /**
      * Array of validation failure message codes
      *
      * @var array
@@ -128,6 +135,56 @@ class Zend_Validate implements Zend_Validate_Interface
     }
 
     /**
+     * Returns the set default namespaces
+     *
+     * @return array
+     */
+    public static function getDefaultNamespaces()
+    {
+        return self::$_defaultNamespaces;
+    }
+
+    /**
+     * Sets new default namespaces
+     *
+     * @param array|string $namespace
+     * @return null
+     */
+    public static function setDefaultNamespaces($namespace)
+    {
+        if (!is_array($namespace)) {
+            $namespace = array((string) $namespace);
+        }
+
+        self::$_defaultNamespaces = $namespace;
+    }
+
+    /**
+     * Adds a new default namespace
+     *
+     * @param array|string $namespace
+     * @return null
+     */
+    public static function addDefaultNamespaces($namespace)
+    {
+        if (!is_array($namespace)) {
+            $namespace = array((string) $namespace);
+        }
+
+        self::$_defaultNamespaces = array_unique(array_merge(self::$_defaultNamespaces, $namespace));
+    }
+
+    /**
+     * Returns true when defaultNamespaces are set
+     *
+     * @return boolean
+     */
+    public static function hasDefaultNamespaces()
+    {
+        return (!empty(self::$_defaultNamespaces));
+    }
+
+    /**
      * @param  mixed    $value
      * @param  string   $classBaseName
      * @param  array    $args          OPTIONAL
@@ -137,7 +194,7 @@ class Zend_Validate implements Zend_Validate_Interface
      */
     public static function is($value, $classBaseName, array $args = array(), $namespaces = array())
     {
-        $namespaces = array_merge((array) $namespaces, array('Zend_Validate'));
+        $namespaces = array_merge((array) $namespaces, self::$_defaultNamespaces, array('Zend_Validate'));
         foreach ($namespaces as $namespace) {
             $className = $namespace . '_' . ucfirst($classBaseName);
             try {
@@ -165,4 +222,25 @@ class Zend_Validate implements Zend_Validate_Interface
         throw new Zend_Validate_Exception("Validate class not found from basename '$classBaseName'");
     }
 
+    /**
+     * Returns the maximum allowed message length
+     *
+     * @return integer
+     */
+    public static function getMessageLength()
+    {
+        require_once 'Zend/Validate/Abstract.php';
+        return Zend_Validate_Abstract::getMessageLength();
+    }
+
+    /**
+     * Sets the maximum allowed message length
+     *
+     * @param integer $length
+     */
+    public static function setMessageLength($length = -1)
+    {
+        require_once 'Zend/Validate/Abstract.php';
+        Zend_Validate_Abstract::setMessageLength($length);
+    }
 }

+ 42 - 0
tests/Zend/FilterTest.php

@@ -60,6 +60,16 @@ class Zend_FilterTest extends PHPUnit_Framework_TestCase
     }
 
     /**
+     * Resets the default namespaces
+     *
+     * @return void
+     */
+    public function tearDown()
+    {
+        Zend_Filter::setDefaultNamespaces(array());
+    }
+
+    /**
      * Ensures expected return value from empty filter chain
      *
      * @return void
@@ -148,6 +158,38 @@ class Zend_FilterTest extends PHPUnit_Framework_TestCase
             $this->error = true;
         }
     }
+
+    /**
+     * Testing Namespaces
+     *
+     * @return void
+     */
+    public function testNamespaces()
+    {
+        $this->assertEquals(array(), Zend_Filter::getDefaultNamespaces());
+        $this->assertFalse(Zend_Filter::hasDefaultNamespaces());
+
+        Zend_Filter::setDefaultNamespaces('TestDir');
+        $this->assertEquals(array('TestDir'), Zend_Filter::getDefaultNamespaces());
+
+        Zend_Filter::setDefaultNamespaces('OtherTestDir');
+        $this->assertEquals(array('OtherTestDir'), Zend_Filter::getDefaultNamespaces());
+
+        $this->assertTrue(Zend_Filter::hasDefaultNamespaces());
+
+        Zend_Filter::setDefaultNamespaces(array());
+
+        $this->assertEquals(array(), Zend_Filter::getDefaultNamespaces());
+        $this->assertFalse(Zend_Filter::hasDefaultNamespaces());
+
+        Zend_Filter::addDefaultNamespaces(array('One', 'Two'));
+        $this->assertEquals(array('One', 'Two'), Zend_Filter::getDefaultNamespaces());
+
+        Zend_Filter::addDefaultNamespaces('Three');
+        $this->assertEquals(array('One', 'Two', 'Three'), Zend_Filter::getDefaultNamespaces());
+
+        Zend_Filter::setDefaultNamespaces(array());
+    }
 }
 
 

+ 42 - 0
tests/Zend/ValidateTest.php

@@ -62,6 +62,16 @@ class Zend_ValidateTest extends PHPUnit_Framework_TestCase
     }
 
     /**
+     * Resets the default namespaces
+     *
+     * @return void
+     */
+    public function tearDown()
+    {
+        Zend_Validate::setDefaultNamespaces(array());
+    }
+
+    /**
      * Ensures expected results from empty validator chain
      *
      * @return void
@@ -169,6 +179,38 @@ class Zend_ValidateTest extends PHPUnit_Framework_TestCase
             $this->error = true;
         }
     }
+
+    /**
+     * Testing Namespaces
+     *
+     * @return void
+     */
+    public function testNamespaces()
+    {
+        $this->assertEquals(array(), Zend_Validate::getDefaultNamespaces());
+        $this->assertFalse(Zend_Validate::hasDefaultNamespaces());
+
+        Zend_Validate::setDefaultNamespaces('TestDir');
+        $this->assertEquals(array('TestDir'), Zend_Validate::getDefaultNamespaces());
+
+        Zend_Validate::setDefaultNamespaces('OtherTestDir');
+        $this->assertEquals(array('OtherTestDir'), Zend_Validate::getDefaultNamespaces());
+
+        $this->assertTrue(Zend_Validate::hasDefaultNamespaces());
+
+        Zend_Validate::setDefaultNamespaces(array());
+
+        $this->assertEquals(array(), Zend_Validate::getDefaultNamespaces());
+        $this->assertFalse(Zend_Validate::hasDefaultNamespaces());
+
+        Zend_Validate::addDefaultNamespaces(array('One', 'Two'));
+        $this->assertEquals(array('One', 'Two'), Zend_Validate::getDefaultNamespaces());
+
+        Zend_Validate::addDefaultNamespaces('Three');
+        $this->assertEquals(array('One', 'Two', 'Three'), Zend_Validate::getDefaultNamespaces());
+
+        Zend_Validate::setDefaultNamespaces(array());
+    }
 }