This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/src/Symfony/Component/Validator/Constraints/IpValidator.php
Tobias Schultze 8573385a83 Merge branch '2.3' into 2.7
Conflicts:
	src/Symfony/Component/ClassLoader/ApcUniversalClassLoader.php
	src/Symfony/Component/ClassLoader/DebugClassLoader.php
	src/Symfony/Component/ClassLoader/UniversalClassLoader.php
	src/Symfony/Component/Console/Command/Command.php
	src/Symfony/Component/DependencyInjection/Definition.php
	src/Symfony/Component/DependencyInjection/DefinitionDecorator.php
	src/Symfony/Component/EventDispatcher/Event.php
	src/Symfony/Component/Filesystem/Exception/IOException.php
	src/Symfony/Component/HttpFoundation/File/File.php
	src/Symfony/Component/HttpFoundation/ResponseHeaderBag.php
	src/Symfony/Component/HttpFoundation/Session/SessionInterface.php
	src/Symfony/Component/HttpFoundation/StreamedResponse.php
	src/Symfony/Component/HttpKernel/Controller/ControllerResolver.php
	src/Symfony/Component/HttpKernel/Controller/ControllerResolverInterface.php
	src/Symfony/Component/HttpKernel/HttpKernel.php
	src/Symfony/Component/HttpKernel/Kernel.php
	src/Symfony/Component/HttpKernel/KernelInterface.php
	src/Symfony/Component/HttpKernel/Log/LoggerInterface.php
	src/Symfony/Component/HttpKernel/Log/NullLogger.php
	src/Symfony/Component/Process/Process.php
	src/Symfony/Component/Routing/RequestContext.php
	src/Symfony/Component/Routing/Route.php
	src/Symfony/Component/Templating/EngineInterface.php
	src/Symfony/Component/Templating/PhpEngine.php
	src/Symfony/Component/Templating/TemplateNameParser.php
	src/Symfony/Component/Templating/TemplateReference.php
	src/Symfony/Component/Templating/TemplateReferenceInterface.php
	src/Symfony/Component/Translation/IdentityTranslator.php
	src/Symfony/Component/Translation/Translator.php
	src/Symfony/Component/Validator/ConstraintViolationInterface.php
	src/Symfony/Component/Validator/Constraints/False.php
	src/Symfony/Component/Validator/Constraints/FalseValidator.php
	src/Symfony/Component/Validator/Constraints/GroupSequence.php
	src/Symfony/Component/Validator/Constraints/Image.php
	src/Symfony/Component/Validator/Constraints/Null.php
	src/Symfony/Component/Validator/Constraints/NullValidator.php
	src/Symfony/Component/Validator/Constraints/True.php
	src/Symfony/Component/Validator/Constraints/TrueValidator.php
	src/Symfony/Component/Validator/ExecutionContextInterface.php
	src/Symfony/Component/Validator/ValidatorInterface.php
2015-09-29 14:06:14 +02:00

109 lines
3.2 KiB
PHP

<?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;
/**
* Validates whether a value is a valid IP address.
*
* @author Bernhard Schussek <bschussek@gmail.com>
* @author Joseph Bielawski <stloyd@gmail.com>
*/
class IpValidator extends ConstraintValidator
{
/**
* {@inheritdoc}
*/
public function validate($value, Constraint $constraint)
{
if (!$constraint instanceof Ip) {
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Ip');
}
if (null === $value || '' === $value) {
return;
}
if (!is_scalar($value) && !(is_object($value) && method_exists($value, '__toString'))) {
throw new UnexpectedTypeException($value, 'string');
}
$value = (string) $value;
switch ($constraint->version) {
case Ip::V4:
$flag = FILTER_FLAG_IPV4;
break;
case Ip::V6:
$flag = FILTER_FLAG_IPV6;
break;
case Ip::V4_NO_PRIV:
$flag = FILTER_FLAG_IPV4 | FILTER_FLAG_NO_PRIV_RANGE;
break;
case Ip::V6_NO_PRIV:
$flag = FILTER_FLAG_IPV6 | FILTER_FLAG_NO_PRIV_RANGE;
break;
case Ip::ALL_NO_PRIV:
$flag = FILTER_FLAG_NO_PRIV_RANGE;
break;
case Ip::V4_NO_RES:
$flag = FILTER_FLAG_IPV4 | FILTER_FLAG_NO_RES_RANGE;
break;
case Ip::V6_NO_RES:
$flag = FILTER_FLAG_IPV6 | FILTER_FLAG_NO_RES_RANGE;
break;
case Ip::ALL_NO_RES:
$flag = FILTER_FLAG_NO_RES_RANGE;
break;
case Ip::V4_ONLY_PUBLIC:
$flag = FILTER_FLAG_IPV4 | FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE;
break;
case Ip::V6_ONLY_PUBLIC:
$flag = FILTER_FLAG_IPV6 | FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE;
break;
case Ip::ALL_ONLY_PUBLIC:
$flag = FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE;
break;
default:
$flag = null;
break;
}
if (!filter_var($value, FILTER_VALIDATE_IP, $flag)) {
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();
}
}
}
}