web/lib/Zend/Validate/InArray.php
changeset 64 162c1de6545a
parent 19 1c2f13fd785c
child 68 ecaf28ffe26e
equal deleted inserted replaced
63:5b37998e522e 64:162c1de6545a
       
     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_Validate
       
    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: InArray.php 20352 2010-01-17 17:55:38Z thomas $
       
    20  */
       
    21 
       
    22 /**
       
    23  * @see Zend_Validate_Abstract
       
    24  */
       
    25 require_once 'Zend/Validate/Abstract.php';
       
    26 
       
    27 /**
       
    28  * @category   Zend
       
    29  * @package    Zend_Validate
       
    30  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    31  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    32  */
       
    33 class Zend_Validate_InArray extends Zend_Validate_Abstract
       
    34 {
       
    35     const NOT_IN_ARRAY = 'notInArray';
       
    36 
       
    37     /**
       
    38      * @var array
       
    39      */
       
    40     protected $_messageTemplates = array(
       
    41         self::NOT_IN_ARRAY => "'%value%' was not found in the haystack",
       
    42     );
       
    43 
       
    44     /**
       
    45      * Haystack of possible values
       
    46      *
       
    47      * @var array
       
    48      */
       
    49     protected $_haystack;
       
    50 
       
    51     /**
       
    52      * Whether a strict in_array() invocation is used
       
    53      *
       
    54      * @var boolean
       
    55      */
       
    56     protected $_strict = false;
       
    57 
       
    58     /**
       
    59      * Whether a recursive search should be done
       
    60      *
       
    61      * @var boolean
       
    62      */
       
    63     protected $_recursive = false;
       
    64 
       
    65     /**
       
    66      * Sets validator options
       
    67      *
       
    68      * @param  array|Zend_Config $haystack
       
    69      * @return void
       
    70      */
       
    71     public function __construct($options)
       
    72     {
       
    73         if ($options instanceof Zend_Config) {
       
    74             $options = $options->toArray();
       
    75         } else if (!is_array($options)) {
       
    76             require_once 'Zend/Validate/Exception.php';
       
    77             throw new Zend_Validate_Exception('Array expected as parameter');
       
    78         } else {
       
    79             $count = func_num_args();
       
    80             $temp  = array();
       
    81             if ($count > 1) {
       
    82                 $temp['haystack'] = func_get_arg(0);
       
    83                 $temp['strict']   = func_get_arg(1);
       
    84                 $options = $temp;
       
    85             } else {
       
    86                 $temp = func_get_arg(0);
       
    87                 if (!array_key_exists('haystack', $options)) {
       
    88                     $options = array();
       
    89                     $options['haystack'] = $temp;
       
    90                 } else {
       
    91                     $options = $temp;
       
    92                 }
       
    93             }
       
    94         }
       
    95 
       
    96         $this->setHaystack($options['haystack']);
       
    97         if (array_key_exists('strict', $options)) {
       
    98             $this->setStrict($options['strict']);
       
    99         }
       
   100 
       
   101         if (array_key_exists('recursive', $options)) {
       
   102             $this->setRecursive($options['recursive']);
       
   103         }
       
   104     }
       
   105 
       
   106     /**
       
   107      * Returns the haystack option
       
   108      *
       
   109      * @return mixed
       
   110      */
       
   111     public function getHaystack()
       
   112     {
       
   113         return $this->_haystack;
       
   114     }
       
   115 
       
   116     /**
       
   117      * Sets the haystack option
       
   118      *
       
   119      * @param  mixed $haystack
       
   120      * @return Zend_Validate_InArray Provides a fluent interface
       
   121      */
       
   122     public function setHaystack(array $haystack)
       
   123     {
       
   124         $this->_haystack = $haystack;
       
   125         return $this;
       
   126     }
       
   127 
       
   128     /**
       
   129      * Returns the strict option
       
   130      *
       
   131      * @return boolean
       
   132      */
       
   133     public function getStrict()
       
   134     {
       
   135         return $this->_strict;
       
   136     }
       
   137 
       
   138     /**
       
   139      * Sets the strict option
       
   140      *
       
   141      * @param  boolean $strict
       
   142      * @return Zend_Validate_InArray Provides a fluent interface
       
   143      */
       
   144     public function setStrict($strict)
       
   145     {
       
   146         $this->_strict = (boolean) $strict;
       
   147         return $this;
       
   148     }
       
   149 
       
   150     /**
       
   151      * Returns the recursive option
       
   152      *
       
   153      * @return boolean
       
   154      */
       
   155     public function getRecursive()
       
   156     {
       
   157         return $this->_recursive;
       
   158     }
       
   159 
       
   160     /**
       
   161      * Sets the recursive option
       
   162      *
       
   163      * @param  boolean $recursive
       
   164      * @return Zend_Validate_InArray Provides a fluent interface
       
   165      */
       
   166     public function setRecursive($recursive)
       
   167     {
       
   168         $this->_recursive = (boolean) $recursive;
       
   169         return $this;
       
   170     }
       
   171 
       
   172     /**
       
   173      * Defined by Zend_Validate_Interface
       
   174      *
       
   175      * Returns true if and only if $value is contained in the haystack option. If the strict
       
   176      * option is true, then the type of $value is also checked.
       
   177      *
       
   178      * @param  mixed $value
       
   179      * @return boolean
       
   180      */
       
   181     public function isValid($value)
       
   182     {
       
   183         $this->_setValue($value);
       
   184         if ($this->getRecursive()) {
       
   185             $iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($this->_haystack));
       
   186             foreach($iterator as $element) {
       
   187                 if ($this->_strict) {
       
   188                     if ($element === $value) {
       
   189                         return true;
       
   190                     }
       
   191                 } else if ($element == $value) {
       
   192                     return true;
       
   193                 }
       
   194             }
       
   195         } else {
       
   196             if (in_array($value, $this->_haystack, $this->_strict)) {
       
   197                 return true;
       
   198             }
       
   199         }
       
   200 
       
   201         $this->_error(self::NOT_IN_ARRAY);
       
   202         return false;
       
   203     }
       
   204 }