Between.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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_Between extends Zend_Validate_Abstract
  32. {
  33. /**
  34. * Validation failure message key for when the value is not between the min and max, inclusively
  35. */
  36. const NOT_BETWEEN = 'notBetween';
  37. /**
  38. * Validation failure message key for when the value is not strictly between the min and max
  39. */
  40. const NOT_BETWEEN_STRICT = 'notBetweenStrict';
  41. /**
  42. * Validation failure message template definitions
  43. *
  44. * @var array
  45. */
  46. protected $_messageTemplates = array(
  47. self::NOT_BETWEEN => "'%value%' is not between '%min%' and '%max%', inclusively",
  48. self::NOT_BETWEEN_STRICT => "'%value%' is not strictly between '%min%' and '%max%'"
  49. );
  50. /**
  51. * Additional variables available for validation failure messages
  52. *
  53. * @var array
  54. */
  55. protected $_messageVariables = array(
  56. 'min' => '_min',
  57. 'max' => '_max'
  58. );
  59. /**
  60. * Minimum value
  61. *
  62. * @var mixed
  63. */
  64. protected $_min;
  65. /**
  66. * Maximum value
  67. *
  68. * @var mixed
  69. */
  70. protected $_max;
  71. /**
  72. * Whether to do inclusive comparisons, allowing equivalence to min and/or max
  73. *
  74. * If false, then strict comparisons are done, and the value may equal neither
  75. * the min nor max options
  76. *
  77. * @var boolean
  78. */
  79. protected $_inclusive;
  80. /**
  81. * Sets validator options
  82. *
  83. * @param mixed $min
  84. * @param mixed $max
  85. * @param boolean $inclusive
  86. * @return void
  87. */
  88. public function __construct($min, $max, $inclusive = true)
  89. {
  90. $this->setMin($min)
  91. ->setMax($max)
  92. ->setInclusive($inclusive);
  93. }
  94. /**
  95. * Returns the min option
  96. *
  97. * @return mixed
  98. */
  99. public function getMin()
  100. {
  101. return $this->_min;
  102. }
  103. /**
  104. * Sets the min option
  105. *
  106. * @param mixed $min
  107. * @return Zend_Validate_Between Provides a fluent interface
  108. */
  109. public function setMin($min)
  110. {
  111. $this->_min = $min;
  112. return $this;
  113. }
  114. /**
  115. * Returns the max option
  116. *
  117. * @return mixed
  118. */
  119. public function getMax()
  120. {
  121. return $this->_max;
  122. }
  123. /**
  124. * Sets the max option
  125. *
  126. * @param mixed $max
  127. * @return Zend_Validate_Between Provides a fluent interface
  128. */
  129. public function setMax($max)
  130. {
  131. $this->_max = $max;
  132. return $this;
  133. }
  134. /**
  135. * Returns the inclusive option
  136. *
  137. * @return boolean
  138. */
  139. public function getInclusive()
  140. {
  141. return $this->_inclusive;
  142. }
  143. /**
  144. * Sets the inclusive option
  145. *
  146. * @param boolean $inclusive
  147. * @return Zend_Validate_Between Provides a fluent interface
  148. */
  149. public function setInclusive($inclusive)
  150. {
  151. $this->_inclusive = $inclusive;
  152. return $this;
  153. }
  154. /**
  155. * Defined by Zend_Validate_Interface
  156. *
  157. * Returns true if and only if $value is between min and max options, inclusively
  158. * if inclusive option is true.
  159. *
  160. * @param mixed $value
  161. * @return boolean
  162. */
  163. public function isValid($value)
  164. {
  165. $this->_setValue($value);
  166. if ($this->_inclusive) {
  167. if ($this->_min > $value || $value > $this->_max) {
  168. $this->_error(self::NOT_BETWEEN);
  169. return false;
  170. }
  171. } else {
  172. if ($this->_min >= $value || $value >= $this->_max) {
  173. $this->_error(self::NOT_BETWEEN_STRICT);
  174. return false;
  175. }
  176. }
  177. return true;
  178. }
  179. }