mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2026-01-13 21:59:34 +00:00
38 lines
No EOL
1.1 KiB
PHP
38 lines
No EOL
1.1 KiB
PHP
<?php
|
|
|
|
namespace App\Validator\Constraints;
|
|
|
|
use App\Entity\Parts\Part;
|
|
use Symfony\Component\Validator\Constraint;
|
|
use Symfony\Component\Validator\ConstraintValidator;
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
|
|
class UniquePartIpnValidator extends ConstraintValidator
|
|
{
|
|
private EntityManagerInterface $entityManager;
|
|
private bool $enforceUniqueIpn;
|
|
|
|
public function __construct(EntityManagerInterface $entityManager, bool $enforceUniqueIpn)
|
|
{
|
|
$this->entityManager = $entityManager;
|
|
$this->enforceUniqueIpn = $enforceUniqueIpn;
|
|
}
|
|
|
|
public function validate($value, Constraint $constraint)
|
|
{
|
|
if (null === $value || '' === $value) {
|
|
return;
|
|
}
|
|
|
|
$repository = $this->entityManager->getRepository(Part::class);
|
|
$existingPart = $repository->findOneBy(['ipn' => $value]);
|
|
|
|
if ($existingPart) {
|
|
if ($this->enforceUniqueIpn) {
|
|
$this->context->buildViolation($constraint->message)
|
|
->setParameter('{{ value }}', $value)
|
|
->addViolation();
|
|
}
|
|
}
|
|
}
|
|
} |