PageRenderTime 56ms CodeModel.GetById 37ms RepoModel.GetById 1ms app.codeStats 0ms

/src/Aura/Filter/Rule/Min.php

https://bitbucket.org/harikt/aura.filter
PHP | 71 lines | 26 code | 4 blank | 41 comment | 3 complexity | 86f52563f9fd16db20ed805637bef8aa MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. *
  4. * This file is part of the Aura project for PHP.
  5. *
  6. * @package Aura.Filter
  7. *
  8. * @license http://opensource.org/licenses/bsd-license.php BSD
  9. *
  10. */
  11. namespace Aura\Filter\Rule;
  12. use Aura\Filter\AbstractRule;
  13. /**
  14. *
  15. * Validates that a value is greater than or equal to a minimum.
  16. *
  17. * @package Aura.Filter
  18. *
  19. * @license http://opensource.org/licenses/bsd-license.php BSD
  20. *
  21. */
  22. class Min extends AbstractRule
  23. {
  24. /**
  25. *
  26. * Error message
  27. *
  28. * @var string
  29. */
  30. protected $message = 'FILTER_MIN';
  31. /**
  32. *
  33. * Validates that the value is greater than or equal to a minimum.
  34. *
  35. * @param mixed $min The minimum valid value.
  36. *
  37. * @return bool True if valid, false if not.
  38. *
  39. */
  40. protected function validate($min)
  41. {
  42. $value = $this->getValue();
  43. if (! is_scalar($value)) {
  44. return false;
  45. }
  46. return $value >= $min;
  47. }
  48. /**
  49. *
  50. * check whether the value is less than min, if so set to min
  51. *
  52. * @param int $min
  53. *
  54. * @return boolean
  55. */
  56. protected function sanitize($min)
  57. {
  58. $value = $this->getValue();
  59. if (! is_scalar($value)) {
  60. return false;
  61. }
  62. if ($value < $min) {
  63. $this->setValue($min);
  64. }
  65. return true;
  66. }
  67. }