pattern = $pattern['pattern'] ?? throw new InvalidArgumentException("Missing option 'pattern'"); $this->notMatchMessage = $pattern['notMatchMessage'] ?? null; if ($this->notMatchMessage !== null && trim($this->notMatchMessage) === '') { throw new InvalidArgumentException( "Option 'notMatchMessage' must not be an empty or whitespace-only string" ); } } else { $this->pattern = $pattern; } $syntax = new RegexSyntaxValidator(); if (! $syntax->isValid($this->pattern)) { throw new InvalidArgumentException($syntax->getMessages()[0]); } } /** * Check whether the value matches the pattern * * @param string $value * * @return bool */ public function isValid($value): bool { // Reset messages from a previous isValid() call. $this->clearMessages(); $result = preg_match($this->pattern, $value); if ($result === false) { $this->addMessage(preg_last_error_msg()); return false; } if (! $result) { if ($this->notMatchMessage === null) { $this->addMessage(sprintf( $this->translate("'%s' does not match against pattern '%s'"), $value, $this->pattern )); } else { $this->addMessage($this->notMatchMessage); } return false; } return true; } }