vendor/symfony/src/Symfony/Component/Validator/Constraints/Ip.php
changeset 0 7f95f8617b0b
equal deleted inserted replaced
-1:000000000000 0:7f95f8617b0b
       
     1 <?php
       
     2 
       
     3 /*
       
     4  * This file is part of the Symfony package.
       
     5  *
       
     6  * (c) Fabien Potencier <fabien@symfony.com>
       
     7  *
       
     8  * For the full copyright and license information, please view the LICENSE
       
     9  * file that was distributed with this source code.
       
    10  */
       
    11 
       
    12 namespace Symfony\Component\Validator\Constraints;
       
    13 
       
    14 use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
       
    15 use Symfony\Component\Validator\Constraint;
       
    16 
       
    17 /**
       
    18  * Validates that a value is a valid IP address
       
    19  *
       
    20  * @Annotation
       
    21  * @author Bernhard Schussek <bernhard.schussek@symfony.com>
       
    22  * @author Joseph Bielawski <stloyd@gmail.com>
       
    23  *
       
    24  * @api
       
    25  */
       
    26 class Ip extends Constraint
       
    27 {
       
    28     const V4 = '4';
       
    29     const V6 = '6';
       
    30     const ALL = 'all';
       
    31 
       
    32     // adds FILTER_FLAG_NO_PRIV_RANGE flag (skip private ranges)
       
    33     const V4_NO_PRIV = '4_no_priv';
       
    34     const V6_NO_PRIV = '6_no_priv';
       
    35     const ALL_NO_PRIV = 'all_no_priv';
       
    36 
       
    37     // adds FILTER_FLAG_NO_RES_RANGE flag (skip reserved ranges)
       
    38     const V4_NO_RES = '4_no_res';
       
    39     const V6_NO_RES = '6_no_res';
       
    40     const ALL_NO_RES = 'all_no_res';
       
    41 
       
    42     // adds FILTER_FLAG_NO_PRIV_RANGE and FILTER_FLAG_NO_RES_RANGE flags (skip both)
       
    43     const V4_ONLY_PUBLIC = '4_public';
       
    44     const V6_ONLY_PUBLIC = '6_public';
       
    45     const ALL_ONLY_PUBLIC = 'all_public';
       
    46 
       
    47     static protected $versions = array(
       
    48         self::V4,
       
    49         self::V6,
       
    50         self::ALL,
       
    51 
       
    52         self::V4_NO_PRIV,
       
    53         self::V6_NO_PRIV,
       
    54         self::ALL_NO_PRIV,
       
    55 
       
    56         self::V4_NO_RES,
       
    57         self::V6_NO_RES,
       
    58         self::ALL_NO_RES,
       
    59 
       
    60         self::V4_ONLY_PUBLIC,
       
    61         self::V6_ONLY_PUBLIC,
       
    62         self::ALL_ONLY_PUBLIC,
       
    63     );
       
    64 
       
    65     public $version = self::V4;
       
    66 
       
    67     public $message = 'This is not a valid IP address';
       
    68 
       
    69     /**
       
    70      * {@inheritDoc}
       
    71      */
       
    72     public function __construct($options = null)
       
    73     {
       
    74         parent::__construct($options);
       
    75 
       
    76         if (!in_array($this->version, self::$versions)) {
       
    77             throw new ConstraintDefinitionException(sprintf('The option "version" must be one of "%s"', implode('", "', self::$versions)));
       
    78         }
       
    79     }
       
    80 }