You are here

UrlValidator.php in Zircon Profile 8

Same filename and directory in other branches
  1. 8.0 vendor/symfony/validator/Constraints/UrlValidator.php

File

vendor/symfony/validator/Constraints/UrlValidator.php
View source
<?php

/*
 * This file is part of the Symfony package.
 *
 * (c) Fabien Potencier <fabien@symfony.com>
 *
 * 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\Context\ExecutionContextInterface;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;

/**
 * @author Bernhard Schussek <bschussek@gmail.com>
 */
class UrlValidator extends ConstraintValidator {
  const PATTERN = '~^
            (%s)://                                 # protocol
            (([\\pL\\pN-]+:)?([\\pL\\pN-]+)@)?          # basic auth
            (
                ([\\pL\\pN\\pS-\\.])+(\\.?([\\pL]|xn\\-\\-[\\pL\\pN-]+)+\\.?) # a domain name
                    |                                              # or
                \\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}                 # a IP address
                    |                                              # or
                \\[
                    (?:(?:(?:(?:(?:(?:(?:[0-9a-f]{1,4})):){6})(?:(?:(?:(?:(?:[0-9a-f]{1,4})):(?:(?:[0-9a-f]{1,4})))|(?:(?:(?:(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9]))\\.){3}(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])))))))|(?:(?:::(?:(?:(?:[0-9a-f]{1,4})):){5})(?:(?:(?:(?:(?:[0-9a-f]{1,4})):(?:(?:[0-9a-f]{1,4})))|(?:(?:(?:(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9]))\\.){3}(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])))))))|(?:(?:(?:(?:(?:[0-9a-f]{1,4})))?::(?:(?:(?:[0-9a-f]{1,4})):){4})(?:(?:(?:(?:(?:[0-9a-f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|1[0-9]|2[0-4])?[0-9])))))))|(?:(?:(?:(?:(?:(?:[0-9a-f]{1,4})):){0,5}(?:(?:[0-9a-f]{1,4})))?::)(?:(?:[0-9a-f]{1,4})))|(?:(?:(?:(?:(?:(?:[0-9a-f]{1,4})):){0,6}(?:(?:[0-9a-f]{1,4})))?::))))
                \\]  # a IPv6 address
            )
            (:[0-9]+)?                              # a port (optional)
            (/?|/\\S+|\\?|\\#)                         # a /, nothing, a / with something, a query or a fragment
        $~ixu';

  /**
   * {@inheritdoc}
   */
  public function validate($value, Constraint $constraint) {
    if (!$constraint instanceof Url) {
      throw new UnexpectedTypeException($constraint, __NAMESPACE__ . '\\Url');
    }
    if (null === $value) {
      return;
    }
    if (!is_scalar($value) && !(is_object($value) && method_exists($value, '__toString'))) {
      throw new UnexpectedTypeException($value, 'string');
    }
    $value = (string) $value;
    if ('' === $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))
          ->addViolation();
      }
      else {
        $this
          ->buildViolation($constraint->message)
          ->setParameter('{{ value }}', $this
          ->formatValue($value))
          ->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))
            ->addViolation();
        }
        else {
          $this
            ->buildViolation($constraint->dnsMessage)
            ->setParameter('{{ value }}', $this
            ->formatValue($host))
            ->addViolation();
        }
      }
    }
  }

}

Classes

Namesort descending Description
UrlValidator @author Bernhard Schussek <bschussek@gmail.com>