Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace Symfony\Component\Validator\Constraints; Chris@0: Chris@0: use Symfony\Component\Validator\Constraint; Chris@0: use Symfony\Component\Validator\Exception\ConstraintDefinitionException; Chris@0: Chris@0: /** Chris@0: * Validates that a value is a valid IP address. Chris@0: * Chris@0: * @Annotation Chris@0: * @Target({"PROPERTY", "METHOD", "ANNOTATION"}) Chris@0: * Chris@0: * @author Bernhard Schussek Chris@0: * @author Joseph Bielawski Chris@0: */ Chris@0: class Ip extends Constraint Chris@0: { Chris@0: const V4 = '4'; Chris@0: const V6 = '6'; Chris@0: const ALL = 'all'; Chris@0: Chris@0: // adds FILTER_FLAG_NO_PRIV_RANGE flag (skip private ranges) Chris@0: const V4_NO_PRIV = '4_no_priv'; Chris@0: const V6_NO_PRIV = '6_no_priv'; Chris@0: const ALL_NO_PRIV = 'all_no_priv'; Chris@0: Chris@0: // adds FILTER_FLAG_NO_RES_RANGE flag (skip reserved ranges) Chris@0: const V4_NO_RES = '4_no_res'; Chris@0: const V6_NO_RES = '6_no_res'; Chris@0: const ALL_NO_RES = 'all_no_res'; Chris@0: Chris@0: // adds FILTER_FLAG_NO_PRIV_RANGE and FILTER_FLAG_NO_RES_RANGE flags (skip both) Chris@0: const V4_ONLY_PUBLIC = '4_public'; Chris@0: const V6_ONLY_PUBLIC = '6_public'; Chris@0: const ALL_ONLY_PUBLIC = 'all_public'; Chris@0: Chris@0: const INVALID_IP_ERROR = 'b1b427ae-9f6f-41b0-aa9b-84511fbb3c5b'; Chris@0: Chris@17: protected static $versions = [ Chris@0: self::V4, Chris@0: self::V6, Chris@0: self::ALL, Chris@0: Chris@0: self::V4_NO_PRIV, Chris@0: self::V6_NO_PRIV, Chris@0: self::ALL_NO_PRIV, Chris@0: Chris@0: self::V4_NO_RES, Chris@0: self::V6_NO_RES, Chris@0: self::ALL_NO_RES, Chris@0: Chris@0: self::V4_ONLY_PUBLIC, Chris@0: self::V6_ONLY_PUBLIC, Chris@0: self::ALL_ONLY_PUBLIC, Chris@17: ]; Chris@0: Chris@17: protected static $errorNames = [ Chris@0: self::INVALID_IP_ERROR => 'INVALID_IP_ERROR', Chris@17: ]; Chris@0: Chris@0: public $version = self::V4; Chris@0: Chris@0: public $message = 'This is not a valid IP address.'; Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function __construct($options = null) Chris@0: { Chris@0: parent::__construct($options); Chris@0: Chris@17: if (!\in_array($this->version, self::$versions)) { Chris@0: throw new ConstraintDefinitionException(sprintf('The option "version" must be one of "%s"', implode('", "', self::$versions))); Chris@0: } Chris@0: } Chris@0: }