- Output for 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.25, 8.4.1 - 8.4.12
- Fatal error: Uncaught Error: Interface "ValidatorRuleInterface" not found in /in/IZjC3:3
Stack trace:
#0 {main}
thrown in /in/IZjC3 on line 3
Process exited with code 255.
<?php
class NumberInRange implements ValidatorRuleInterface
{
private string $format;
private int $min;
private int $max;
private string $fieldName;
public function __construct(int $min, int $max, string $fieldName)
{
$this->min = $min;
$this->max = $max;
$this->fieldName = $fieldName;
$this->format = '%s не может быть больше $this->max и меньше $this->min.';
}
public function __invoke(mixed $value): ?ValidationError
{
if (!is_int($value)) {
throw new TypeException("Неправильный тип данных в аргументе функции.");
}
$resultMin = $value < $min;
$resultMax = $value > $max;
if ($resultMin || $resultMax) {
$errorText = sprintf($this->format, $this->fieldName);
return new ValidationError($errorText);
} else {
return null;
}
}
}