0 follower

Abstract Class Yiisoft\Validator\Rule\Date\BaseDateHandler

InheritanceYiisoft\Validator\Rule\Date\BaseDateHandler
ImplementsYiisoft\Validator\RuleHandlerInterface
SubclassesYiisoft\Validator\Rule\Date\DateHandler, Yiisoft\Validator\Rule\Date\DateTimeHandler, Yiisoft\Validator\Rule\Date\TimeHandler

Method Details

Hide inherited methods

__construct() public method

public mixed __construct ( integer $dateType, integer $timeType, string|null $timeZone, string|null $locale, string|null $messageFormat, integer|null $messageDateType, integer|null $messageTimeType, string $incorrectInputMessage, string $tooEarlyMessage, string $tooLateMessage )
$dateType integer
$timeType integer
$timeZone string|null
$locale string|null
$messageFormat string|null
$messageDateType integer|null
$messageTimeType integer|null
$incorrectInputMessage string
$tooEarlyMessage string
$tooLateMessage string

                public function __construct(
    private readonly int $dateType,
    private readonly int $timeType,
    private readonly ?string $timeZone,
    private readonly ?string $locale,
    private readonly ?string $messageFormat,
    private readonly ?int $messageDateType,
    private readonly ?int $messageTimeType,
    private readonly string $incorrectInputMessage,
    private readonly string $tooEarlyMessage,
    private readonly string $tooLateMessage,
) {}

            
validate() public method

public Yiisoft\Validator\Result validate ( mixed $value, Yiisoft\Validator\RuleInterface $rule, Yiisoft\Validator\ValidationContext $context )
$value mixed
$rule Yiisoft\Validator\RuleInterface
$context Yiisoft\Validator\ValidationContext

                public function validate(mixed $value, RuleInterface $rule, ValidationContext $context): Result
{
    if (!$rule instanceof Date && !$rule instanceof DateTime && !$rule instanceof Time) {
        throw new UnexpectedRuleException([Date::class, DateTime::class, Time::class], $rule);
    }
    $timeZone = $rule->getTimeZone() ?? $this->timeZone;
    if ($timeZone !== null) {
        $timeZone = new DateTimeZone($timeZone);
    }
    $result = new Result();
    $date = $this->prepareValue($value, $rule, $timeZone, false);
    if ($date === null) {
        $result->addError(
            $rule->getIncorrectInputMessage() ?? $this->incorrectInputMessage,
            [
                'property' => $context->getTranslatedProperty(),
                'Property' => $context->getCapitalizedTranslatedProperty(),
            ],
        );
        return $result;
    }
    $min = $this->prepareValue($rule->getMin(), $rule, $timeZone, true);
    if ($min !== null && $date < $min) {
        $result->addError(
            $rule->getTooEarlyMessage() ?? $this->tooEarlyMessage,
            [
                'property' => $context->getTranslatedProperty(),
                'Property' => $context->getCapitalizedTranslatedProperty(),
                'value' => $this->formatDate($date, $rule, $timeZone),
                'limit' => $this->formatDate($min, $rule, $timeZone),
            ],
        );
        return $result;
    }
    $max = $this->prepareValue($rule->getMax(), $rule, $timeZone, true);
    if ($max !== null && $date > $max) {
        $result->addError(
            $rule->getTooLateMessage() ?? $this->tooLateMessage,
            [
                'property' => $context->getTranslatedProperty(),
                'Property' => $context->getCapitalizedTranslatedProperty(),
                'value' => $this->formatDate($date, $rule, $timeZone),
                'limit' => $this->formatDate($max, $rule, $timeZone),
            ],
        );
        return $result;
    }
    return $result;
}