Skip to content

Commit

Permalink
Automatically enable CHAR_LIMIT check when allowed locales are config…
Browse files Browse the repository at this point in the history
…ured
  • Loading branch information
MatTheCat committed Feb 13, 2023
1 parent 25bb90d commit 9c0be08
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,6 @@ class NoSuspiciousCharacters extends Constraint
*/
public const CHECK_INVISIBLE = 32;

/** Check a string contains only characters from the configured locales. */
public const CHECK_CHAR_LIMIT = 64;

/**
* Check that a string does not mix numbers from different numbering systems;
* for example “8” (Digit Eight) and “৪” (Bengali Digit Four).
Expand Down Expand Up @@ -89,7 +86,7 @@ class NoSuspiciousCharacters extends Constraint
public string $mixedNumbersMessage = 'Mixed numbers check failed.';
public string $hiddenOverlayMessage = 'Hidden overlay check failed.';

public int $checks = self::CHECK_RESTRICTION_LEVEL | self::CHECK_INVISIBLE | self::CHECK_CHAR_LIMIT | self::CHECK_MIXED_NUMBERS | self::CHECK_HIDDEN_OVERLAY;
public int $checks = self::CHECK_RESTRICTION_LEVEL | self::CHECK_INVISIBLE | self::CHECK_MIXED_NUMBERS | self::CHECK_HIDDEN_OVERLAY;
public ?int $restrictionLevel = null;
public ?array $locales = null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
*/
class NoSuspiciousCharactersValidator extends ConstraintValidator
{
private const CHECK_CHAR_LIMIT = 64;

private const CHECK_ERROR = [
NoSuspiciousCharacters::CHECK_RESTRICTION_LEVEL => [
'code' => NoSuspiciousCharacters::RESTRICTION_LEVEL_ERROR,
Expand All @@ -30,7 +32,7 @@ class NoSuspiciousCharactersValidator extends ConstraintValidator
'code' => NoSuspiciousCharacters::INVISIBLE_ERROR,
'messageProperty' => 'invisibleMessage',
],
NoSuspiciousCharacters::CHECK_CHAR_LIMIT => [
self::CHECK_CHAR_LIMIT => [
'code' => NoSuspiciousCharacters::CHAR_LIMIT_ERROR,
'messageProperty' => 'charLimitMessage',
],
Expand Down Expand Up @@ -74,7 +76,11 @@ public function validate(mixed $value, Constraint $constraint)
if ($constraint->restrictionLevel) {
$checker->setRestrictionLevel($constraint->restrictionLevel);
}
$checker->setAllowedLocales(implode(',', $constraint->locales ?? $this->defaultLocales));

if ($allowedLocales = $constraint->locales ?? $this->defaultLocales) {
$checker->setAllowedLocales($allowedLocales);
$constraint->checks &= self::CHECK_CHAR_LIMIT;
}

$checker->setChecks($constraint->checks);

Expand Down

0 comments on commit 9c0be08

Please sign in to comment.