0 follower

Final Class Yiisoft\Validator\Rule\Url

InheritanceYiisoft\Validator\Rule\Url
ImplementsYiisoft\Validator\DumpedRuleInterface, Yiisoft\Validator\SkipOnEmptyInterface, Yiisoft\Validator\SkipOnErrorInterface, Yiisoft\Validator\WhenInterface
Uses TraitsYiisoft\Validator\Rule\Trait\SkipOnEmptyTrait, Yiisoft\Validator\Rule\Trait\SkipOnErrorTrait, Yiisoft\Validator\Rule\Trait\WhenTrait

Defines validation options for a value that is a valid HTTP or HTTPS URL.

Note that the handler only checks if the URL scheme and host parts are correct. It does not check the remaining parts of a URL.

See also Yiisoft\Validator\Rule\UrlHandler.

Public Methods

Hide inherited methods

Method Description Defined By
__construct() Yiisoft\Validator\Rule\Url
getHandler() Yiisoft\Validator\Rule\Url
getIncorrectInputMessage() Get a message used when the input is incorrect. Yiisoft\Validator\Rule\Url
getMessage() Get a message used when the value is not valid. Yiisoft\Validator\Rule\Url
getName() Yiisoft\Validator\Rule\Url
getOptions() Yiisoft\Validator\Rule\Url
getPattern() Get ready to use regular expression pattern applied for URL validation. Yiisoft\Validator\Rule\Url
getSkipOnEmpty() A getter for $skipOnEmpty property. Yiisoft\Validator\Rule\Trait\SkipOnEmptyTrait
getValidSchemes() Get valid URI schemas. Yiisoft\Validator\Rule\Url
getWhen() A getter for $when property. Yiisoft\Validator\Rule\Trait\WhenTrait
isIdnEnabled() Whether the validation process must take {@link https://en.wikipedia.org/wiki/Internationalized_domain_name IDN (internationalized domain names)} into account. false means that validation of URLs containing IDN will always fail. Note that in order to use IDN validation you have to install and enable intl PHP extension, otherwise an exception will be thrown. Yiisoft\Validator\Rule\Url
shouldSkipOnError() A getter for $skipOnError property. Yiisoft\Validator\Rule\Trait\SkipOnErrorTrait
skipOnEmpty() An immutable setter to change $skipOnEmpty property. Yiisoft\Validator\Rule\Trait\SkipOnEmptyTrait
skipOnError() An immutable setter to change $skipOnError property. Yiisoft\Validator\Rule\Trait\SkipOnErrorTrait
when() An immutable setter to change $when property. Yiisoft\Validator\Rule\Trait\WhenTrait

Method Details

Hide inherited methods

__construct() public method

public mixed __construct ( string $pattern '/^{schemes}:\\/\\/(([a-zA-Z0-9][a-zA-Z0-9_-]*)(\\.[a-zA-Z0-9][a-zA-Z0-9_-]*)+)(?::\\d{1,5})?([?\\/#].*$|$)/', string[] $validSchemes = ['http''https'], boolean $enableIdn false, string $incorrectInputMessage '{Property} must be a string. {type} given.', string $message '{Property} is not a valid URL.', boolean|callable|null $skipOnEmpty null, boolean $skipOnError false, Closure|null $when null )
$pattern string

The regular expression used to validate the value. The pattern may contain a {schemes} token that will be replaced by a regular expression which represents the {@see $schemes}.

Note that if you want to reuse the pattern in HTML5 input, it should have ^ and $, should not have any modifiers and should not be case-insensitive.

$validSchemes string[]

List of URI schemes which should be considered valid. By default, http and https are considered to be valid schemes.

$enableIdn boolean

Whether the validation process must take {@link https://en.wikipedia.org/wiki/Internationalized_domain_name IDN (internationalized domain names)} into account . Defaults to false meaning that validation of URLs containing IDN will always fail. Note that in order to use IDN validation you have to install and enable intl PHP extension, otherwise an exception will be thrown.

$incorrectInputMessage string

A message used when the input is incorrect.

You may use the following placeholders in the message:

  • {property}: the translated label of the property being validated.
  • {type}: the value's type.
$message string

@var string A message used when the value is not valid.

You may use the following placeholders in the message:

  • {property}: the translated label of the property being validated.
  • {value}: the value of the property being validated.
$skipOnEmpty boolean|callable|null

Whether to skip this rule if the validated value is empty. See {@see \Yiisoft\Validator\SkipOnEmptyInterface}.

$skipOnError boolean

Whether to skip this rule if any of the previous rules gave an error. See {@see \Yiisoft\Validator\SkipOnErrorInterface}.

$when Closure|null

A callable to define a condition for applying the rule. See {@see \Yiisoft\Validator\WhenInterface}.

throws RuntimeException

If intl extension is not enabled and {@see $enableIdn} is true.

                public function __construct(
    string $pattern = '/^{schemes}:\/\/(([a-zA-Z0-9][a-zA-Z0-9_-]*)(\.[a-zA-Z0-9][a-zA-Z0-9_-]*)+)(?::\d{1,5})?([?\/#].*$|$)/',
    private array $validSchemes = ['http', 'https'],
    private bool $enableIdn = false,
    private string $incorrectInputMessage = '{Property} must be a string. {type} given.',
    private string $message = '{Property} is not a valid URL.',
    bool|callable|null $skipOnEmpty = null,
    private bool $skipOnError = false,
    private ?Closure $when = null,
) {
    $pattern = $this->preparePattern($pattern);
    if ($pattern === '') {
        throw new InvalidArgumentException('Pattern can\'t be empty.');
    }
    /**
     * @psalm-var non-empty-string $pattern
     */
    $this->pattern = $pattern;
    if ($enableIdn && !function_exists('idn_to_ascii')) {
        // Tested via separate CI configuration (see ".github/workflows/build.yml").
        // @codeCoverageIgnoreStart
        throw new RuntimeException('In order to use IDN validation intl extension must be installed and enabled.');
        // @codeCoverageIgnoreEnd
    }
    $this->skipOnEmpty = $skipOnEmpty;
}

            
getHandler() public method

public string getHandler ( )

                public function getHandler(): string
{
    return UrlHandler::class;
}

            
getIncorrectInputMessage() public method

Get a message used when the input is incorrect.

public string getIncorrectInputMessage ( )
return string

Error message.

                public function getIncorrectInputMessage(): string
{
    return $this->incorrectInputMessage;
}

            
getMessage() public method

Get a message used when the value is not valid.

public string getMessage ( )
return string

Error message.

                public function getMessage(): string
{
    return $this->message;
}

            
getName() public method

public string getName ( )

                public function getName(): string
{
    return self::class;
}

            
getOptions() public method

public array getOptions ( )

                public function getOptions(): array
{
    return [
        'pattern' => $this->getPattern(),
        'validSchemes' => $this->validSchemes,
        'enableIdn' => $this->enableIdn,
        'incorrectInputMessage' => [
            'template' => $this->incorrectInputMessage,
            'parameters' => [],
        ],
        'message' => [
            'template' => $this->message,
            'parameters' => [],
        ],
        'skipOnEmpty' => $this->getSkipOnEmptyOption(),
        'skipOnError' => $this->skipOnError,
    ];
}

            
getPattern() public method

Get ready to use regular expression pattern applied for URL validation.

public string getPattern ( )
return string

Regular expression pattern applied for URL validation.

                public function getPattern(): string
{
    return $this->pattern;
}

            
getSkipOnEmpty() public method

Defined in: Yiisoft\Validator\Rule\Trait\SkipOnEmptyTrait::getSkipOnEmpty()

A getter for $skipOnEmpty property.

public boolean|callable|null getSkipOnEmpty ( )
return boolean|callable|null

A current raw (non-normalized) value.

                public function getSkipOnEmpty(): bool|callable|null
{
    return $this->skipOnEmpty;
}

            
getValidSchemes() public method

Get valid URI schemas.

public string[] getValidSchemes ( )
return string[]

List of URI schemes which should be considered valid. By default, http and https are considered to be valid schemes.

                public function getValidSchemes(): array
{
    return $this->validSchemes;
}

            
getWhen() public method

Defined in: Yiisoft\Validator\Rule\Trait\WhenTrait::getWhen()

A getter for $when property.

public Closure|null getWhen ( )
return Closure|null

Current value:

  • null - always apply the validation.
  • callable - apply the validation depending on a return value: true - apply, false - do not apply.

                public function getWhen(): ?Closure
{
    return $this->when;
}

            
isIdnEnabled() public method

Whether the validation process must take {@link https://en.wikipedia.org/wiki/Internationalized_domain_name IDN (internationalized domain names)} into account. false means that validation of URLs containing IDN will always fail. Note that in order to use IDN validation you have to install and enable intl PHP extension, otherwise an exception will be thrown.

public boolean isIdnEnabled ( )
return boolean

Whether to enable IDN validation.

                public function isIdnEnabled(): bool
{
    return $this->enableIdn;
}

            
shouldSkipOnError() public method

Defined in: Yiisoft\Validator\Rule\Trait\SkipOnErrorTrait::shouldSkipOnError()

A getter for $skipOnError property.

public boolean shouldSkipOnError ( )
return boolean

Current value. true means to skip the current rule when the previous one errored and false - do not skip.

                public function shouldSkipOnError(): bool
{
    return $this->skipOnError;
}

            
skipOnEmpty() public method

Defined in: Yiisoft\Validator\Rule\Trait\SkipOnEmptyTrait::skipOnEmpty()

An immutable setter to change $skipOnEmpty property.

public $this skipOnEmpty ( boolean|callable|null $value )
$value boolean|callable|null

A new value.

return $this

The new instance with a changed value.

                public function skipOnEmpty(bool|callable|null $value): static
{
    $new = clone $this;
    $new->skipOnEmpty = $value;
    return $new;
}

            
skipOnError() public method

Defined in: Yiisoft\Validator\Rule\Trait\SkipOnErrorTrait::skipOnError()

An immutable setter to change $skipOnError property.

public $this skipOnError ( boolean $value )
$value boolean

A new value. true means to skip the current rule when the previous one errored and false - do not skip.

return $this

The new instance with a changed value.

                public function skipOnError(bool $value): static
{
    $new = clone $this;
    $new->skipOnError = $value;
    return $new;
}

            
when() public method

Defined in: Yiisoft\Validator\Rule\Trait\WhenTrait::when()

An immutable setter to change $when property.

public $this when ( Closure|null $value )
$value Closure|null

A new value:

  • null - always apply the validation.
  • callable - apply the validation depending on a return value: true - apply, false - do not apply.
return $this

The new instance with a changed value.

                public function when(?Closure $value): static
{
    $new = clone $this;
    $new->when = $value;
    return $new;
}