mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-10-22 13:12:49 +00:00
Refactor validation of PersonIdentifier
This commit is contained in:
@@ -13,10 +13,10 @@ namespace Chill\PersonBundle\PersonIdentifier\Identifier;
|
||||
|
||||
use Chill\PersonBundle\Entity\Identifier\PersonIdentifier;
|
||||
use Chill\PersonBundle\Entity\Identifier\PersonIdentifierDefinition;
|
||||
use Chill\PersonBundle\PersonIdentifier\IdentifierViolationDTO;
|
||||
use Chill\PersonBundle\PersonIdentifier\PersonIdentifierEngineInterface;
|
||||
use Symfony\Component\Form\Extension\Core\Type\TextType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Symfony\Component\Validator\Context\ExecutionContextInterface;
|
||||
|
||||
final readonly class StringIdentifier implements PersonIdentifierEngineInterface
|
||||
{
|
||||
@@ -50,20 +50,24 @@ final readonly class StringIdentifier implements PersonIdentifierEngineInterface
|
||||
return '' === trim($identifier->getValue()['content'] ?? '');
|
||||
}
|
||||
|
||||
public function validate(ExecutionContextInterface $context, PersonIdentifier $identifier, PersonIdentifierDefinition $definition): void
|
||||
public function validate(PersonIdentifier $identifier, PersonIdentifierDefinition $definition): array
|
||||
{
|
||||
$config = $definition->getData();
|
||||
$content = (string) ($identifier->getValue()['content'] ?? '');
|
||||
$violations = [];
|
||||
|
||||
if (($config[self::ONLY_NUMBERS] ?? false) && !preg_match('/^[0-9]+$/', $content)) {
|
||||
$context->buildViolation('person_identifier.only_number')
|
||||
->addViolation();
|
||||
$violations[] = new IdentifierViolationDTO('person_identifier.only_number', '2a3352c0-a2b9-11f0-a767-b7a3f80e52f1');
|
||||
}
|
||||
|
||||
if (null !== ($config[self::FIXED_LENGTH] ?? null) && strlen($content) !== $config[self::FIXED_LENGTH]) {
|
||||
$context->buildViolation('person_identifier.fixed_length')
|
||||
->setParameter('limit', (string) $config[self::FIXED_LENGTH])
|
||||
->addViolation();
|
||||
$violations[] = new IdentifierViolationDTO(
|
||||
'person_identifier.fixed_length',
|
||||
'2b02a8fe-a2b9-11f0-bfe5-033300972783',
|
||||
['limit' => (string) $config[self::FIXED_LENGTH]]
|
||||
);
|
||||
}
|
||||
|
||||
return $violations;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user