Skip to content

Commit

Permalink
feat: add validRegexPattern
Browse files Browse the repository at this point in the history
  • Loading branch information
zlodes committed Oct 20, 2024
1 parent c4c6fb8 commit a211931
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/Assert.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
53 changes: 53 additions & 0 deletions src/Mixin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> $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<string|null> $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);
}
}
}
6 changes: 6 additions & 0 deletions tests/AssertTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}

Expand Down

0 comments on commit a211931

Please sign in to comment.