Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6e2b092

Browse files
committed
Simplify constraint usage
1 parent 25bb90d commit 6e2b092

File tree

3 files changed

+45
-75
lines changed

3 files changed

+45
-75
lines changed

src/Symfony/Component/Validator/Constraints/NoSuspiciousCharacters.php

Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -25,34 +25,22 @@ class NoSuspiciousCharacters extends Constraint
2525
{
2626
public const RESTRICTION_LEVEL_ERROR = '1ece07dc-dca2-45f1-ba47-8d7dc3a12774';
2727
public const INVISIBLE_ERROR = '6ed60e6c-179b-4e93-8a6c-667d85c6de5e';
28-
public const CHAR_LIMIT_ERROR = 'ae6e496e-b315-4fdf-bea5-657accac631d';
2928
public const MIXED_NUMBERS_ERROR = '9f01fc26-3bc4-44b1-a6b1-c08e2412053a';
3029
public const HIDDEN_OVERLAY_ERROR = '56380dc5-0476-4f04-bbaa-b68cd1c2d974';
3130

3231
protected const ERROR_NAMES = [
3332
self::RESTRICTION_LEVEL_ERROR => 'RESTRICTION_LEVEL_ERROR',
3433
self::INVISIBLE_ERROR => 'INVISIBLE_ERROR',
35-
self::CHAR_LIMIT_ERROR => 'CHAR_LIMIT_ERROR',
3634
self::MIXED_NUMBERS_ERROR => 'MIXED_NUMBERS_ERROR',
3735
self::HIDDEN_OVERLAY_ERROR => 'INVALID_CASE_ERROR',
3836
];
3937

40-
/**
41-
* Check that a string satisfies the requirements for the specified restriction level.
42-
* It defaults to {@see self::RESTRICTION_LEVEL_MODERATE} when using ICU >= 58,
43-
* and is locked to {@see self::RESTRICTION_LEVEL_SINGLE_SCRIPT} on older versions.
44-
*/
45-
public const CHECK_RESTRICTION_LEVEL = 16;
46-
4738
/**
4839
* Check a string for the presence of invisible characters such as zero-width spaces,
4940
* or character sequences that are likely not to display such as multiple occurrences of the same non-spacing mark.
5041
*/
5142
public const CHECK_INVISIBLE = 32;
5243

53-
/** Check a string contains only characters from the configured locales. */
54-
public const CHECK_CHAR_LIMIT = 64;
55-
5644
/**
5745
* Check that a string does not mix numbers from different numbering systems;
5846
* for example “8” (Digit Eight) and “৪” (Bengali Digit Four).
@@ -83,13 +71,12 @@ class NoSuspiciousCharacters extends Constraint
8371
/** @see https://unicode.org/reports/tr39/#unrestricted */
8472
public const RESTRICTION_LEVEL_NONE = 1610612736;
8573

86-
public string $restrictionLevelMessage = 'Restriction level check failed.';
87-
public string $invisibleMessage = 'Invisible check failed.';
88-
public string $charLimitMessage = 'Char limit check failed.';
89-
public string $mixedNumbersMessage = 'Mixed numbers check failed.';
90-
public string $hiddenOverlayMessage = 'Hidden overlay check failed.';
74+
public string $restrictionLevelMessage = 'This value contains characters that are not allowed by the current restriction-level.';
75+
public string $invisibleMessage = 'Using invisible characters is not allowed.';
76+
public string $mixedNumbersMessage = 'Mixing numbers from different scripts is not allowed.';
77+
public string $hiddenOverlayMessage = 'Using hidden overlay characters is not allowed.';
9178

92-
public int $checks = self::CHECK_RESTRICTION_LEVEL | self::CHECK_INVISIBLE | self::CHECK_CHAR_LIMIT | self::CHECK_MIXED_NUMBERS | self::CHECK_HIDDEN_OVERLAY;
79+
public int $checks = self::CHECK_INVISIBLE | self::CHECK_MIXED_NUMBERS | self::CHECK_HIDDEN_OVERLAY;
9380
public ?int $restrictionLevel = null;
9481
public ?array $locales = null;
9582

@@ -101,7 +88,6 @@ public function __construct(
10188
array $options = null,
10289
string $restrictionLevelMessage = null,
10390
string $invisibleMessage = null,
104-
string $charLimitMessage = null,
10591
string $mixedNumbersMessage = null,
10692
string $hiddenOverlayMessage = null,
10793
int $checks = null,
@@ -118,19 +104,10 @@ public function __construct(
118104

119105
$this->restrictionLevelMessage ??= $restrictionLevelMessage;
120106
$this->invisibleMessage ??= $invisibleMessage;
121-
$this->charLimitMessage ??= $charLimitMessage;
122107
$this->mixedNumbersMessage ??= $mixedNumbersMessage;
123108
$this->hiddenOverlayMessage ??= $hiddenOverlayMessage;
124109
$this->checks ??= $checks;
125110
$this->restrictionLevel ??= $restrictionLevel;
126111
$this->locales ??= $locales;
127-
128-
if (method_exists(\Spoofchecker::class, 'setRestrictionLevel')) {
129-
$this->restrictionLevel ??= self::RESTRICTION_LEVEL_MODERATE;
130-
} elseif ($this->restrictionLevel && self::RESTRICTION_LEVEL_SINGLE_SCRIPT !== $this->restrictionLevel) {
131-
throw new LogicException('ICU <= 58 only supports single-script restriction level.');
132-
} else {
133-
$this->restrictionLevel = null;
134-
}
135112
}
136113
}

src/Symfony/Component/Validator/Constraints/NoSuspiciousCharactersValidator.php

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Component\Validator\Constraint;
1515
use Symfony\Component\Validator\ConstraintValidator;
16+
use Symfony\Component\Validator\Exception\LogicException;
1617
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
1718
use Symfony\Component\Validator\Exception\UnexpectedValueException;
1819

@@ -21,18 +22,22 @@
2122
*/
2223
class NoSuspiciousCharactersValidator extends ConstraintValidator
2324
{
25+
private const CHECK_RESTRICTION_LEVEL = 16;
26+
private const CHECK_SINGLE_SCRIPT = 16;
27+
private const CHECK_CHAR_LIMIT = 64;
28+
2429
private const CHECK_ERROR = [
25-
NoSuspiciousCharacters::CHECK_RESTRICTION_LEVEL => [
30+
self::CHECK_RESTRICTION_LEVEL => [
2631
'code' => NoSuspiciousCharacters::RESTRICTION_LEVEL_ERROR,
2732
'messageProperty' => 'restrictionLevelMessage',
2833
],
2934
NoSuspiciousCharacters::CHECK_INVISIBLE => [
3035
'code' => NoSuspiciousCharacters::INVISIBLE_ERROR,
3136
'messageProperty' => 'invisibleMessage',
3237
],
33-
NoSuspiciousCharacters::CHECK_CHAR_LIMIT => [
34-
'code' => NoSuspiciousCharacters::CHAR_LIMIT_ERROR,
35-
'messageProperty' => 'charLimitMessage',
38+
self::CHECK_CHAR_LIMIT => [
39+
'code' => NoSuspiciousCharacters::RESTRICTION_LEVEL_ERROR,
40+
'messageProperty' => 'restrictionLevelMessage',
3641
],
3742
NoSuspiciousCharacters::CHECK_MIXED_NUMBERS => [
3843
'code' => NoSuspiciousCharacters::MIXED_NUMBERS_ERROR,
@@ -70,20 +75,31 @@ public function validate(mixed $value, Constraint $constraint)
7075
}
7176

7277
$checker = new \Spoofchecker();
73-
74-
if ($constraint->restrictionLevel) {
75-
$checker->setRestrictionLevel($constraint->restrictionLevel);
78+
$checks = $constraint->checks;
79+
80+
if (method_exists($checker, 'setRestrictionLevel')) {
81+
$checks |= self::CHECK_RESTRICTION_LEVEL;
82+
$checker->setRestrictionLevel($constraint->restrictionLevel ?? NoSuspiciousCharacters::RESTRICTION_LEVEL_MODERATE);
83+
} elseif (NoSuspiciousCharacters::RESTRICTION_LEVEL_MINIMAL === $constraint->restrictionLevel) {
84+
$checks |= self::CHECK_CHAR_LIMIT;
85+
} elseif (NoSuspiciousCharacters::RESTRICTION_LEVEL_SINGLE_SCRIPT === $constraint->restrictionLevel) {
86+
$checks |= self::CHECK_SINGLE_SCRIPT | self::CHECK_CHAR_LIMIT;
87+
} elseif ($constraint->restrictionLevel) {
88+
throw new LogicException('You can only use one of RESTRICTION_LEVEL_NONE, RESTRICTION_LEVEL_MINIMAL or RESTRICTION_LEVEL_SINGLE_SCRIPT with intl compiled against ICU < 58.');
89+
} else {
90+
$checks |= self::CHECK_SINGLE_SCRIPT;
7691
}
92+
7793
$checker->setAllowedLocales(implode(',', $constraint->locales ?? $this->defaultLocales));
7894

79-
$checker->setChecks($constraint->checks);
95+
$checker->setChecks($checks);
8096

8197
if (!$checker->isSuspicious($value)) {
8298
return;
8399
}
84100

85101
foreach (self::CHECK_ERROR as $check => $error) {
86-
if (!($constraint->checks & $check)) {
102+
if (!($checks & $check)) {
87103
continue;
88104
}
89105

src/Symfony/Component/Validator/Tests/Constraints/NoSuspiciousCharactersValidatorTest.php

Lines changed: 15 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class NoSuspiciousCharactersValidatorTest extends ConstraintValidatorTestCase
2424
{
2525
protected function createValidator(): NoSuspiciousCharactersValidator
2626
{
27-
return new NoSuspiciousCharactersValidator(['en']);
27+
return new NoSuspiciousCharactersValidator();
2828
}
2929

3030
/**
@@ -44,26 +44,13 @@ public static function provideNonSuspiciousStrings(): iterable
4444
['restrictionLevel' => NoSuspiciousCharacters::RESTRICTION_LEVEL_SINGLE_SCRIPT],
4545
];
4646

47-
yield 'Empty profile cannot make CHAR_LIMIT fail' => [
47+
yield 'RESTRICTION_LEVEL_MINIMAL cannot fail without configured locales' => [
4848
'àㄚԱπ৪',
4949
[
50-
'checks' => NoSuspiciousCharacters::CHECK_CHAR_LIMIT,
50+
'restrictionLevel' => NoSuspiciousCharacters::RESTRICTION_LEVEL_MINIMAL,
5151
'locales' => [],
5252
],
5353
];
54-
55-
yield 'Override setAllowedLocales implicit CHAR_LIMIT check' => [
56-
'Ա',
57-
['checks' => NoSuspiciousCharacters::CHECK_HIDDEN_OVERLAY],
58-
];
59-
60-
yield 'Override setRestrictionLevel implicit RESTRICTION_LEVEL check' => [
61-
'à',
62-
[
63-
'checks' => 0,
64-
'restrictionLevel' => NoSuspiciousCharacters::RESTRICTION_LEVEL_ASCII,
65-
],
66-
];
6754
}
6855

6956
/**
@@ -85,7 +72,7 @@ public static function provideSuspiciousStrings(): iterable
8572
'à',
8673
['restrictionLevel' => NoSuspiciousCharacters::RESTRICTION_LEVEL_ASCII],
8774
NoSuspiciousCharacters::RESTRICTION_LEVEL_ERROR,
88-
'Restriction level check failed.',
75+
'This value contains characters that are not allowed by the current restriction-level.',
8976
];
9077

9178
yield 'Fails RESTRICTION_LEVEL check because of mixed-script string' => [
@@ -95,37 +82,37 @@ public static function provideSuspiciousStrings(): iterable
9582
'locales' => ['en', 'zh_Hant_TW'],
9683
],
9784
NoSuspiciousCharacters::RESTRICTION_LEVEL_ERROR,
98-
'Restriction level check failed.',
85+
'This value contains characters that are not allowed by the current restriction-level.',
9986
];
10087

101-
yield 'Fails RESTRICTION_LEVEL check because of disallowed Armenian script' => [
88+
yield 'Fails RESTRICTION_LEVEL check because RESTRICTION_LEVEL_HIGH disallows Armenian script' => [
10289
'àԱ',
10390
[
10491
'restrictionLevel' => NoSuspiciousCharacters::RESTRICTION_LEVEL_HIGH,
10592
'locales' => ['en', 'hy_AM'],
10693
],
10794
NoSuspiciousCharacters::RESTRICTION_LEVEL_ERROR,
108-
'Restriction level check failed.',
95+
'This value contains characters that are not allowed by the current restriction-level.',
10996
];
11097

111-
yield 'Fails RESTRICTION_LEVEL check because of disallowed Greek script' => [
98+
yield 'Fails RESTRICTION_LEVEL check because RESTRICTION_LEVEL_MODERATE disallows Greek script' => [
11299
'àπ',
113100
[
114101
'restrictionLevel' => NoSuspiciousCharacters::RESTRICTION_LEVEL_MODERATE,
115102
'locales' => ['en', 'el_GR'],
116103
],
117104
NoSuspiciousCharacters::RESTRICTION_LEVEL_ERROR,
118-
'Restriction level check failed.',
105+
'This value contains characters that are not allowed by the current restriction-level.',
119106
];
120107

121-
yield 'Fails RESTRICTION_LEVEL check because of Greek script absent from profile' => [
108+
yield 'Fails RESTRICTION_LEVEL check because of characters missing from the configured locales’ scripts' => [
122109
'àπ',
123110
[
124-
'checks' => NoSuspiciousCharacters::CHECK_RESTRICTION_LEVEL,
125111
'restrictionLevel' => NoSuspiciousCharacters::RESTRICTION_LEVEL_MINIMAL,
112+
'locales' => ['en'],
126113
],
127114
NoSuspiciousCharacters::RESTRICTION_LEVEL_ERROR,
128-
'Restriction level check failed.',
115+
'This value contains characters that are not allowed by the current restriction-level.',
129116
];
130117

131118
yield 'Fails INVISIBLE check because of duplicated non-spacing mark' => [
@@ -134,16 +121,7 @@ public static function provideSuspiciousStrings(): iterable
134121
'checks' => NoSuspiciousCharacters::CHECK_INVISIBLE,
135122
],
136123
NoSuspiciousCharacters::INVISIBLE_ERROR,
137-
'Invisible check failed.',
138-
];
139-
140-
yield 'Fails CHAR_LIMIT check because of Greek script absent from profile' => [
141-
'àπ',
142-
[
143-
'checks' => NoSuspiciousCharacters::CHECK_CHAR_LIMIT,
144-
],
145-
NoSuspiciousCharacters::CHAR_LIMIT_ERROR,
146-
'Char limit check failed.',
124+
'Using invisible characters is not allowed.',
147125
];
148126

149127
yield 'Fails MIXED_NUMBERS check because of different numbering systems' => [
@@ -152,7 +130,7 @@ public static function provideSuspiciousStrings(): iterable
152130
'checks' => NoSuspiciousCharacters::CHECK_MIXED_NUMBERS,
153131
],
154132
NoSuspiciousCharacters::MIXED_NUMBERS_ERROR,
155-
'Mixed numbers check failed.',
133+
'Mixing numbers from different scripts is not allowed.',
156134
];
157135

158136
yield 'Fails HIDDEN_OVERLAY check because of hidden combining character' => [
@@ -161,14 +139,13 @@ public static function provideSuspiciousStrings(): iterable
161139
'checks' => NoSuspiciousCharacters::CHECK_HIDDEN_OVERLAY,
162140
],
163141
NoSuspiciousCharacters::HIDDEN_OVERLAY_ERROR,
164-
'Hidden overlay check failed.',
142+
'Using hidden overlay characters is not allowed.',
165143
];
166144
}
167145

168146
public function testConstants()
169147
{
170148
$this->assertSame(\Spoofchecker::INVISIBLE, NoSuspiciousCharacters::CHECK_INVISIBLE);
171-
$this->assertSame(\Spoofchecker::CHAR_LIMIT, NoSuspiciousCharacters::CHECK_CHAR_LIMIT);
172149

173150
$this->assertSame(\Spoofchecker::ASCII, NoSuspiciousCharacters::RESTRICTION_LEVEL_ASCII);
174151
$this->assertSame(\Spoofchecker::SINGLE_SCRIPT_RESTRICTIVE, NoSuspiciousCharacters::RESTRICTION_LEVEL_SINGLE_SCRIPT);

0 commit comments

Comments
 (0)