Вход Регистрация
Файл: symfony-2.7/src/Symfony/Component/Validator/Constraints/UrlValidator.php
Строк: 116
<?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 SymfonyComponentValidatorConstraints;

use 
SymfonyComponentValidatorContextExecutionContextInterface;
use 
SymfonyComponentValidatorConstraint;
use 
SymfonyComponentValidatorConstraintValidator;
use 
SymfonyComponentValidatorExceptionUnexpectedTypeException;

/**
 * @author Bernhard Schussek <bschussek@gmail.com>
 *
 * @api
 */
class UrlValidator extends ConstraintValidator
{
    const 
PATTERN '~^
            (%s)://                                 # protocol
            (([pLpN-]+:)?([pLpN-]+)@)?          # basic auth
            (
                ([pLpNpS-.])+(.?([pL]|xn--[pLpN-]+)+.?) # 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]{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,1}(?:(?:[0-9a-f]{1,4})))?::(?:(?:(?:[0-9a-f]{1,4})):){3})(?:(?:(?:(?:(?:[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,2}(?:(?:[0-9a-f]{1,4})))?::(?:(?:(?:[0-9a-f]{1,4})):){2})(?:(?:(?:(?:(?:[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,3}(?:(?:[0-9a-f]{1,4})))?::(?:(?:[0-9a-f]{1,4})):)(?:(?:(?:(?:(?:[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,4}(?:(?:[0-9a-f]{1,4})))?::)(?:(?:(?:(?:(?:[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,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 or a / with something
        $~ixu'
;

    
/**
     * {@inheritdoc}
     */
    
public function validate($valueConstraint $constraint)
    {
        if (!
$constraint instanceof Url) {
            throw new 
UnexpectedTypeException($constraint__NAMESPACE__.'Url');
        }

        if (
null === $value || '' === $value) {
            return;
        }

        if (!
is_scalar($value) && !(is_object($value) && method_exists($value'__toString'))) {
            throw new 
UnexpectedTypeException($value'string');
        }

        
$value = (string) $value;
        
$pattern sprintf(static::PATTERNimplode('|'$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($valuePHP_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();
                }
            }
        }
    }
}
Онлайн: 1
Реклама