| 1 |
<?php |
| 2 |
|
| 3 |
namespace ipl\Validator; |
| 4 |
|
| 5 |
/** |
| 6 |
* Validate a PHP regular expression, syntax: /PATTERN/MODIFIERS |
| 7 |
*/ |
| 8 |
class RegexSyntaxValidator extends BaseValidator |
| 9 |
{ |
| 10 |
/** |
| 11 |
* Check whether the value is a valid PHP regular expression |
| 12 |
* |
| 13 |
* @param string $value |
| 14 |
* |
| 15 |
* @return bool |
| 16 |
*/ |
| 17 |
public function isValid($value): bool |
| 18 |
{ |
| 19 |
$this->clearMessages(); |
| 20 |
|
| 21 |
if (! is_string($value)) { |
| 22 |
$this->addMessage($this->translate('Invalid regex given, must be a string')); |
| 23 |
|
| 24 |
return false; |
| 25 |
} |
| 26 |
|
| 27 |
if (@preg_match($value, '') === false) { |
| 28 |
$this->addMessage(sprintf( |
| 29 |
$this->translate('Regex "%s" failed to compile: %s'), |
| 30 |
$value, |
| 31 |
preg_last_error_msg() |
| 32 |
)); |
| 33 |
|
| 34 |
return false; |
| 35 |
} |
| 36 |
|
| 37 |
return true; |
| 38 |
} |
| 39 |
} |