InArray.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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;
  52. /**
  53. * Sets validator options
  54. *
  55. * @param array $haystack
  56. * @param boolean $strict
  57. * @return void
  58. */
  59. public function __construct(array $haystack, $strict = false)
  60. {
  61. $this->setHaystack($haystack)
  62. ->setStrict($strict);
  63. }
  64. /**
  65. * Returns the haystack option
  66. *
  67. * @return mixed
  68. */
  69. public function getHaystack()
  70. {
  71. return $this->_haystack;
  72. }
  73. /**
  74. * Sets the haystack option
  75. *
  76. * @param mixed $haystack
  77. * @return Zend_Validate_InArray Provides a fluent interface
  78. */
  79. public function setHaystack(array $haystack)
  80. {
  81. $this->_haystack = $haystack;
  82. return $this;
  83. }
  84. /**
  85. * Returns the strict option
  86. *
  87. * @return boolean
  88. */
  89. public function getStrict()
  90. {
  91. return $this->_strict;
  92. }
  93. /**
  94. * Sets the strict option
  95. *
  96. * @param boolean $strict
  97. * @return Zend_Validate_InArray Provides a fluent interface
  98. */
  99. public function setStrict($strict)
  100. {
  101. $this->_strict = $strict;
  102. return $this;
  103. }
  104. /**
  105. * Defined by Zend_Validate_Interface
  106. *
  107. * Returns true if and only if $value is contained in the haystack option. If the strict
  108. * option is true, then the type of $value is also checked.
  109. *
  110. * @param mixed $value
  111. * @return boolean
  112. */
  113. public function isValid($value)
  114. {
  115. $this->_setValue($value);
  116. if (!in_array($value, $this->_haystack, $this->_strict)) {
  117. $this->_error();
  118. return false;
  119. }
  120. return true;
  121. }
  122. }