[ZF-6295] Generic:
[zend.git] / library / Zend / Validate / InArray.php
blob841a195cef472a1267c00efa9561c03044fc8d21
1 <?php
3 /**
4 * Zend Framework
6 * LICENSE
8 * This source file is subject to the new BSD license that is bundled
9 * with this package in the file LICENSE.txt.
10 * It is also available through the world-wide-web at this URL:
11 * http://framework.zend.com/license/new-bsd
12 * If you did not receive a copy of the license and are unable to
13 * obtain it through the world-wide-web, please send an email
14 * to license@zend.com so we can send you a copy immediately.
16 * @category Zend
17 * @package Zend_Validate
18 * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
19 * @license http://framework.zend.com/license/new-bsd New BSD License
20 * @version $Id$
24 /**
25 * @see Zend_Validate_Abstract
27 require_once 'Zend/Validate/Abstract.php';
30 /**
31 * @category Zend
32 * @package Zend_Validate
33 * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
34 * @license http://framework.zend.com/license/new-bsd New BSD License
36 class Zend_Validate_InArray extends Zend_Validate_Abstract
39 const NOT_IN_ARRAY = 'notInArray';
41 /**
42 * @var array
44 protected $_messageTemplates = array(
45 self::NOT_IN_ARRAY => "'%value%' was not found in the haystack"
48 /**
49 * Haystack of possible values
51 * @var array
53 protected $_haystack;
55 /**
56 * Whether a strict in_array() invocation is used
58 * @var boolean
60 protected $_strict;
62 /**
63 * Sets validator options
65 * @param array $haystack
66 * @param boolean $strict
67 * @return void
69 public function __construct(array $haystack, $strict = false)
71 $this->setHaystack($haystack)
72 ->setStrict($strict);
75 /**
76 * Returns the haystack option
78 * @return mixed
80 public function getHaystack()
82 return $this->_haystack;
85 /**
86 * Sets the haystack option
88 * @param mixed $haystack
89 * @return Zend_Validate_InArray Provides a fluent interface
91 public function setHaystack(array $haystack)
93 $this->_haystack = $haystack;
94 return $this;
97 /**
98 * Returns the strict option
100 * @return boolean
102 public function getStrict()
104 return $this->_strict;
108 * Sets the strict option
110 * @param boolean $strict
111 * @return Zend_Validate_InArray Provides a fluent interface
113 public function setStrict($strict)
115 $this->_strict = $strict;
116 return $this;
120 * Defined by Zend_Validate_Interface
122 * Returns true if and only if $value is contained in the haystack option. If the strict
123 * option is true, then the type of $value is also checked.
125 * @param mixed $value
126 * @return boolean
128 public function isValid($value)
130 $this->_setValue($value);
131 if (!in_array($value, $this->_haystack, $this->_strict)) {
132 $this->_error();
133 return false;
135 return true;