Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add validRegexPattern assertions #305

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ Method | Description
`ipv6($value, $message = '')` | Check that a string is a valid IPv6
`email($value, $message = '')` | Check that a string is a valid e-mail address
`notWhitespaceOnly($value, $message = '')` | Check that a string contains at least one non-whitespace character
`validRegexPattern($value, $message = '')` | Check that a string is a valid regular expression pattern

### File Assertions

Expand Down
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,
$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 @@ -3200,6 +3200,59 @@ public static function allNullOrNotRegex($value, $pattern, $message = '')
}
}

/**
* @psalm-pure
*
* @param string|null $value
* @param string $message
*
* @return void
*
* @throws InvalidArgumentException
*/
public static function nullOrValidRegexPattern($value, $message = 'The value %s must be a valid regex pattern.')
{
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 = 'The value %s must be a valid regex pattern.')
{
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 = 'The value %s must be a valid regex pattern.')
{
static::isIterable($value);

foreach ($value as $entry) {
null === $entry || static::validRegexPattern($entry, $message);
}
}

/**
* @psalm-pure
*
Expand Down
5 changes: 5 additions & 0 deletions tests/AssertTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,11 @@ 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('/(?:abc)\1/'), false), // Backreference to non-capturing group
);
}

Expand Down