Repository: laminas/laminas-validator (php)

Issue:
Regex Validator Doesn't Pass Options to Parent Constructor
### Bug Report

|    Q        |   A
|------------ | ------
| Version(s)  | 3.x.x

#### Summary
In all validator classes I have used, it is possible to pass an options array with message overrides as a parameter when instantiating an object. However, the [Regex ](https://github.com/laminas/laminas-validator/blob/3.2.x/src/Regex.php) class appears to lack this functionality. The options parameter is not passed to the [parent constructor method](https://github.com/laminas/laminas-validator/blob/3.2.x/src/Regex.php#L68).

The options parameter was removed from the parent method call in this commit: 
fe44175d17169138d3bd393e03034edbdea296c5

#### Current behavior
When instantiating an object of the Regex class, all options besides the pattern are ignored.

#### How to reproduce
Instantiate a Regex object and pass additional options beyond just the pattern:

`
$regexValidator = new Regex([
    'pattern' => '<Regex>', 
    'messages' => [Regex::NOT_MATCH => '<Custom message>']
]);
`

The message for Regex::NOT_MATCH will still be the default one.

#### Expected behavior

Options should be applied during instantiation, just like in other validator classes.


Pull request description:
Fix: Regex validator does not permit customisation of abstract options via options argument
|    Q          |   A
|-------------- | ------
| Bugfix        | yes

### Description

Closes #411 

Patch:
diff --git a/src/Regex.php b/src/Regex.php
index 8063a625..3968a153 100644
--- a/src/Regex.php
+++ b/src/Regex.php
@@ -50,7 +50,8 @@ final class Regex extends AbstractValidator
      */
     public function __construct(string|array $options)
     {
-        $pattern = is_string($options) ? $options : $options['pattern'] ?? null;
+        $options = is_string($options) ? ['pattern' => $options] : $options;
+        $pattern = $options['pattern'] ?? null;
         /** @psalm-suppress DocblockTypeContradiction The user may still supply an empty string */
         if (! is_string($pattern) || $pattern === '') {
             throw new InvalidArgumentException('A regex pattern is required');
@@ -65,7 +66,7 @@ final class Regex extends AbstractValidator
 
         $this->pattern = $pattern;
 
-        parent::__construct();
+        parent::__construct($options);
     }
 
     /**


Test patch:
diff --git a/test/RegexTest.php b/test/RegexTest.php
index 066273ec..0db884a9 100644
--- a/test/RegexTest.php
+++ b/test/RegexTest.php
@@ -86,7 +86,7 @@ final class RegexTest extends TestCase
         self::assertSame(
             $expected,
             $validator->isValid($input),
-            'Reason: ' . implode('', $validator->getMessages())
+            'Reason: ' . implode('', $validator->getMessages()),
         );
     }
 
@@ -144,4 +144,20 @@ final class RegexTest extends TestCase
         /** @psalm-suppress InvalidArgument */
         new Regex($options);
     }
+
+    public function testAbstractOptionsAreProvidedToParentConstructor(): void
+    {
+        $validator = new Regex([
+            'pattern'  => '/^\d+$/',
+            'messages' => [
+                Regex::NOT_MATCH => 'Numbers only',
+            ],
+        ]);
+
+        self::assertFalse($validator->isValid('Not numbers'));
+        $messages = $validator->getMessages();
+        self::assertSame([
+            Regex::NOT_MATCH => 'Numbers only',
+        ], $messages);
+    }
 }