48 lines
1.5 KiB
PHP
48 lines
1.5 KiB
PHP
<?php
|
|
|
|
namespace App\Validator;
|
|
|
|
use PDO;
|
|
use App\Services\PdoServices;
|
|
use App\Validator\ExistingEmail;
|
|
use Symfony\Component\Validator\Constraint;
|
|
use Symfony\Component\Validator\ConstraintValidator;
|
|
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
|
|
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
|
|
|
|
class ExistingEmailValidator extends ConstraintValidator
|
|
{
|
|
|
|
private PdoServices $pdoServices;
|
|
public $params;
|
|
public function __construct(PdoServices $pdoServices, ParameterBagInterface $params)
|
|
{
|
|
$this->pdoServices = $pdoServices;
|
|
$this->params = $params;
|
|
}
|
|
public function validate($value, Constraint $constraint)
|
|
{
|
|
if (!$constraint instanceof ExistingEmail) {
|
|
throw new UnexpectedTypeException($constraint, ExistingEmail::class);
|
|
}
|
|
|
|
// custom constraints should ignore null and empty values to allow
|
|
// other constraints (NotBlank, NotNull, etc.) to take care of that
|
|
if (null === $value || '' === $value) {
|
|
return;
|
|
}
|
|
$dbh = $this->pdoServices->connection();
|
|
|
|
$query = $dbh->prepare($this->params->get("queryHashPassword")."'".$value ."';");
|
|
$query->execute();
|
|
$hashPassword = $query->fetch(PDO::FETCH_ASSOC);
|
|
|
|
if(!$hashPassword){
|
|
$this->context->buildViolation(
|
|
$constraint->message
|
|
)->setParameter('{{ string }}', $value)
|
|
->addViolation()
|
|
;
|
|
}
|
|
}
|
|
} |