Total Complexity | 10 |
Total Lines | 70 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
23 | final class RegexChecker |
||
24 | { |
||
25 | // https://www.php.net/manual/en/reference.pcre.pattern.modifiers.php |
||
26 | private const PATTERN_MODIFIERS = [ |
||
27 | 'i', |
||
28 | 'm', |
||
29 | 's', |
||
30 | 'x', |
||
31 | 'A', |
||
32 | 'D', |
||
33 | 'S', |
||
34 | 'U', |
||
35 | 'X', |
||
36 | 'J', |
||
37 | 'u', |
||
38 | ]; |
||
39 | |||
40 | public function isRegexLike(string $value): bool |
||
41 | { |
||
42 | $valueLength = strlen($value); |
||
43 | |||
44 | if ($valueLength < 2) { |
||
45 | return false; |
||
46 | } |
||
47 | |||
48 | $firstCharacter = $value[0]; |
||
49 | |||
50 | $parts = explode($firstCharacter, $value); |
||
51 | |||
52 | if (count($parts) !== 3) { |
||
53 | return false; |
||
54 | } |
||
55 | |||
56 | $lastPart = array_pop($parts); |
||
57 | |||
58 | if (!self::isValidRegexFlags($lastPart)) { |
||
59 | return false; |
||
60 | } |
||
61 | |||
62 | return true; |
||
63 | } |
||
64 | |||
65 | public function validateRegex(string $regex): ?string |
||
75 | ); |
||
76 | } |
||
77 | |||
78 | private static function isValidRegexFlags(string $value): bool |
||
95 |