PageRenderTime 76ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/concreteOLD/libraries/3rdparty/Zend/Validate/Between.php

https://bitbucket.org/selfeky/xclusivescardwebsite
PHP | 224 lines | 87 code | 23 blank | 114 comment | 14 complexity | adc3603600abd91c45c3a9a76190a8d9 MD5 | raw file
  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-2011 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id: Between.php 23775 2011-03-01 17:25:24Z ralph $
  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-2011 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. * Accepts the following option keys:
  83. * 'min' => scalar, minimum border
  84. * 'max' => scalar, maximum border
  85. * 'inclusive' => boolean, inclusive border values
  86. *
  87. * @param array|Zend_Config $options
  88. * @return void
  89. */
  90. public function __construct($options)
  91. {
  92. if ($options instanceof Zend_Config) {
  93. $options = $options->toArray();
  94. } else if (!is_array($options)) {
  95. $options = func_get_args();
  96. $temp['min'] = array_shift($options);
  97. if (!empty($options)) {
  98. $temp['max'] = array_shift($options);
  99. }
  100. if (!empty($options)) {
  101. $temp['inclusive'] = array_shift($options);
  102. }
  103. $options = $temp;
  104. }
  105. if (!array_key_exists('min', $options) || !array_key_exists('max', $options)) {
  106. require_once 'Zend/Validate/Exception.php';
  107. throw new Zend_Validate_Exception("Missing option. 'min' and 'max' has to be given");
  108. }
  109. if (!array_key_exists('inclusive', $options)) {
  110. $options['inclusive'] = true;
  111. }
  112. $this->setMin($options['min'])
  113. ->setMax($options['max'])
  114. ->setInclusive($options['inclusive']);
  115. }
  116. /**
  117. * Returns the min option
  118. *
  119. * @return mixed
  120. */
  121. public function getMin()
  122. {
  123. return $this->_min;
  124. }
  125. /**
  126. * Sets the min option
  127. *
  128. * @param mixed $min
  129. * @return Zend_Validate_Between Provides a fluent interface
  130. */
  131. public function setMin($min)
  132. {
  133. $this->_min = $min;
  134. return $this;
  135. }
  136. /**
  137. * Returns the max option
  138. *
  139. * @return mixed
  140. */
  141. public function getMax()
  142. {
  143. return $this->_max;
  144. }
  145. /**
  146. * Sets the max option
  147. *
  148. * @param mixed $max
  149. * @return Zend_Validate_Between Provides a fluent interface
  150. */
  151. public function setMax($max)
  152. {
  153. $this->_max = $max;
  154. return $this;
  155. }
  156. /**
  157. * Returns the inclusive option
  158. *
  159. * @return boolean
  160. */
  161. public function getInclusive()
  162. {
  163. return $this->_inclusive;
  164. }
  165. /**
  166. * Sets the inclusive option
  167. *
  168. * @param boolean $inclusive
  169. * @return Zend_Validate_Between Provides a fluent interface
  170. */
  171. public function setInclusive($inclusive)
  172. {
  173. $this->_inclusive = $inclusive;
  174. return $this;
  175. }
  176. /**
  177. * Defined by Zend_Validate_Interface
  178. *
  179. * Returns true if and only if $value is between min and max options, inclusively
  180. * if inclusive option is true.
  181. *
  182. * @param mixed $value
  183. * @return boolean
  184. */
  185. public function isValid($value)
  186. {
  187. $this->_setValue($value);
  188. if ($this->_inclusive) {
  189. if ($this->_min > $value || $value > $this->_max) {
  190. $this->_error(self::NOT_BETWEEN);
  191. return false;
  192. }
  193. } else {
  194. if ($this->_min >= $value || $value >= $this->_max) {
  195. $this->_error(self::NOT_BETWEEN_STRICT);
  196. return false;
  197. }
  198. }
  199. return true;
  200. }
  201. }