Skip to content

Commit caf8763

Browse files
Merge branch '5.4' into 6.0
* 5.4: (46 commits) move username/password fix to non-deprecated Connection class cs fix [VarDumper] Fix dumping twig templates found in exceptions do not replace definition arguments that have not been configured fix Console tests on Windows [Validator] Add translations for CIDR constraint [Dotenv] Fix testLoadEnv() to start from a fresh context [Console] Add completion to server:dump command bug #42194 [RateLimiter] fix: sliding window policy to use microtime [Validator] Update validators.sr_Cyrl.xlf [Validator] Update validators.sr_Latn.xlf Add suggestions for the option 'format' of lints commands: twig, yaml and xliff [VarDumper] Add support for Fiber uzb translation Update validators.uz.xlf Fix logging of impersonator introduced in 5.3 [Console] Add show proxified command class in completion debug skip command completion tests with older Symfony Console versions [Uid] Allow use autocompletion [Console] Add completion to messenger:setup-transports command ...
2 parents 9adf74a + 9240232 commit caf8763

16 files changed

+615
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ CHANGELOG
1212
5.4
1313
---
1414

15+
* Add a `Cidr` constraint to validate CIDR notations
1516
* Add a `CssColor` constraint to validate CSS colors
1617
* Add support for `ConstraintViolationList::createFromMessage()`
1718
* Add error's uid to `Count` and `Length` constraints with "exactly" option enabled

Constraints/Cidr.php

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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\Constraint;
15+
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
16+
17+
/**
18+
* Validates that a value is a valid CIDR notation.
19+
*
20+
* @Annotation
21+
* @Target({"PROPERTY", "METHOD", "ANNOTATION"})
22+
*
23+
* @author Sorin Pop <[email protected]>
24+
* @author Calin Bolea <[email protected]>
25+
*/
26+
#[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]
27+
class Cidr extends Constraint
28+
{
29+
public const INVALID_CIDR_ERROR = '5649e53a-5afb-47c5-a360-ffbab3be8567';
30+
public const OUT_OF_RANGE_ERROR = 'b9f14a51-acbd-401a-a078-8c6b204ab32f';
31+
32+
protected static $errorNames = [
33+
self::INVALID_CIDR_ERROR => 'INVALID_CIDR_ERROR',
34+
self::OUT_OF_RANGE_ERROR => 'OUT_OF_RANGE_VIOLATION',
35+
];
36+
37+
private const NET_MAXES = [
38+
Ip::ALL => 128,
39+
Ip::V4 => 32,
40+
Ip::V6 => 128,
41+
];
42+
43+
public $version = Ip::ALL;
44+
45+
public $message = 'This value is not a valid CIDR notation.';
46+
47+
public $netmaskRangeViolationMessage = 'The value of the netmask should be between {{ min }} and {{ max }}.';
48+
49+
public $netmaskMin = 0;
50+
51+
public $netmaskMax;
52+
53+
public function __construct(
54+
array $options = null,
55+
string $version = null,
56+
int $netmaskMin = null,
57+
int $netmaskMax = null,
58+
string $message = null,
59+
array $groups = null,
60+
$payload = null
61+
) {
62+
$this->version = $version ?? $options['version'] ?? $this->version;
63+
64+
if (!\in_array($this->version, array_keys(self::NET_MAXES))) {
65+
throw new ConstraintDefinitionException(sprintf('The option "version" must be one of "%s".', implode('", "', array_keys(self::NET_MAXES))));
66+
}
67+
68+
$this->netmaskMin = $netmaskMin ?? $options['netmaskMin'] ?? $this->netmaskMin;
69+
$this->netmaskMax = $netmaskMax ?? $options['netmaskMax'] ?? self::NET_MAXES[$this->version];
70+
$this->message = $message ?? $this->message;
71+
72+
unset($options['netmaskMin'], $options['netmaskMax'], $options['version']);
73+
74+
if ($this->netmaskMin < 0 || $this->netmaskMax > self::NET_MAXES[$this->version] || $this->netmaskMin > $this->netmaskMax) {
75+
throw new ConstraintDefinitionException(sprintf('The netmask range must be between 0 and %d.', self::NET_MAXES[$this->version]));
76+
}
77+
78+
parent::__construct($options, $groups, $payload);
79+
}
80+
}

Constraints/CidrValidator.php

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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\Constraint;
15+
use Symfony\Component\Validator\ConstraintValidator;
16+
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
17+
use Symfony\Component\Validator\Exception\UnexpectedValueException;
18+
19+
class CidrValidator extends ConstraintValidator
20+
{
21+
public function validate($value, Constraint $constraint): void
22+
{
23+
if (!$constraint instanceof Cidr) {
24+
throw new UnexpectedTypeException($constraint, Cidr::class);
25+
}
26+
27+
if (null === $value || '' === $value) {
28+
return;
29+
}
30+
31+
if (!\is_string($value)) {
32+
throw new UnexpectedValueException($value, 'string');
33+
}
34+
35+
$cidrParts = explode('/', $value, 2);
36+
37+
if (!isset($cidrParts[1])
38+
|| !ctype_digit($cidrParts[1])
39+
|| '' === $cidrParts[0]
40+
) {
41+
$this->context
42+
->buildViolation($constraint->message)
43+
->setCode(Cidr::INVALID_CIDR_ERROR)
44+
->addViolation();
45+
46+
return;
47+
}
48+
49+
$ipAddress = $cidrParts[0];
50+
$netmask = (int) $cidrParts[1];
51+
52+
$validV4 = Ip::V6 !== $constraint->version
53+
&& filter_var($ipAddress, \FILTER_VALIDATE_IP, \FILTER_FLAG_IPV4)
54+
&& $netmask <= 32;
55+
56+
$validV6 = Ip::V4 !== $constraint->version
57+
&& filter_var($ipAddress, \FILTER_VALIDATE_IP, \FILTER_FLAG_IPV6);
58+
59+
if (!$validV4 && !$validV6) {
60+
$this->context
61+
->buildViolation($constraint->message)
62+
->setCode(Cidr::INVALID_CIDR_ERROR)
63+
->addViolation();
64+
65+
return;
66+
}
67+
68+
if ($netmask < $constraint->netmaskMin || $netmask > $constraint->netmaskMax) {
69+
$this->context
70+
->buildViolation($constraint->netmaskRangeViolationMessage)
71+
->setParameter('{{ min }}', $constraint->netmaskMin)
72+
->setParameter('{{ max }}', $constraint->netmaskMax)
73+
->setCode(Cidr::OUT_OF_RANGE_ERROR)
74+
->addViolation();
75+
}
76+
}
77+
}

