Debian Codegraph @

vendor/ipl/validator/src/GreaterThanValidator.php

raw
1 <?php
2
3 namespace ipl\Validator;
4
5 /**
6 * Validate whether a value is greater than the given minimum
7 */
8 class GreaterThanValidator extends BaseValidator
9 {
10 /** @var int|float Comparison value for greater than */
11 protected int|float $min;
12
13 /**
14 * Create a new GreaterThanValidator
15 *
16 * Optional options:
17 * - min: (int|float) Comparison value for greater than, default 0
18 *
19 * @param array{min?: int|float} $options
20 */
21 public function __construct(array $options = [])
22 {
23 $this->setMin($options['min'] ?? 0);
24 }
25
26 /**
27 * Get the min option
28 *
29 * @return int|float
30 */
31 public function getMin(): int|float
32 {
33 return $this->min;
34 }
35
36 /**
37 * Set the min option
38 *
39 * @param int|float $min
40 *
41 * @return $this
42 */
43 public function setMin(int|float $min): static
44 {
45 $this->min = $min;
46
47 return $this;
48 }
49
50 /**
51 * Check whether the value is greater than the minimum
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->getMin() >= $value) {
63 $this->addMessage(sprintf(
64 $this->translate("'%s' is not greater than '%s'"),
65 $value,
66 $this->min
67 ));
68
69 return false;
70 }
71
72 return true;
73 }
74 }