File indexing completed on 2024-12-22 05:37:12

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_Validate
0017  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0018  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0019  * @version    $Id$
0020  */
0021 
0022 /**
0023  * @see Zend_Validate_Abstract
0024  */
0025 // require_once 'Zend/Validate/Abstract.php';
0026 
0027 /**
0028  * @category   Zend
0029  * @package    Zend_Validate
0030  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0031  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0032  */
0033 class Zend_Validate_InArray extends Zend_Validate_Abstract
0034 {
0035     const NOT_IN_ARRAY = 'notInArray';
0036 
0037     /**
0038      * @var array
0039      */
0040     protected $_messageTemplates = array(
0041         self::NOT_IN_ARRAY => "'%value%' was not found in the haystack",
0042     );
0043 
0044     /**
0045      * Haystack of possible values
0046      *
0047      * @var array
0048      */
0049     protected $_haystack;
0050 
0051     /**
0052      * Whether a strict in_array() invocation is used
0053      *
0054      * @var boolean
0055      */
0056     protected $_strict = false;
0057 
0058     /**
0059      * Whether a recursive search should be done
0060      *
0061      * @var boolean
0062      */
0063     protected $_recursive = false;
0064 
0065     /**
0066      * Sets validator options
0067      *
0068      * @param array|Zend_Config $options Validator options
0069      * @throws Zend_Validate_Exception
0070      */
0071     public function __construct($options)
0072     {
0073         if ($options instanceof Zend_Config) {
0074             $options = $options->toArray();
0075         } else if (!is_array($options)) {
0076             // require_once 'Zend/Validate/Exception.php';
0077             throw new Zend_Validate_Exception('Array expected as parameter');
0078         } else {
0079             $count = func_num_args();
0080             $temp  = array();
0081             if ($count > 1) {
0082                 $temp['haystack'] = func_get_arg(0);
0083                 $temp['strict']   = func_get_arg(1);
0084                 $options = $temp;
0085             } else {
0086                 $temp = func_get_arg(0);
0087                 if (!array_key_exists('haystack', $options)) {
0088                     $options = array();
0089                     $options['haystack'] = $temp;
0090                 } else {
0091                     $options = $temp;
0092                 }
0093             }
0094         }
0095 
0096         $this->setHaystack($options['haystack']);
0097         if (array_key_exists('strict', $options)) {
0098             $this->setStrict($options['strict']);
0099         }
0100 
0101         if (array_key_exists('recursive', $options)) {
0102             $this->setRecursive($options['recursive']);
0103         }
0104     }
0105 
0106     /**
0107      * Returns the haystack option
0108      *
0109      * @return mixed
0110      */
0111     public function getHaystack()
0112     {
0113         return $this->_haystack;
0114     }
0115 
0116     /**
0117      * Sets the haystack option
0118      *
0119      * @param  mixed $haystack
0120      * @return Zend_Validate_InArray Provides a fluent interface
0121      */
0122     public function setHaystack(array $haystack)
0123     {
0124         $this->_haystack = $haystack;
0125         return $this;
0126     }
0127 
0128     /**
0129      * Returns the strict option
0130      *
0131      * @return boolean
0132      */
0133     public function getStrict()
0134     {
0135         return $this->_strict;
0136     }
0137 
0138     /**
0139      * Sets the strict option
0140      *
0141      * @param  boolean $strict
0142      * @return Zend_Validate_InArray Provides a fluent interface
0143      */
0144     public function setStrict($strict)
0145     {
0146         $this->_strict = (boolean) $strict;
0147         return $this;
0148     }
0149 
0150     /**
0151      * Returns the recursive option
0152      *
0153      * @return boolean
0154      */
0155     public function getRecursive()
0156     {
0157         return $this->_recursive;
0158     }
0159 
0160     /**
0161      * Sets the recursive option
0162      *
0163      * @param  boolean $recursive
0164      * @return Zend_Validate_InArray Provides a fluent interface
0165      */
0166     public function setRecursive($recursive)
0167     {
0168         $this->_recursive = (boolean) $recursive;
0169         return $this;
0170     }
0171 
0172     /**
0173      * Defined by Zend_Validate_Interface
0174      *
0175      * Returns true if and only if $value is contained in the haystack option. If the strict
0176      * option is true, then the type of $value is also checked.
0177      *
0178      * @param  mixed $value
0179      * @return boolean
0180      */
0181     public function isValid($value)
0182     {
0183         $this->_setValue($value);
0184         if ($this->getRecursive()) {
0185             $iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($this->_haystack));
0186             foreach($iterator as $element) {
0187                 if ($this->_strict) {
0188                     if ($element === $value) {
0189                         return true;
0190                     }
0191                 } else if ($element == $value) {
0192                     return true;
0193                 }
0194             }
0195         } else {
0196             if (in_array($value, $this->_haystack, $this->_strict)) {
0197                 return true;
0198             }
0199         }
0200 
0201         $this->_error(self::NOT_IN_ARRAY);
0202         return false;
0203     }
0204 }