|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 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\LogicException; |
| 16 | + |
| 17 | +/** |
| 18 | + * @Annotation |
| 19 | + * @Target({"PROPERTY", "METHOD", "ANNOTATION"}) |
| 20 | + * |
| 21 | + * @author Mathieu Lechat <mathieu.lechat@les-tilleuls.coop> |
| 22 | + */ |
| 23 | +#[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)] |
| 24 | +class NoSuspiciousCharacters extends Constraint |
| 25 | +{ |
| 26 | + public const RESTRICTION_LEVEL_ERROR = '1ece07dc-dca2-45f1-ba47-8d7dc3a12774'; |
| 27 | + public const INVISIBLE_ERROR = '6ed60e6c-179b-4e93-8a6c-667d85c6de5e'; |
| 28 | + public const MIXED_NUMBERS_ERROR = '9f01fc26-3bc4-44b1-a6b1-c08e2412053a'; |
| 29 | + public const HIDDEN_OVERLAY_ERROR = '56380dc5-0476-4f04-bbaa-b68cd1c2d974'; |
| 30 | + |
| 31 | + protected const ERROR_NAMES = [ |
| 32 | + self::RESTRICTION_LEVEL_ERROR => 'RESTRICTION_LEVEL_ERROR', |
| 33 | + self::INVISIBLE_ERROR => 'INVISIBLE_ERROR', |
| 34 | + self::MIXED_NUMBERS_ERROR => 'MIXED_NUMBERS_ERROR', |
| 35 | + self::HIDDEN_OVERLAY_ERROR => 'INVALID_CASE_ERROR', |
| 36 | + ]; |
| 37 | + |
| 38 | + /** |
| 39 | + * Check a string for the presence of invisible characters such as zero-width spaces, |
| 40 | + * or character sequences that are likely not to display such as multiple occurrences of the same non-spacing mark. |
| 41 | + */ |
| 42 | + public const CHECK_INVISIBLE = 32; |
| 43 | + |
| 44 | + /** |
| 45 | + * Check that a string does not mix numbers from different numbering systems; |
| 46 | + * for example “8” (Digit Eight) and “৪” (Bengali Digit Four). |
| 47 | + */ |
| 48 | + public const CHECK_MIXED_NUMBERS = 128; |
| 49 | + |
| 50 | + /** |
| 51 | + * Check that a string does not have a combining character following a character in which it would be hidden; |
| 52 | + * for example “i” (Latin Small Letter I) followed by a U+0307 (Combining Dot Above). |
| 53 | + */ |
| 54 | + public const CHECK_HIDDEN_OVERLAY = 256; |
| 55 | + |
| 56 | + /** @see https://unicode.org/reports/tr39/#ascii_only */ |
| 57 | + public const RESTRICTION_LEVEL_ASCII = 268435456; |
| 58 | + |
| 59 | + /** @see https://unicode.org/reports/tr39/#single_script */ |
| 60 | + public const RESTRICTION_LEVEL_SINGLE_SCRIPT = 536870912; |
| 61 | + |
| 62 | + /** @see https://unicode.org/reports/tr39/#highly_restrictive */ |
| 63 | + public const RESTRICTION_LEVEL_HIGH = 805306368; |
| 64 | + |
| 65 | + /** @see https://unicode.org/reports/tr39/#moderately_restrictive */ |
| 66 | + public const RESTRICTION_LEVEL_MODERATE = 1073741824; |
| 67 | + |
| 68 | + /** @see https://unicode.org/reports/tr39/#minimally_restrictive */ |
| 69 | + public const RESTRICTION_LEVEL_MINIMAL = 1342177280; |
| 70 | + |
| 71 | + /** @see https://unicode.org/reports/tr39/#unrestricted */ |
| 72 | + public const RESTRICTION_LEVEL_NONE = 1610612736; |
| 73 | + |
| 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.'; |
| 78 | + |
| 79 | + public int $checks = self::CHECK_INVISIBLE | self::CHECK_MIXED_NUMBERS | self::CHECK_HIDDEN_OVERLAY; |
| 80 | + public ?int $restrictionLevel = null; |
| 81 | + public ?array $locales = null; |
| 82 | + |
| 83 | + /** |
| 84 | + * @param int-mask-of<self::CHECK_*>|null $checks |
| 85 | + * @param self::RESTRICTION_LEVEL_*|null $restrictionLevel |
| 86 | + */ |
| 87 | + public function __construct( |
| 88 | + array $options = null, |
| 89 | + string $restrictionLevelMessage = null, |
| 90 | + string $invisibleMessage = null, |
| 91 | + string $mixedNumbersMessage = null, |
| 92 | + string $hiddenOverlayMessage = null, |
| 93 | + int $checks = null, |
| 94 | + int $restrictionLevel = null, |
| 95 | + array $locales = null, |
| 96 | + array $groups = null, |
| 97 | + mixed $payload = null |
| 98 | + ) { |
| 99 | + if (!class_exists(\Spoofchecker::class)) { |
| 100 | + throw new LogicException('The intl extension is required to use the NoSuspiciousCharacters constraint.'); |
| 101 | + } |
| 102 | + |
| 103 | + parent::__construct($options, $groups, $payload); |
| 104 | + |
| 105 | + $this->restrictionLevelMessage ??= $restrictionLevelMessage; |
| 106 | + $this->invisibleMessage ??= $invisibleMessage; |
| 107 | + $this->mixedNumbersMessage ??= $mixedNumbersMessage; |
| 108 | + $this->hiddenOverlayMessage ??= $hiddenOverlayMessage; |
| 109 | + $this->checks ??= $checks; |
| 110 | + $this->restrictionLevel ??= $restrictionLevel; |
| 111 | + $this->locales ??= $locales; |
| 112 | + } |
| 113 | +} |
0 commit comments