2
0

InArray.php 5.3 KB

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