diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-13 11:30:08 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-13 11:30:08 +0000 |
commit | 4ce65d59ca91871cfd126497158200a818720bce (patch) | |
tree | e277def01fc7eba7dbc21c4a4ae5576e8aa2cf1f /vendor/ipl/validator/src/LessThanValidator.php | |
parent | Initial commit. (diff) | |
download | icinga-php-library-96ecad332d1684426665842bfcebf1aa4692a020.tar.xz icinga-php-library-96ecad332d1684426665842bfcebf1aa4692a020.zip |
Adding upstream version 0.13.1.upstream/0.13.1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/ipl/validator/src/LessThanValidator.php')
-rw-r--r-- | vendor/ipl/validator/src/LessThanValidator.php | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/vendor/ipl/validator/src/LessThanValidator.php b/vendor/ipl/validator/src/LessThanValidator.php new file mode 100644 index 0000000..68e3daf --- /dev/null +++ b/vendor/ipl/validator/src/LessThanValidator.php @@ -0,0 +1,69 @@ +<?php + +namespace ipl\Validator; + +use ipl\I18n\Translation; + +/** + * Validates whether the value is less than the given max + */ +class LessThanValidator extends BaseValidator +{ + use Translation; + + /** @var mixed Comparison value for less than */ + protected $max; + + /** + * Create a new LessThanValidator + * + * Optional options: + * - max: (int) Comparison value for less than, default 0 + */ + public function __construct(array $options = []) + { + $this->setMax($options['max'] ?? 0); + } + + /** + * Get the max option + * + * @return mixed + */ + public function getMax() + { + return $this->max; + } + + /** + * Set the max option + * + * @param mixed $max + * + * @return $this + */ + public function setMax($max): self + { + $this->max = $max; + + return $this; + } + + public function isValid($value) + { + // Multiple isValid() calls must not stack validation messages + $this->clearMessages(); + + if ($this->getMax() <= $value) { + $this->addMessage(sprintf( + $this->translate("'%s' is not less than '%s'"), + $value, + $this->getMax() + )); + + return false; + } + + return true; + } +} |