LessThan.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. /**
  3. *
  4. * Cube Framework $Id$ P8vf0/dK8jx2MjEUuRQLZ7tDO8jXX1BeSsghlRbeTkY=
  5. *
  6. * @link http://codecu.be/framework
  7. * @copyright Copyright (c) 2014 CodeCube SRL
  8. * @license http://codecu.be/framework/license Commercial License
  9. *
  10. * @version 1.3
  11. */
  12. /**
  13. * checks if the variable is smaller than a set value (with option to check if smaller or equal)
  14. */
  15. namespace Cube\Validate;
  16. class LessThan extends AbstractValidate
  17. {
  18. const LESS = 1;
  19. const LESS_EQUAL = 2;
  20. protected $_messages = array(
  21. self::LESS => "'%s' must be smaller than %value%.",
  22. self::LESS_EQUAL => "'%s' must be smaller or equal to %value%."
  23. );
  24. /**
  25. *
  26. * the maximum value allowed for the validator to check
  27. *
  28. * @var float
  29. */
  30. private $_maxValue;
  31. /**
  32. *
  33. * if true, it will check for equal values as well
  34. *
  35. * @var bool
  36. */
  37. private $_equal = false;
  38. /**
  39. *
  40. * class constructor
  41. *
  42. * initialize the maximum value allowed and the equal check
  43. *
  44. * @param array $data data[0] -> max value;
  45. * data[1] -> accept equal values (default = false)
  46. */
  47. public function __construct(array $data = null)
  48. {
  49. $this->setMaxValue($data[0])
  50. ->setEqual($data[1]);
  51. }
  52. /**
  53. *
  54. * get the maximum value accepted by the validator
  55. *
  56. * @return float
  57. */
  58. public function getMaxValue()
  59. {
  60. return $this->_maxValue;
  61. }
  62. /**
  63. *
  64. * set the maximum value the validator will compare against
  65. *
  66. * @param mixed $maxValue
  67. * @return \Cube\Validate\LessThan
  68. */
  69. public function setMaxValue($maxValue)
  70. {
  71. $this->_maxValue = $maxValue;
  72. return $this;
  73. }
  74. /**
  75. *
  76. * check if equal values are accepted
  77. *
  78. * @return bool
  79. */
  80. public function getEqual()
  81. {
  82. return $this->_equal;
  83. }
  84. /**
  85. *
  86. * set whether to validate equal values
  87. *
  88. * @param bool $equal
  89. * @return \Cube\Validate\LessThan
  90. */
  91. public function setEqual($equal = true)
  92. {
  93. $this->_equal = (bool) $equal;
  94. if ($this->_equal === true) {
  95. $this->setMessage($this->_messages[self::LESS_EQUAL]);
  96. }
  97. else {
  98. $this->setMessage($this->_messages[self::LESS]);
  99. }
  100. return $this;
  101. }
  102. /**
  103. *
  104. * checks if the variable is smaller than (or equal to) the set maximum value
  105. *
  106. * @return bool return true if the validation is successful
  107. */
  108. public function isValid()
  109. {
  110. $this->setMessage(
  111. str_replace('%value%', $this->_maxValue, $this->getMessage()));
  112. if ($this->_equal === true) {
  113. if ($this->_value > $this->_maxValue) {
  114. return false;
  115. }
  116. return true;
  117. }
  118. else {
  119. if ($this->_value >= $this->_maxValue) {
  120. return false;
  121. }
  122. return true;
  123. }
  124. }
  125. }