0 follower

Final Class Yiisoft\Validator\Rule\Date\DateHandler

InheritanceYiisoft\Validator\Rule\Date\DateHandler » Yiisoft\Validator\Rule\Date\BaseDateHandler
ImplementsYiisoft\Validator\RuleHandlerInterface

Method Details

Hide inherited methods

__construct() public method

public mixed __construct ( integer $dateType IntlDateFormatter::SHORT, string|null $timeZone null, string|null $locale null, string|null $messageFormat null, integer|null $messageDateType IntlDateFormatter::SHORT, string $incorrectInputMessage '{Property} must be a date.', string $tooEarlyMessage '{Property} must be no early than {limit}.', string $tooLateMessage '{Property} must be no late than {limit}.' )
$dateType integer
$timeZone string|null
$locale string|null
$messageFormat string|null
$messageDateType integer|null
$incorrectInputMessage string
$tooEarlyMessage string
$tooLateMessage string

                public function __construct(
    int $dateType = IntlDateFormatter::SHORT,
    ?string $timeZone = null,
    ?string $locale = null,
    ?string $messageFormat = null,
    ?int $messageDateType = IntlDateFormatter::SHORT,
    string $incorrectInputMessage = '{Property} must be a date.',
    string $tooEarlyMessage = '{Property} must be no early than {limit}.',
    string $tooLateMessage = '{Property} must be no late than {limit}.',
) {
    parent::__construct(
        $dateType,
        IntlDateFormatter::NONE,
        $timeZone,
        $locale,
        $messageFormat,
        $messageDateType,
        IntlDateFormatter::NONE,
        $incorrectInputMessage,
        $tooEarlyMessage,
        $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;
}