| 1 |
<?php |
| 2 |
|
| 3 |
namespace ipl\Validator; |
| 4 |
|
| 5 |
use InvalidArgumentException; |
| 6 |
|
| 7 |
/** |
| 8 |
* Validate a string's length |
| 9 |
*/ |
| 10 |
class StringLengthValidator extends BaseValidator |
| 11 |
{ |
| 12 |
/** @var int Minimum required length */ |
| 13 |
protected int $min; |
| 14 |
|
| 15 |
/** @var ?int Maximum required length */ |
| 16 |
protected ?int $max = null; |
| 17 |
|
| 18 |
/** @var ?string Encoding to use */ |
| 19 |
protected ?string $encoding; |
| 20 |
|
| 21 |
/** |
| 22 |
* Create a new StringLengthValidator |
| 23 |
* |
| 24 |
* Optional options: |
| 25 |
* - min: (int) Minimum required string length, default 0 |
| 26 |
* - max: (int) Maximum required string length, default none |
| 27 |
* - encoding: (string) Encoding type, default none |
| 28 |
* |
| 29 |
* @param array{min?: int, max?: int, encoding?: string} $options |
| 30 |
*/ |
| 31 |
public function __construct(array $options = []) |
| 32 |
{ |
| 33 |
$this |
| 34 |
->setMin($options['min'] ?? 0) |
| 35 |
->setMax($options['max'] ?? null) |
| 36 |
->setEncoding($options['encoding'] ?? null); |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Get the minimum required string length |
| 41 |
* |
| 42 |
* @return int |
| 43 |
*/ |
| 44 |
public function getMin(): int |
| 45 |
{ |
| 46 |
return $this->min; |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Set the minimum required string length |
| 51 |
* |
| 52 |
* @param int $min |
| 53 |
* |
| 54 |
* @return $this |
| 55 |
* |
| 56 |
* @throws InvalidArgumentException When the $min is greater than the $max value |
| 57 |
*/ |
| 58 |
public function setMin(int $min): static |
| 59 |
{ |
| 60 |
if ($this->getMax() !== null && $min > $this->getMax()) { |
| 61 |
throw new InvalidArgumentException( |
| 62 |
sprintf( |
| 63 |
'The min must be less than or equal to the max length, but min: %d and max: %d given.', |
| 64 |
$min, |
| 65 |
$this->getMax() |
| 66 |
) |
| 67 |
); |
| 68 |
} |
| 69 |
|
| 70 |
$this->min = $min; |
| 71 |
|
| 72 |
return $this; |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Get the maximum required string length |
| 77 |
* |
| 78 |
* @return ?int |
| 79 |
*/ |
| 80 |
public function getMax(): ?int |
| 81 |
{ |
| 82 |
return $this->max; |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Set the maximum required string length |
| 87 |
* |
| 88 |
* @param ?int $max |
| 89 |
* |
| 90 |
* @return $this |
| 91 |
* |
| 92 |
* @throws InvalidArgumentException When the $min is greater than the $max value |
| 93 |
*/ |
| 94 |
public function setMax(?int $max): static |
| 95 |
{ |
| 96 |
if ($max !== null && $this->getMin() > $max) { |
| 97 |
throw new InvalidArgumentException( |
| 98 |
sprintf( |
| 99 |
'The min must be less than or equal to the max length, but min: %d and max: %d given.', |
| 100 |
$this->getMin(), |
| 101 |
$max |
| 102 |
) |
| 103 |
); |
| 104 |
} |
| 105 |
|
| 106 |
$this->max = $max; |
| 107 |
|
| 108 |
return $this; |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Get the encoding type to use |
| 113 |
* |
| 114 |
* @return ?string |
| 115 |
*/ |
| 116 |
public function getEncoding(): ?string |
| 117 |
{ |
| 118 |
return $this->encoding; |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Set the encoding type to use |
| 123 |
* |
| 124 |
* @param ?string $encoding |
| 125 |
* |
| 126 |
* @return $this |
| 127 |
* |
| 128 |
* @throws InvalidArgumentException When the given encoding is not supported |
| 129 |
*/ |
| 130 |
public function setEncoding(?string $encoding): static |
| 131 |
{ |
| 132 |
if ($encoding !== null) { |
| 133 |
$availableEncodings = array_map('strtolower', mb_list_encodings()); |
| 134 |
if (! in_array(strtolower($encoding), $availableEncodings, true)) { |
| 135 |
throw new InvalidArgumentException( |
| 136 |
sprintf('Given encoding "%s" is not supported on this OS!', $encoding) |
| 137 |
); |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
$this->encoding = $encoding; |
| 142 |
|
| 143 |
return $this; |
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* Check whether the string's length is within the configured min and max bounds |
| 148 |
* |
| 149 |
* @param string $value |
| 150 |
* |
| 151 |
* @return bool |
| 152 |
*/ |
| 153 |
public function isValid($value): bool |
| 154 |
{ |
| 155 |
// Reset messages from a previous isValid() call. |
| 156 |
$this->clearMessages(); |
| 157 |
|
| 158 |
if ($encoding = $this->getEncoding()) { // Encoding is nullable only in PHP >= 8.0. |
| 159 |
$length = mb_strlen($value, $encoding); |
| 160 |
} else { |
| 161 |
$length = mb_strlen($value); |
| 162 |
} |
| 163 |
|
| 164 |
if ($length < $this->getMin()) { |
| 165 |
$this->addMessage(sprintf( |
| 166 |
$this->translate('String should be %d characters long, %d given'), |
| 167 |
$this->getMin(), |
| 168 |
$length |
| 169 |
)); |
| 170 |
|
| 171 |
return false; |
| 172 |
} |
| 173 |
|
| 174 |
if ($this->getMax() && $this->getMax() < $length) { |
| 175 |
$this->addMessage(sprintf( |
| 176 |
$this->translate('String should be %d characters long, %d given'), |
| 177 |
$this->getMax(), |
| 178 |
$length |
| 179 |
)); |
| 180 |
|
| 181 |
return false; |
| 182 |
} |
| 183 |
|
| 184 |
return true; |
| 185 |
} |
| 186 |
} |