Browse Source

[ZF-7153] Zend_Filter*:

- added Zend_Config support for parameters to all filters
- added Array support for parameters to all filters

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@18081 44c647ce-9c0f-0410-b52a-842ac1e357ba
thomas 16 years ago
parent
commit
81d3770539

+ 4 - 2
documentation/manual/en/module_specs/Zend_Filter-Callback.xml

@@ -49,8 +49,10 @@ print $filter->filter('Hello!');
 
     <programlisting language="php"><![CDATA[
 $filter = new Zend_Filter_Callback(
-    'MyMethod',
-    array('key' => 'param1', 'key2' => 'param2')
+    array(
+        'callback' => 'MyMethod',
+        'options'  => array('key' => 'param1', 'key2' => 'param2')
+    )
 );
 $filter->filter(array('value' => 'Hello'));
 ]]></programlisting>

+ 10 - 0
library/Zend/Filter/Alnum.php

@@ -73,6 +73,16 @@ class Zend_Filter_Alnum implements Zend_Filter_Interface
      */
     public function __construct($allowWhiteSpace = false)
     {
+        if ($allowWhiteSpace instanceof Zend_Config) {
+            $allowWhiteSpace = $allowWhiteSpace->toArray();
+        } else if (is_array($allowWhiteSpace)) {
+            if (array_key_exists('allowwhitespace', $allowWhiteSpace)) {
+                $allowWhiteSpace = $allowWhiteSpace['allowwhitespace'];
+            } else {
+                $allowWhiteSpace = false;
+            }
+        }
+
         $this->allowWhiteSpace = (boolean) $allowWhiteSpace;
         if (null === self::$_unicodeEnabled) {
             self::$_unicodeEnabled = (@preg_match('/\pL/u', 'a')) ? true : false;

+ 10 - 0
library/Zend/Filter/Alpha.php

@@ -73,6 +73,16 @@ class Zend_Filter_Alpha implements Zend_Filter_Interface
      */
     public function __construct($allowWhiteSpace = false)
     {
+        if ($allowWhiteSpace instanceof Zend_Config) {
+            $allowWhiteSpace = $allowWhiteSpace->toArray();
+        } else if (is_array($allowWhiteSpace)) {
+            if (array_key_exists('allowwhitespace', $allowWhiteSpace)) {
+                $allowWhiteSpace = $allowWhiteSpace['allowwhitespace'];
+            } else {
+                $allowWhiteSpace = false;
+            }
+        }
+
         $this->allowWhiteSpace = (boolean) $allowWhiteSpace;
         if (null === self::$_unicodeEnabled) {
             self::$_unicodeEnabled = (@preg_match('/\pL/u', 'a')) ? true : false;

+ 22 - 3
library/Zend/Filter/Callback.php

@@ -52,10 +52,29 @@ class Zend_Filter_Callback implements Zend_Filter_Interface
      * @param string|array $callback Callback in a call_user_func format
      * @param mixed        $options  (Optional) Default options for this filter
      */
-    public function __construct($callback, $options = null)
+    public function __construct($options)
     {
-        $this->setCallback($callback);
-        $this->setOptions($options);
+        if ($options instanceof Zend_Config) {
+            $options = $options->toArray();
+        } else if (!is_array($options) || !array_key_exists('callback', $options)) {
+            $options          = func_get_args();
+            $temp['callback'] = array_shift($options);
+            if (!empty($options)) {
+                $temp['options'] = array_shift($options);
+            }
+
+            $options = $temp;
+        }
+
+        if (!array_key_exists('callback', $options)) {
+            require_once 'Zend/Filter/Exception.php';
+            throw new Zend_Filter_Exception('Missing callback to use');
+        }
+
+        $this->setCallback($options['callback']);
+        if (array_key_exists('options', $options)) {
+            $this->setOptions($options['options']);
+        }
     }
 
     /**

+ 4 - 0
library/Zend/Filter/Encrypt.php

@@ -51,6 +51,10 @@ class Zend_Filter_Encrypt implements Zend_Filter_Interface
      */
     public function __construct($options = null)
     {
+        if ($options instanceof Zend_Config) {
+            $options = $options->toArray();
+        }
+
         $this->setAdapter($options);
     }
 

+ 2 - 1
library/Zend/Filter/HtmlEntities.php

@@ -63,7 +63,8 @@ class Zend_Filter_HtmlEntities implements Zend_Filter_Interface
     public function __construct($options = array())
     {
         if (!is_array($options)) {
-            trigger_error('Support for multiple arguments is deprecated in favor of a single options array', E_USER_NOTICE);
+// @todo: Preperation for 2.0... needs to be cleared with the dev-team
+//            trigger_error('Support for multiple arguments is deprecated in favor of a single options array', E_USER_NOTICE);
             $options = func_get_args();
             $temp['quotestyle'] = array_shift($options);
             if (!empty($options)) {

+ 4 - 0
library/Zend/Filter/LocalizedToNormalized.php

@@ -56,6 +56,10 @@ class Zend_Filter_LocalizedToNormalized implements Zend_Filter_Interface
      */
     public function __construct($options = null)
     {
+        if ($options instanceof Zend_Config) {
+            $options = $options->toArray();
+        }
+
         if (null !== $options) {
             $this->setOptions($options);
         }

+ 4 - 0
library/Zend/Filter/NormalizedToLocalized.php

@@ -55,6 +55,10 @@ class Zend_Filter_NormalizedToLocalized implements Zend_Filter_Interface
      */
     public function __construct($options = null)
     {
+        if ($options instanceof Zend_Config) {
+            $options = $options->toArray();
+        }
+
         if (null !== $options) {
             $this->setOptions($options);
         }

+ 20 - 7
library/Zend/Filter/PregReplace.php

@@ -77,19 +77,32 @@ class Zend_Filter_PregReplace implements Zend_Filter_Interface
 
     /**
      * Constructor
+     * Supported options are
+     *     'match'   => matching pattern
+     *     'replace' => replace with this
      *
-     * @param  string $match
-     * @param  string $replace
+     * @param  string|array $options
      * @return void
      */
-    public function __construct($matchPattern = null, $replacement = null)
+    public function __construct($options = null)
     {
-        if ($matchPattern) {
-            $this->setMatchPattern($matchPattern);
+        if ($options instanceof Zend_Config) {
+            $options = $options->toArray();
+        } else if (!is_array($options)) {
+            $options       = func_get_args();
+            $temp['match'] = array_shift($options);
+            if (!empty($options)) {
+                $temp['replace'] = array_shift($options);
+            }
+            $options = $temp;
         }
 
-        if ($replacement) {
-            $this->setReplacement($replacement);
+        if (array_key_exists('match', $options)) {
+            $this->setMatchPattern($options['match']);
+        }
+
+        if (array_key_exists('replace', $options)) {
+            $this->setReplacement($options['replace']);
         }
     }
 

+ 13 - 4
library/Zend/Filter/StringToLower.php

@@ -42,15 +42,24 @@ class Zend_Filter_StringToLower implements Zend_Filter_Interface
     /**
      * Constructor
      *
-     * @param string|array $options OPTIONAL
+     * @param string|array|Zend_Config $options OPTIONAL
      */
     public function __construct($options = null)
     {
-        if (is_array($options) && array_key_exists('encoding', $options)) {
-            $options = $options['encoding'];
+        if ($options instanceof Zend_Config) {
+            $options = $options->toArray();
+        } else if (!is_array($options)) {
+            $options = func_get_args();
+            $temp    = array();
+            if (!empty($options)) {
+                $temp['encoding'] = array_shift($options);
+            }
+            $options = $temp;
         }
 
-        $this->setEncoding($options);
+        if (array_key_exists('encoding', $options)) {
+            $this->setEncoding($options);
+        }
     }
 
     /**

+ 12 - 2
library/Zend/Filter/StringTrim.php

@@ -45,12 +45,22 @@ class Zend_Filter_StringTrim implements Zend_Filter_Interface
     /**
      * Sets filter options
      *
-     * @param  string $charList
+     * @param  string|array|Zend_Config $charList
      * @return void
      */
     public function __construct($charList = null)
     {
-        $this->_charList = $charList;
+        if ($charList instanceof Zend_Config) {
+            $charList = $charList->toArray();
+        } else if (!is_array($charList)) {
+            $options          = func_get_args();
+            $temp['charlist'] = array_shift($options);
+            $options          = $temp;
+        }
+
+        if (array_key_exists('charlist', $options)) {
+            $this->setCharList($options['charlist']);
+        }
     }
 
     /**

+ 34 - 8
library/Zend/Filter/StripTags.php

@@ -48,7 +48,7 @@ class Zend_Filter_StripTags implements Zend_Filter_Interface
      * @var boolean
      * @deprecated
      */
-    public $commentsAllowed;
+    public $commentsAllowed = false;
 
     /**
      * Array of allowed tags and allowed attributes for each allowed tag
@@ -71,17 +71,43 @@ class Zend_Filter_StripTags implements Zend_Filter_Interface
 
     /**
      * Sets the filter options
+     * Allowed options are
+     *     'allowTags'     => Tags which are allowed
+     *     'allowAttribs'  => Attributes which are allowed
+     *     'allowComments' => Are comments allowed ?
      *
-     * @param  array|string $tagsAllowed
-     * @param  array|string $attributesAllowed
-     * @param  boolean      $allowComments
+     * @param  string|array|Zend_Config $options
      * @return void
      */
-    public function __construct($tagsAllowed = null, $attributesAllowed = null, $commentsAllowed = false)
+    public function __construct($options = null)
     {
-        $this->setTagsAllowed($tagsAllowed);
-        $this->setAttributesAllowed($attributesAllowed);
-        $this->setCommentsAllowed($commentsAllowed);
+        if ($options instanceof Zend_Config) {
+            $options = $options->toArray();
+        } else if (!is_array($options)) {
+            $options = func_get_args();
+            $temp['allowTags'] = array_shift($options);
+            if (!empty($options)) {
+                $temp['allowAttribs'] = array_shift($options);
+            }
+
+            if (!empty($options)) {
+                $temp['allowComments'] = array_shift($options);
+            }
+
+            $options = $temp;
+        }
+
+        if (array_key_exists('allowTags', $options)) {
+            $this->setTagsAllowed($options['allowTags']);
+        }
+
+        if (array_key_exists('allowAttribs', $options)) {
+            $this->setAttributesAllowed($options['allowAttribs']);
+        }
+
+        if (array_key_exists('allowComments', $options)) {
+            $this->setCommentsAllowed($options['allowComments']);
+        }
     }
 
     /**

+ 84 - 16
library/Zend/Validate/InArray.php

@@ -1,5 +1,4 @@
 <?php
-
 /**
  * Zend Framework
  *
@@ -20,13 +19,11 @@
  * @version    $Id$
  */
 
-
 /**
  * @see Zend_Validate_Abstract
  */
 require_once 'Zend/Validate/Abstract.php';
 
-
 /**
  * @category   Zend
  * @package    Zend_Validate
@@ -35,7 +32,6 @@ require_once 'Zend/Validate/Abstract.php';
  */
 class Zend_Validate_InArray extends Zend_Validate_Abstract
 {
-
     const NOT_IN_ARRAY = 'notInArray';
 
     /**
@@ -57,19 +53,56 @@ class Zend_Validate_InArray extends Zend_Validate_Abstract
      *
      * @var boolean
      */
-    protected $_strict;
+    protected $_strict = false;
+
+    /**
+     * Whether a recursive search should be done
+     *
+     * @var boolean
+     */
+    protected $_recursive = false;
 
     /**
      * Sets validator options
      *
-     * @param  array   $haystack
-     * @param  boolean $strict
+     * @param  array|Zend_Config $haystack
      * @return void
      */
-    public function __construct(array $haystack, $strict = false)
+    public function __construct($options)
     {
-        $this->setHaystack($haystack)
-             ->setStrict($strict);
+        if ($options instanceof Zend_Config) {
+            $options = $options->toArray();
+        } else if (!is_array($options)) {
+            require_once 'Zend/Validate/Exception.php';
+            throw new Zend_Validate_Exception('Array expected as parameter');
+        } else {
+            $count = func_num_args();
+            $temp = array();
+            if ($count > 1) {
+// @todo: Preperation for 2.0... needs to be cleared with the dev-team
+//              trigger_error('Support for multiple arguments is deprecated in favor of a single options array', E_USER_NOTICE);
+                $temp['haystack'] = func_get_arg(0);
+                $temp['strict']   = func_get_arg(1);
+                $options = $temp;
+            } else {
+                $temp = func_get_arg(0);
+                if (!array_key_exists('haystack', $options)) {
+                    $options = array();
+                    $options['haystack'] = $temp;
+                } else {
+                    $options = $temp;
+                }
+            }
+        }
+
+        $this->setHaystack($options['haystack']);
+        if (array_key_exists('strict', $options)) {
+            $this->setStrict($options['strict']);
+        }
+
+        if (array_key_exists('recursive', $options)) {
+            $this->setRecursive($options['recursive']);
+        }
     }
 
     /**
@@ -112,7 +145,29 @@ class Zend_Validate_InArray extends Zend_Validate_Abstract
      */
     public function setStrict($strict)
     {
-        $this->_strict = $strict;
+        $this->_strict = (boolean) $strict;
+        return $this;
+    }
+
+    /**
+     * Returns the recursive option
+     *
+     * @return boolean
+     */
+    public function getRecursive()
+    {
+        return $this->_recursive;
+    }
+
+    /**
+     * Sets the recursive option
+     *
+     * @param  boolean $recursive
+     * @return Zend_Validate_InArray Provides a fluent interface
+     */
+    public function setRecursive($recursive)
+    {
+        $this->_recursive = (boolean) $recursive;
         return $this;
     }
 
@@ -128,11 +183,24 @@ class Zend_Validate_InArray extends Zend_Validate_Abstract
     public function isValid($value)
     {
         $this->_setValue($value);
-        if (!in_array($value, $this->_haystack, $this->_strict)) {
-            $this->_error(self::NOT_IN_ARRAY);
-            return false;
+        if ($this->getRecursive()) {
+            $iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($this->_haystack));
+            foreach($iterator as $element) {
+                if ($this->_strict) {
+                    if ($element === $value) {
+                        return true;
+                    }
+                } else if ($element == $value) {
+                    return true;
+                }
+            }
+        } else {
+            if (in_array($value, $this->_haystack, $this->_strict)) {
+                return true;
+            }
         }
-        return true;
-    }
 
+        $this->_error(self::NOT_IN_ARRAY);
+        return false;
+    }
 }