Skip to content

Commit

Permalink
Decouple constants from intl
Browse files Browse the repository at this point in the history
  • Loading branch information
MatTheCat committed Feb 10, 2023
1 parent ac1355d commit 18fd4ff
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,18 @@ class NoSuspiciousCharacters extends Constraint
{
/**
* Check that a string satisfies the requirements for the specified restriction level
* (by default {@see \Spoofchecker::HIGHLY_RESTRICTIVE}).
* (by default {@see self::RESTRICTION_LEVEL_HIGH}).
*/
public const CHECK_RESTRICTION_LEVEL = 16;

/**
* Check a string for the presence of invisible characters such as zero-width spaces,
* or character sequences that are likely not to display such as multiple occurrences of the same non-spacing mark.
*/
public const CHECK_INVISIBLE = \Spoofchecker::INVISIBLE;
public const CHECK_INVISIBLE = 32;

/** Check a string contains only characters allowed by the configured profile. */
public const CHECK_CHAR_LIMIT = \Spoofchecker::CHAR_LIMIT;
public const CHECK_CHAR_LIMIT = 64;

/**
* Check that a string does not mix numbers from different numbering systems;
Expand All @@ -50,14 +50,22 @@ class NoSuspiciousCharacters extends Constraint
*/
public const CHECK_HIDDEN_OVERLAY = 256;

public const RESTRICTION_LEVEL_ASCII = 268435456;
public const RESTRICTION_LEVEL_SINGLE_SCRIPT = 536870912;
public const RESTRICTION_LEVEL_HIGH = 805306368;
public const RESTRICTION_LEVEL_MODERATE = 1073741824;
public const RESTRICTION_LEVEL_MINIMAL = 1342177280;
public const RESTRICTION_LEVEL_NONE = 1610612736;

public $message = 'This value is suspicious.';
public int $checks = self::CHECK_RESTRICTION_LEVEL | self::CHECK_INVISIBLE | self::CHECK_CHAR_LIMIT | self::CHECK_MIXED_NUMBERS | self::CHECK_HIDDEN_OVERLAY;
public int $restrictionLevel = \Spoofchecker::HIGHLY_RESTRICTIVE;
public int $restrictionLevel = self::RESTRICTION_LEVEL_HIGH;
public array $profileLocales = [];
public bool $addCurrentLocaleToProfile = true;

/**
* @param int-mask-of<self::CHECK_*>|null $checks
* @param self::RESTRICTION_LEVEL_*|null $restrictionLevel
*/
public function __construct(
array $options = null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,23 +52,23 @@ public function testSuspiciousStrings(string $string, array $options)
public static function provideSuspiciousStrings(): iterable
{
yield 'Fails restriction level check because of character outside ASCII range' => ['à',
['restrictionLevel' => \Spoofchecker::ASCII],
['restrictionLevel' => NoSuspiciousCharacters::RESTRICTION_LEVEL_ASCII],
];
yield 'Fails restriction level check because of mixed-script string' => ['àㄚ', [
'restrictionLevel' => \Spoofchecker::SINGLE_SCRIPT_RESTRICTIVE,
'restrictionLevel' => NoSuspiciousCharacters::RESTRICTION_LEVEL_SINGLE_SCRIPT,
'profileLocales' => ['zh_Hant_TW'],
]];
yield 'Fails restriction level check because of disallowed Armenian script' => ['àԱ', [
'restrictionLevel' => \Spoofchecker::HIGHLY_RESTRICTIVE,
'restrictionLevel' => NoSuspiciousCharacters::RESTRICTION_LEVEL_HIGH,
'profileLocales' => ['hy_AM'],
]];
yield 'Fails restriction level check because of disallowed Greek script' => ['àπ', [
'restrictionLevel' => \Spoofchecker::MODERATELY_RESTRICTIVE,
'restrictionLevel' => NoSuspiciousCharacters::RESTRICTION_LEVEL_MODERATE,
'profileLocales' => ['el_GR'],
]];
yield 'Fails restriction level check because of Greek script absent from profile' => ['àπ', [
'checks' => NoSuspiciousCharacters::CHECK_RESTRICTION_LEVEL,
'restrictionLevel' => \Spoofchecker::MINIMALLY_RESTRICTIVE,
'restrictionLevel' => NoSuspiciousCharacters::RESTRICTION_LEVEL_MINIMAL,
]];

yield 'Fails INVISIBLE check because of duplicated non-spacing mark' => ['à̀', [
Expand All @@ -84,4 +84,17 @@ public static function provideSuspiciousStrings(): iterable
'checks' => NoSuspiciousCharacters::CHECK_HIDDEN_OVERLAY,
]];
}

public function testConstants(): void
{
$this->assertSame(\Spoofchecker::INVISIBLE, NoSuspiciousCharacters::CHECK_INVISIBLE);
$this->assertSame(\Spoofchecker::CHAR_LIMIT, NoSuspiciousCharacters::CHECK_CHAR_LIMIT);

$this->assertSame(\Spoofchecker::ASCII, NoSuspiciousCharacters::RESTRICTION_LEVEL_ASCII);
$this->assertSame(\Spoofchecker::SINGLE_SCRIPT_RESTRICTIVE, NoSuspiciousCharacters::RESTRICTION_LEVEL_SINGLE_SCRIPT);
$this->assertSame(\Spoofchecker::HIGHLY_RESTRICTIVE, NoSuspiciousCharacters::RESTRICTION_LEVEL_HIGH);
$this->assertSame(\Spoofchecker::MODERATELY_RESTRICTIVE, NoSuspiciousCharacters::RESTRICTION_LEVEL_MODERATE);
$this->assertSame(\Spoofchecker::MINIMALLY_RESTRICTIVE, NoSuspiciousCharacters::RESTRICTION_LEVEL_MINIMAL);
$this->assertSame(\Spoofchecker::UNRESTRICTIVE, NoSuspiciousCharacters::RESTRICTION_LEVEL_NONE);
}
}

0 comments on commit 18fd4ff

Please sign in to comment.