Debian Codegraph @

vendor/ipl/validator/src/LessThanValidator.php

raw
1 <?php
2
3 namespace ipl\Validator;
4
5 /**
6 * Validate whether a value is less than the given maximum
7 */
8 class LessThanValidator extends BaseValidator
9 {
10 /** @var int|float Comparison value for less than */
11 protected int|float $max;
12
13 /**
14 * Create a new LessThanValidator
15 *
16 * Optional options:
17 * - max: (int|float) Comparison value for less than, default 0
18 *
19 * @param array{max?: int|float} $options
20 */
21 public function __construct(array $options = [])
22 {
23 $this->setMax($options['max'] ?? 0);
24 }
25
26 /**
27 * Get the max option
28 *
29 * @return int|float
30 */
31 public function getMax(): int|float
32 {
33 return $this->max;
34 }
35
36 /**
37 * Set the max option
38 *
39 * @param int|float $max
40 *
41 * @return $this
42 */
43 public function setMax(int|float $max): static
44 {
45 $this->max = $max;
46
47 return $this;
48 }
49
50 /**
51 * Check whether the value is less than the maximum
52 *
53 * @param int|float $value
54 *
55 * @return bool
56 */
57 public function isValid($value): bool
58 {
59 // Reset messages from a previous isValid() call.
60 $this->clearMessages();
61
62 if ($this->getMax() <= $value) {
63 $this->addMessage(sprintf(
64 $this->translate("'%s' is not less than '%s'"),
65 $value,
66 $this->getMax()
67 ));
68
69 return false;
70 }
71
72 return true;
73 }
74 }