- Added min/max values

This commit is contained in:
Dave Mc Nicoll 2026-02-25 19:12:35 +00:00
parent a9613222f8
commit 05975089e5

View File

@ -15,6 +15,8 @@ class ContextField
public bool $mandatory = false,
public ?int $minLength = null,
public ?int $maxLength = null,
public ?int $minValue = null,
public ?int $maxValue = null,
public ?string $regexFormat = null,
public ?string $example = null,
public mixed $default = null,
@ -27,14 +29,21 @@ class ContextField
throw new MandatoryFieldException("Mandatory field must be provided a value.");
}
elseif ($value !== null) {
if ($this->minLength || $this->maxLength) {
if (is_string($value)) {
if (mb_strlen($value) < $this->minLength) {
throw new MinimumLengthException("Minimum {$this->minLength} string length is required.");
}
elseif (mb_strlen($value) > $this->maxLength) {
throw new MaximumLengthException("Maximum {$this->maxLength} string length has been exceeded.");
}
if (($this->minLength || $this->maxLength) && is_string($value)) {
if (mb_strlen($value) < $this->minLength) {
throw new MinimumLengthException("Minimum {$this->minLength} string length is required.");
}
elseif (mb_strlen($value) > $this->maxLength) {
throw new MaximumLengthException("Maximum {$this->maxLength} string length has been exceeded.");
}
}
if (($this->minValue || $this->maxValue) && is_numeric($value)) {
if ($value < $this->minValue) {
throw new MinimumLengthException("Minimum value set at {$this->minValue}.");
}
elseif ($value > $this->maxValue) {
throw new MaximumLengthException("Maximum value set at {$this->maxValue}.");
}
}