Resources/translations/validators.da.xlf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,10 @@
390390
<source>This value should be a valid expression.</source>
391391
<target>Værdien skal være et gyldigt udtryk.</target>
392392
</trans-unit>
393+
<trans-unit id="101">
394+
<source>This value is not a valid CSS color.</source>
395+
<target>Værdien skal være en gyldig CSS farve.</target>
396+
</trans-unit>
393397
</body>
394398
</file>
395399
</xliff>

Resources/translations/validators.de.xlf

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,14 @@
394394
<source>This value is not a valid CSS color.</source>
395395
<target>Dieser Wert ist keine gültige CSS-Farbe.</target>
396396
</trans-unit>
397+
<trans-unit id="102">
398+
<source>This value is not a valid CIDR notation.</source>
399+
<target>Dieser Wert entspricht nicht der CIDR-Notation.</target>
400+
</trans-unit>
401+
<trans-unit id="103">
402+
<source>The value of the netmask should be between {{ min }} and {{ max }}.</source>
403+
<target>Der Wert der Subnetzmaske sollte zwischen {{ min }} und {{ max }} liegen.</target>
404+
</trans-unit>
397405
</body>
398406
</file>
399407
</xliff>

Resources/translations/validators.el.xlf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,10 @@
390390
<source>This value should be a valid expression.</source>
391391
<target>Αυτή η τιμή θα πρέπει να είναι μία έγκυρη έκφραση.</target>
392392
</trans-unit>
393+
<trans-unit id="101">
394+
<source>This value is not a valid CSS color.</source>
395+
<target>Αυτή η τιμή δεν είναι έγκυρο χρώμα CSS.</target>
396+
</trans-unit>
393397
</body>
394398
</file>
395399
</xliff>

Resources/translations/validators.en.xlf

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,14 @@
394394
<source>This value is not a valid CSS color.</source>
395395
<target>This value is not a valid CSS color.</target>
396396
</trans-unit>
397+
<trans-unit id="102">
398+
<source>This value is not a valid CIDR notation.</source>
399+
<target>This value is not a valid CIDR notation.</target>
400+
</trans-unit>
401+
<trans-unit id="103">
402+
<source>The value of the netmask should be between {{ min }} and {{ max }}.</source>
403+
<target>The value of the netmask should be between {{ min }} and {{ max }}.</target>
404+
</trans-unit>
397405
</body>
398406
</file>
399407
</xliff>

Resources/translations/validators.fr.xlf

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,14 @@
394394
<source>This value is not a valid CSS color.</source>
395395
<target>Cette valeur n'est pas une couleur CSS valide.</target>
396396
</trans-unit>
397+
<trans-unit id="102">
398+
<source>This value is not a valid CIDR notation.</source>
399+
<target>Cette valeur n'est pas une notation CIDR valide.</target>
400+
</trans-unit>
401+
<trans-unit id="103">
402+
<source>The value of the netmask should be between {{ min }} and {{ max }}.</source>
403+
<target>La valeur du masque de réseau doit être comprise entre {{ min }} et {{ max }}.</target>
404+
</trans-unit>
397405
</body>
398406
</file>
399407
</xliff>

Resources/translations/validators.hu.xlf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,10 @@
390390
<source>This value should be a valid expression.</source>
391391
<target>Ennek az értéknek érvényes kifejezésnek kell lennie.</target>
392392
</trans-unit>
393+
<trans-unit id="101">
394+
<source>This value is not a valid CSS color.</source>
395+
<target>Ez az érték nem egy érvényes CSS szín.</target>
396+
</trans-unit>
393397
</body>
394398
</file>
395399
</xliff>

Resources/translations/validators.lv.xlf

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -340,15 +340,15 @@
340340
</trans-unit>
341341
<trans-unit id="88">
342342
<source>This value should be positive.</source>
343-
<target>Šai vērtībāi jābūt pozitīvai.</target>
343+
<target>Šai vērtībai jābūt pozitīvai.</target>
344344
</trans-unit>
345345
<trans-unit id="89">
346346
<source>This value should be either positive or zero.</source>
347-
<target>Šai vērtībāi jābūt pozitīvai vai vienādai ar nulli.</target>
347+
<target>Šai vērtībai jābūt pozitīvai vai vienādai ar nulli.</target>
348348
</trans-unit>
349349
<trans-unit id="90">
350350
<source>This value should be negative.</source>
351-
<target>Šai vērtībāi jābūt negatīvai.</target>
351+
<target>Šai vērtībai jābūt negatīvai.</target>
352352
</trans-unit>
353353
<trans-unit id="91">
354354
<source>This value should be either negative or zero.</source>

0 commit comments

Comments
 (0)