| 1 |
<?php |
| 2 |
|
| 3 |
namespace ipl\Validator; |
| 4 |
|
| 5 |
use InvalidArgumentException; |
| 6 |
|
| 7 |
/** |
| 8 |
* Validate whether a value is between the given min and max |
| 9 |
*/ |
| 10 |
class BetweenValidator extends BaseValidator |
| 11 |
{ |
| 12 |
/** @var int|float Min value */ |
| 13 |
protected int|float $min; |
| 14 |
|
| 15 |
/** @var int|float Max value */ |
| 16 |
protected int|float $max; |
| 17 |
|
| 18 |
/** @var bool Whether min/max comparisons are inclusive; if false, the value may not equal min or max */ |
| 19 |
protected bool $inclusive; |
| 20 |
|
| 21 |
/** |
| 22 |
* Create a new BetweenValidator |
| 23 |
* |
| 24 |
* Required options: |
| 25 |
* |
| 26 |
* - min: (int|float) Minimum border |
| 27 |
* - max: (int|float) Maximum border |
| 28 |
* |
| 29 |
* Optional options: |
| 30 |
* |
| 31 |
* - inclusive: (bool) Whether inclusive border values, default true |
| 32 |
* |
| 33 |
* @param array{min: int|float, max: int|float, inclusive?: bool} $options |
| 34 |
* |
| 35 |
* @throws InvalidArgumentException When required option is missing |
| 36 |
*/ |
| 37 |
public function __construct(array $options) |
| 38 |
{ |
| 39 |
if (! isset($options['min'], $options['max'])) { |
| 40 |
throw new InvalidArgumentException("Missing option. 'min' and 'max' has to be given"); |
| 41 |
} |
| 42 |
|
| 43 |
$this->setMin($options['min']) |
| 44 |
->setMax($options['max']) |
| 45 |
->setInclusive($options['inclusive'] ?? true); |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Get the min option |
| 50 |
* |
| 51 |
* @return int|float |
| 52 |
*/ |
| 53 |
public function getMin(): int|float |
| 54 |
{ |
| 55 |
return $this->min; |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Set the min option |
| 60 |
* |
| 61 |
* @param int|float $min |
| 62 |
* |
| 63 |
* @return $this |
| 64 |
*/ |
| 65 |
public function setMin(int|float $min): static |
| 66 |
{ |
| 67 |
$this->min = $min; |
| 68 |
|
| 69 |
return $this; |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Get the max option |
| 74 |
* |
| 75 |
* @return int|float |
| 76 |
*/ |
| 77 |
public function getMax(): int|float |
| 78 |
{ |
| 79 |
return $this->max; |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Set the max option |
| 84 |
* |
| 85 |
* @param int|float $max |
| 86 |
* |
| 87 |
* @return $this |
| 88 |
*/ |
| 89 |
public function setMax(int|float $max): static |
| 90 |
{ |
| 91 |
$this->max = $max; |
| 92 |
|
| 93 |
return $this; |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Get the inclusive option |
| 98 |
* |
| 99 |
* @return bool |
| 100 |
*/ |
| 101 |
public function getInclusive(): bool |
| 102 |
{ |
| 103 |
return $this->inclusive; |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Set the inclusive option |
| 108 |
* |
| 109 |
* @param bool $inclusive |
| 110 |
* |
| 111 |
* @return $this |
| 112 |
*/ |
| 113 |
public function setInclusive(bool $inclusive = true): static |
| 114 |
{ |
| 115 |
$this->inclusive = $inclusive; |
| 116 |
|
| 117 |
return $this; |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Check whether the value is between the min and max |
| 122 |
* |
| 123 |
* @param int|float $value |
| 124 |
* |
| 125 |
* @return bool |
| 126 |
*/ |
| 127 |
public function isValid($value): bool |
| 128 |
{ |
| 129 |
// Reset messages from a previous isValid() call. |
| 130 |
$this->clearMessages(); |
| 131 |
|
| 132 |
if ($this->getInclusive()) { |
| 133 |
if ($this->getMin() > $value || $value > $this->getMax()) { |
| 134 |
$this->addMessage(sprintf( |
| 135 |
$this->translate("'%s' is not between '%s' and '%s', inclusively"), |
| 136 |
$value, |
| 137 |
$this->getMin(), |
| 138 |
$this->getMax() |
| 139 |
)); |
| 140 |
|
| 141 |
return false; |
| 142 |
} |
| 143 |
} elseif ($this->getMin() >= $value || $value >= $this->getMax()) { |
| 144 |
$this->addMessage(sprintf( |
| 145 |
$this->translate("'%s' is not between '%s' and '%s'"), |
| 146 |
$value, |
| 147 |
$this->getMin(), |
| 148 |
$this->getMax() |
| 149 |
)); |
| 150 |
|
| 151 |
return false; |
| 152 |
} |
| 153 |
|
| 154 |
return true; |
| 155 |
} |
| 156 |
} |