forked from mautic/mautic
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request mautic#2206 from acquia/MAUT-11541
Adding regex operator validation
- Loading branch information
Showing
6 changed files
with
140 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
app/bundles/LeadBundle/Form/Validator/Constraints/DbRegex.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Mautic\LeadBundle\Form\Validator\Constraints; | ||
|
||
use Symfony\Component\Validator\Constraint; | ||
|
||
final class DbRegex extends Constraint | ||
{ | ||
} |
38 changes: 38 additions & 0 deletions
38
app/bundles/LeadBundle/Form/Validator/Constraints/DbRegexValidator.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Mautic\LeadBundle\Form\Validator\Constraints; | ||
|
||
use Doctrine\DBAL\Connection; | ||
use Doctrine\DBAL\Exception; | ||
use Symfony\Component\Validator\Constraint; | ||
use Symfony\Component\Validator\ConstraintValidator; | ||
use Symfony\Component\Validator\Exception\UnexpectedTypeException; | ||
|
||
final class DbRegexValidator extends ConstraintValidator | ||
{ | ||
public function __construct(private Connection $connection) | ||
{ | ||
} | ||
|
||
public function validate($regex, Constraint $constraint): void | ||
{ | ||
if (!$constraint instanceof DbRegex) { | ||
throw new UnexpectedTypeException($constraint, DbRegex::class); | ||
} | ||
|
||
try { | ||
$this->connection->executeQuery('SELECT "" REGEXP ? AS is_valid', [$regex]); | ||
} catch (Exception $e) { | ||
$this->context->buildViolation( | ||
$this->stripUglyPartOfTheErrorMessage($e->getPrevious()->getMessage()) | ||
)->addViolation(); | ||
} | ||
} | ||
|
||
private function stripUglyPartOfTheErrorMessage(string $message): string | ||
{ | ||
return preg_replace('/^SQLSTATE\[HY000\]: General error: \d+ /', '', $message); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters