Fixes #4033 -- Add pre-DB duplicate entity validation to entities.
This commit is contained in:
parent
ed1d3263ef
commit
21bcb0dc98
|
@ -6,6 +6,7 @@ namespace App\Entity;
|
|||
|
||||
use App\Annotations\AuditLog;
|
||||
use App\Normalizer\Annotation\DeepNormalize;
|
||||
use App\Validator\Constraints\UniqueEntity;
|
||||
use Doctrine\Common\Collections\ArrayCollection;
|
||||
use Doctrine\Common\Collections\Collection;
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
|
@ -24,6 +25,8 @@ use const PASSWORD_ARGON2ID;
|
|||
* @ORM\Entity()
|
||||
* @ORM\HasLifecycleCallbacks
|
||||
*
|
||||
* @UniqueEntity(fields={"station_id", "streamer_username"})
|
||||
*
|
||||
* @AuditLog\Auditable
|
||||
*
|
||||
* @OA\Schema(type="object")
|
||||
|
|
|
@ -61,7 +61,10 @@ class UniqueEntity extends Constraint
|
|||
$this->message = __('This value is already used.');
|
||||
}
|
||||
|
||||
public function getRequiredOptions()
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
public function getRequiredOptions(): array
|
||||
{
|
||||
return ['fields'];
|
||||
}
|
||||
|
@ -74,7 +77,7 @@ class UniqueEntity extends Constraint
|
|||
return self::CLASS_CONSTRAINT;
|
||||
}
|
||||
|
||||
public function getDefaultOption()
|
||||
public function getDefaultOption(): string
|
||||
{
|
||||
return 'fields';
|
||||
}
|
||||
|
|
|
@ -37,7 +37,7 @@ class UniqueEntityValidator extends ConstraintValidator
|
|||
* @throws UnexpectedTypeException
|
||||
* @throws ConstraintDefinitionException
|
||||
*/
|
||||
public function validate($entity, Constraint $constraint)
|
||||
public function validate($entity, Constraint $constraint): void
|
||||
{
|
||||
if (!$constraint instanceof UniqueEntity) {
|
||||
throw new UnexpectedTypeException($constraint, UniqueEntity::class);
|
||||
|
@ -119,7 +119,8 @@ class UniqueEntityValidator extends ConstraintValidator
|
|||
if (!$entity instanceof $supportedClass) {
|
||||
throw new ConstraintDefinitionException(
|
||||
sprintf(
|
||||
'The "%s" entity repository does not support the "%s" entity. The entity should be an instance of or extend "%s".',
|
||||
'The "%s" entity repository does not support the "%s" entity. The entity should be '
|
||||
. 'an instance of or extend "%s".',
|
||||
$constraint->entityClass,
|
||||
$class->getName(),
|
||||
$supportedClass
|
||||
|
@ -172,7 +173,7 @@ class UniqueEntityValidator extends ConstraintValidator
|
|||
->addViolation();
|
||||
}
|
||||
|
||||
private function formatWithIdentifiers($em, $class, $value)
|
||||
private function formatWithIdentifiers($em, $class, $value): string
|
||||
{
|
||||
if (!\is_object($value) || $value instanceof \DateTimeInterface) {
|
||||
return $this->formatValue($value, self::PRETTY_DATE);
|
||||
|
@ -201,7 +202,7 @@ class UniqueEntityValidator extends ConstraintValidator
|
|||
|
||||
array_walk(
|
||||
$identifiers,
|
||||
function (&$id, $field) {
|
||||
function (&$id, $field): void {
|
||||
if (!\is_object($id) || $id instanceof \DateTimeInterface) {
|
||||
$idAsString = $this->formatValue($id, self::PRETTY_DATE);
|
||||
} else {
|
||||
|
|
Loading…
Reference in New Issue