13
13
14
14
use Symfony \Component \Validator \Constraint ;
15
15
use Symfony \Component \Validator \Exception \ConstraintDefinitionException ;
16
+ use Symfony \Component \Validator \Exception \InvalidArgumentException ;
16
17
17
18
/**
18
19
* Validates that a value is a valid CIDR notation.
21
22
*
22
23
* @author Sorin Pop <[email protected] >
23
24
* @author Calin Bolea <[email protected] >
25
+ * @author Ninos Ego <[email protected] >
24
26
*/
25
27
#[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE )]
26
28
class Cidr extends Constraint
@@ -34,9 +36,33 @@ class Cidr extends Constraint
34
36
];
35
37
36
38
private const NET_MAXES = [
37
- Ip::ALL => 128 ,
38
39
Ip::V4 => 32 ,
39
40
Ip::V6 => 128 ,
41
+ Ip::ALL => 128 ,
42
+
43
+ Ip::V4_NO_PUBLIC => 32 ,
44
+ Ip::V6_NO_PUBLIC => 128 ,
45
+ Ip::ALL_NO_PUBLIC => 128 ,
46
+
47
+ Ip::V4_NO_PRIVATE => 32 ,
48
+ Ip::V6_NO_PRIVATE => 128 ,
49
+ Ip::ALL_NO_PRIVATE => 128 ,
50
+
51
+ Ip::V4_NO_RESERVED => 32 ,
52
+ Ip::V6_NO_RESERVED => 128 ,
53
+ Ip::ALL_NO_RESERVED => 128 ,
54
+
55
+ Ip::V4_ONLY_PUBLIC => 32 ,
56
+ Ip::V6_ONLY_PUBLIC => 128 ,
57
+ Ip::ALL_ONLY_PUBLIC => 128 ,
58
+
59
+ Ip::V4_ONLY_PRIVATE => 32 ,
60
+ Ip::V6_ONLY_PRIVATE => 128 ,
61
+ Ip::ALL_ONLY_PRIVATE => 128 ,
62
+
63
+ Ip::V4_ONLY_RESERVED => 32 ,
64
+ Ip::V6_ONLY_RESERVED => 128 ,
65
+ Ip::ALL_ONLY_RESERVED => 128 ,
40
66
];
41
67
42
68
public string $ version = Ip::ALL ;
@@ -45,13 +71,9 @@ class Cidr extends Constraint
45
71
public int $ netmaskMin = 0 ;
46
72
public int $ netmaskMax ;
47
73
48
- /**
49
- * @param array<string,mixed>|null $options
50
- * @param string|null $version The CIDR version to validate (4, 6 or all, defaults to all)
51
- * @param int|null $netmaskMin The lowest valid for a valid netmask (defaults to 0)
52
- * @param int|null $netmaskMax The biggest valid for a valid netmask (defaults to 32 for IPv4, 128 for IPv6)
53
- * @param string[]|null $groups
54
- */
74
+ /** @var callable|null */
75
+ public $ normalizer ;
76
+
55
77
public function __construct (
56
78
?array $ options = null ,
57
79
?string $ version = null ,
@@ -60,6 +82,7 @@ public function __construct(
60
82
?string $ message = null ,
61
83
?array $ groups = null ,
62
84
$ payload = null ,
85
+ ?callable $ normalizer = null ,
63
86
) {
64
87
$ this ->version = $ version ?? $ options ['version ' ] ?? $ this ->version ;
65
88
@@ -70,13 +93,18 @@ public function __construct(
70
93
$ this ->netmaskMin = $ netmaskMin ?? $ options ['netmaskMin ' ] ?? $ this ->netmaskMin ;
71
94
$ this ->netmaskMax = $ netmaskMax ?? $ options ['netmaskMax ' ] ?? self ::NET_MAXES [$ this ->version ];
72
95
$ this ->message = $ message ?? $ this ->message ;
96
+ $ this ->normalizer = $ normalizer ?? $ this ->normalizer ;
73
97
74
98
unset($ options ['netmaskMin ' ], $ options ['netmaskMax ' ], $ options ['version ' ]);
75
99
76
100
if ($ this ->netmaskMin < 0 || $ this ->netmaskMax > self ::NET_MAXES [$ this ->version ] || $ this ->netmaskMin > $ this ->netmaskMax ) {
77
101
throw new ConstraintDefinitionException (sprintf ('The netmask range must be between 0 and %d. ' , self ::NET_MAXES [$ this ->version ]));
78
102
}
79
103
104
+ if (null !== $ this ->normalizer && !\is_callable ($ this ->normalizer )) {
105
+ throw new InvalidArgumentException (sprintf ('The "normalizer" option must be a valid callable ("%s" given). ' , get_debug_type ($ this ->normalizer )));
106
+ }
107
+
80
108
parent ::__construct ($ options , $ groups , $ payload );
81
109
}
82
110
}
0 commit comments