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