0 follower

Final Class Yiisoft\Validator\Rule\UrlHandler

InheritanceYiisoft\Validator\Rule\UrlHandler
ImplementsYiisoft\Validator\RuleHandlerInterface

Validates that the value is a valid HTTP or HTTPS URL.

See also Yiisoft\Validator\Rule\Url.

Public Methods

Hide inherited methods

Method Description Defined By
validate() Yiisoft\Validator\Rule\UrlHandler

Method Details

Hide inherited methods

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 Url) {
        throw new UnexpectedRuleException(Url::class, $rule);
    }
    $result = new Result();
    if (!is_string($value)) {
        $result->addError($rule->getIncorrectInputMessage(), [
            'property' => $context->getTranslatedProperty(),
            'Property' => $context->getCapitalizedTranslatedProperty(),
            'type' => get_debug_type($value),
        ]);
        return $result;
    }
    // Make sure the length is limited to avoid DOS attacks.
    if (strlen($value) < 2000) {
        if ($rule->isIdnEnabled()) {
            $value = $this->convertIdn($value);
        }
        if (preg_match($rule->getPattern(), $value)) {
            return $result;
        }
    }
    $result->addError($rule->getMessage(), [
        'property' => $context->getTranslatedProperty(),
        'Property' => $context->getCapitalizedTranslatedProperty(),
        'value' => $value,
    ]);
    return $result;
}