namespace Symfony\Component\Validator\Constraints;
-use Symfony\Component\Validator\Context\ExecutionContextInterface;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Url');
}
- if (null === $value) {
+ if (null === $value || '' === $value) {
return;
}
$pattern = sprintf(static::PATTERN, implode('|', $constraint->protocols));
if (!preg_match($pattern, $value)) {
- if ($this->context instanceof ExecutionContextInterface) {
- $this->context->buildViolation($constraint->message)
- ->setParameter('{{ value }}', $this->formatValue($value))
- ->setCode(Url::INVALID_URL_ERROR)
- ->addViolation();
- } else {
- $this->buildViolation($constraint->message)
- ->setParameter('{{ value }}', $this->formatValue($value))
- ->setCode(Url::INVALID_URL_ERROR)
- ->addViolation();
- }
+ $this->context->buildViolation($constraint->message)
+ ->setParameter('{{ value }}', $this->formatValue($value))
+ ->setCode(Url::INVALID_URL_ERROR)
+ ->addViolation();
return;
}
if ($constraint->checkDNS) {
$host = parse_url($value, PHP_URL_HOST);
- if (!checkdnsrr($host, 'ANY')) {
- if ($this->context instanceof ExecutionContextInterface) {
- $this->context->buildViolation($constraint->dnsMessage)
- ->setParameter('{{ value }}', $this->formatValue($host))
- ->setCode(Url::INVALID_URL_ERROR)
- ->addViolation();
- } else {
- $this->buildViolation($constraint->dnsMessage)
- ->setParameter('{{ value }}', $this->formatValue($host))
- ->setCode(Url::INVALID_URL_ERROR)
- ->addViolation();
- }
+ if (!is_string($host) || !checkdnsrr($host, 'ANY')) {
+ $this->context->buildViolation($constraint->dnsMessage)
+ ->setParameter('{{ value }}', $this->formatValue($host))
+ ->setCode(Url::INVALID_URL_ERROR)
+ ->addViolation();
}
}
}