Skip to content

Commit f82511e

Browse files
authored
Merge pull request #145 from membrane-php/fix-regex-to-php
Fixes converting a regex validator to php code when special chars are…
2 parents 21f0477 + 46ef370 commit f82511e

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

src/Validator/String/Regex.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public function __toString(): string
2222

2323
public function __toPHP(): string
2424
{
25-
return sprintf('new %s("%s")', self::class, $this->pattern);
25+
return sprintf('new %s(\'%s\')', self::class, addslashes($this->pattern));
2626
}
2727

2828
public function validate(mixed $value): Result

tests/Validator/String/RegexTest.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ public function toStringTest(): void
3232
}
3333

3434
#[Test]
35-
public function toPHPTest(): void
35+
#[DataProvider('provideRegularExpresions')]
36+
public function toPHPTest(string $regex): void
3637
{
3738
$sut = new Regex('/[abc]/i');
3839

@@ -41,6 +42,14 @@ public function toPHPTest(): void
4142
self::assertEquals($sut, eval('return ' . $actual . ';'));
4243
}
4344

45+
public static function provideRegularExpresions(): array
46+
{
47+
return [
48+
'simple regex' => ['/[abc]/i'],
49+
'regex with special chars' => ['#^[^$\'"]$#']
50+
];
51+
}
52+
4453
public static function dataSetsWithIncorrectTypes(): array
4554
{
4655
return [

0 commit comments

Comments
 (0)