From a2119316b4bef2706cb398d7a7e8fba53907d87c Mon Sep 17 00:00:00 2001 From: Zlatoslav Desyatnikov Date: Sun, 20 Oct 2024 19:08:37 +0400 Subject: [PATCH] feat: add validRegexPattern --- src/Assert.php | 20 +++++++++++++++++ src/Mixin.php | 53 ++++++++++++++++++++++++++++++++++++++++++++ tests/AssertTest.php | 6 +++++ 3 files changed, 79 insertions(+) diff --git a/src/Assert.php b/src/Assert.php index b962e3e..b115ab9 100644 --- a/src/Assert.php +++ b/src/Assert.php @@ -1227,6 +1227,26 @@ public static function notRegex($value, $pattern, $message = '') } } + /** + * @psalm-pure + * + * @param string $value + * @param string $message + * + * @throws InvalidArgumentException + */ + public static function validRegexPattern($value, $message = 'The value %s must be a valid regex pattern.') + { + static::string($value); + + if (@\preg_match($value, '') === false) { + static::reportInvalidArgument(\sprintf( + $message, + static::valueToString($value) + )); + } + } + /** * @psalm-pure * diff --git a/src/Mixin.php b/src/Mixin.php index fad0d47..e85b0d9 100644 --- a/src/Mixin.php +++ b/src/Mixin.php @@ -5207,4 +5207,57 @@ public static function allNullOrThrows($expression, $class = 'Exception', $messa null === $entry || static::throws($entry, $class, $message); } } + + /** + * @psalm-pure + * + * @param string|null $value + * @param string $message + * + * @return void + * + * @throws InvalidArgumentException + */ + public static function nullOrValidRegexPattern($value, $message = '') + { + null === $value || static::validRegexPattern($value, $message); + } + + /** + * @psalm-pure + * + * @param iterable $value + * @param string $message + * + * @return void + * + * @throws InvalidArgumentException + */ + public static function allValidRegexPattern($value, $message = '') + { + static::isIterable($value); + + foreach ($value as $entry) { + static::validRegexPattern($entry, $message); + } + } + + /** + * @psalm-pure + * + * @param iterable $value + * @param string $message + * + * @return void + * + * @throws InvalidArgumentException + */ + public static function allNullOrValidRegexPattern($value, $message = '') + { + static::isIterable($value); + + foreach ($value as $entry) { + null === $entry || static::validRegexPattern($entry, $message); + } + } } diff --git a/tests/AssertTest.php b/tests/AssertTest.php index ef9b6be..b8b48ce 100644 --- a/tests/AssertTest.php +++ b/tests/AssertTest.php @@ -595,6 +595,12 @@ public function getTests() array('uniqueValues', array(array('qwerty', 'qwerty')), false), array('uniqueValues', array(array('asdfg', 'qwerty')), true), array('uniqueValues', array(array(123, '123')), false), + array('validRegexPattern', array('/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/'), true), + array('validRegexPattern', array('/^\(\d{3}\) \d{3}-\d{4}$/'), true), + array('validRegexPattern', array('/^https?:\/\/[^\s/$.?#].[^\s]*$/i'), true), + array('validRegexPattern', array('/^(abc/'), false), // Unclosed parenthesis + array('validRegexPattern', array('/[a-z-]/'), false), // Improper character class + array('validRegexPattern', array('/(?:abc)\1/'), false), // Backreference to non-capturing group ); }