- md5
- 86a91851929c32ea5fd52e33e6245df8
- sha1
- c44a084ffb8b3d8d05387cdcc9fc72c7e4a665f0
- sha256
- af75c5e70fafc09aabdca4b3f73bc27476c9e4f18a21affee1d42c669201069c
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Validator\Constraints;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
use Symfony\Component\Validator\Exception\UnexpectedValueException;
/**
* @author Dmitrii Poddubnyi <[email protected]>
*/
class HostnameValidator extends ConstraintValidator
{
/**
* https://tools.ietf.org/html/rfc2606.
*/
private const RESERVED_TLDS = [
'example',
'invalid',
'localhost',
'test',
];
/**
* @return void
*/
public function validate(mixed $value, Constraint $constraint)
{
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);
}
}
The same content is in 136 releases, Joomla 5.1.0-beta1 to 6.3.0-alpha1. See them all, or every version of this file.