Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
22 / 22 |
|
100.00% |
11 / 11 |
CRAP | |
100.00% |
1 / 1 |
| Regex | |
100.00% |
22 / 22 |
|
100.00% |
11 / 11 |
12 | |
100.00% |
1 / 1 |
| matchFloat | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| matchNumber | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| matchInteger | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| matchPositiveInteger | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| matchNegativeInteger | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| matchIntegerID | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| matchText | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| matchQuery | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| match | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| pattern | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| sanitize | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Projom\Util; |
| 6 | |
| 7 | class Regex |
| 8 | { |
| 9 | private const FLOAT_PATTERN = '/^[+-]?[0-9]*[.,]{1}[0-9]+$/'; |
| 10 | private const NUMBER_PATTERN = '/^[+-]?([0-9]*[.,]{1})?[0-9]+$/'; |
| 11 | private const INT_PATTERN = '/^[+-]?[0-9]+$/'; |
| 12 | private const INT_POS_PATTERN = '/^[+]?[0-9]+$/'; |
| 13 | private const INT_NEG_PATTERN = '/^[-]{1}[0-9]+$/'; |
| 14 | private const INT_ID_PATTERN = '/^[1-9]+[0-9]*$/'; |
| 15 | private const TEXT_PATTERN = '/^.*$/'; |
| 16 | private const QUERY_PATTERN = '/^[\w\/\.?=&]+$/'; |
| 17 | private const SANTIZE_PATTERN = '/[^\w]+/'; |
| 18 | |
| 19 | public static function matchFloat(int|float|string $subject): bool |
| 20 | { |
| 21 | $match = static::match(static::FLOAT_PATTERN, (string) $subject); |
| 22 | return $match; |
| 23 | } |
| 24 | |
| 25 | public static function matchNumber(int|float|string $subject): bool |
| 26 | { |
| 27 | $match = static::match(static::NUMBER_PATTERN, (string) $subject); |
| 28 | return $match; |
| 29 | } |
| 30 | |
| 31 | public static function matchInteger(int|float|string $subject): bool |
| 32 | { |
| 33 | $match = static::match(static::INT_PATTERN, (string) $subject); |
| 34 | return $match; |
| 35 | } |
| 36 | |
| 37 | public static function matchPositiveInteger(int|float|string $subject): bool |
| 38 | { |
| 39 | $match = static::match(static::INT_POS_PATTERN, (string) $subject); |
| 40 | return $match; |
| 41 | } |
| 42 | |
| 43 | public static function matchNegativeInteger(int|float|string $subject): bool |
| 44 | { |
| 45 | $match = static::match(static::INT_NEG_PATTERN, (string) $subject); |
| 46 | return $match; |
| 47 | } |
| 48 | |
| 49 | public static function matchIntegerID(int|float|string $subject): bool |
| 50 | { |
| 51 | $match = static::match(static::INT_ID_PATTERN, (string) $subject); |
| 52 | return $match; |
| 53 | } |
| 54 | |
| 55 | public static function matchText(string $subject): bool |
| 56 | { |
| 57 | $match = static::match(static::TEXT_PATTERN, $subject); |
| 58 | return $match; |
| 59 | } |
| 60 | |
| 61 | public static function matchQuery(string $subject): bool |
| 62 | { |
| 63 | $match = static::match(static::QUERY_PATTERN, $subject); |
| 64 | return $match; |
| 65 | } |
| 66 | |
| 67 | public static function match(string $pattern, int|float|string $subject): bool |
| 68 | { |
| 69 | return preg_match($pattern, $subject) === 1; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Returns the matches of the pattern in the subject. If no match is found, null is returned. |
| 74 | */ |
| 75 | public static function pattern(string $pattern, string $subject): null|array |
| 76 | { |
| 77 | if (preg_match($pattern, $subject, $matches) === 1) |
| 78 | return $matches; |
| 79 | return null; |
| 80 | } |
| 81 | |
| 82 | public static function sanitize(string $subject): string |
| 83 | { |
| 84 | $sanitizedSubject = preg_replace(static::SANTIZE_PATTERN, '', $subject); |
| 85 | return $sanitizedSubject; |
| 86 | } |
| 87 | } |