class HostnameValidator
@author Dmitrii Poddubnyi <dpoddubny@gmail.com>
Hierarchy
- class \Symfony\Component\Validator\ConstraintValidator implements \Symfony\Component\Validator\ConstraintValidatorInterface
- class \Symfony\Component\Validator\Constraints\HostnameValidator extends \Symfony\Component\Validator\ConstraintValidator
Expanded class hierarchy of HostnameValidator
File
-
vendor/
symfony/ validator/ Constraints/ HostnameValidator.php, line 22
Namespace
Symfony\Component\Validator\ConstraintsView source
class HostnameValidator extends ConstraintValidator {
/**
* https://tools.ietf.org/html/rfc2606.
*/
private const RESERVED_TLDS = [
'example',
'invalid',
'localhost',
'test',
];
public function validate(mixed $value, Constraint $constraint) : void {
if (!$constraint instanceof Hostname) {
throw new UnexpectedTypeException($constraint, Hostname::class);
}
if (null === $value || '' === $value) {
return;
}
if (!\is_scalar($value) && !$value instanceof \Stringable) {
throw new UnexpectedValueException($value, 'string');
}
$value = (string) $value;
if ('' === $value) {
return;
}
if (!$this->isValid($value) || $constraint->requireTld && !$this->hasValidTld($value)) {
$this->context
->buildViolation($constraint->message)
->setParameter('{{ value }}', $this->formatValue($value))
->setCode(Hostname::INVALID_HOSTNAME_ERROR)
->addViolation();
}
}
private function isValid(string $domain) : bool {
return false !== filter_var($domain, \FILTER_VALIDATE_DOMAIN, \FILTER_FLAG_HOSTNAME);
}
private function hasValidTld(string $domain) : bool {
return str_contains($domain, '.') && !\in_array(substr($domain, strrpos($domain, '.') + 1), self::RESERVED_TLDS, true);
}
}
Members
Title Sort descending | Modifiers | Object type | Summary | Overriden Title |
---|---|---|---|---|
ConstraintValidator::$context | protected | property | ||
ConstraintValidator::formatTypeOf | protected | function | Returns a string representation of the type of the value. | |
ConstraintValidator::formatValue | protected | function | Returns a string representation of the value. | |
ConstraintValidator::formatValues | protected | function | Returns a string representation of a list of values. | |
ConstraintValidator::initialize | public | function | Initializes the constraint validator. | Overrides ConstraintValidatorInterface::initialize |
ConstraintValidator::OBJECT_TO_STRING | public | constant | Whether to cast objects with a "__toString()" method to strings. | |
ConstraintValidator::PRETTY_DATE | public | constant | Whether to format {@link \DateTime} objects, either with the {@link \IntlDateFormatter} (if it is available) or as RFC-3339 dates ("Y-m-d H:i:s"). |
|
HostnameValidator::hasValidTld | private | function | ||
HostnameValidator::isValid | private | function | ||
HostnameValidator::RESERVED_TLDS | private | constant | https://tools.ietf.org/html/rfc2606. | |
HostnameValidator::validate | public | function | Checks if the passed value is valid. | Overrides ConstraintValidatorInterface::validate |