summaryrefslogtreecommitdiffstats
path: root/vendor/ipl/validator/src/LessThanValidator.php
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/ipl/validator/src/LessThanValidator.php')
-rw-r--r--vendor/ipl/validator/src/LessThanValidator.php69
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;
+ }
+}