mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-10-22 13:12:49 +00:00
Add validation logic and tests for StringIdentifier
- Implemented `validate` method in `StringIdentifier` to enforce `only_numbers` and `fixed_length` constraints. - Created `StringIdentifierValidationTest` to cover validation rules.
This commit is contained in:
@@ -16,11 +16,15 @@ use Chill\PersonBundle\Entity\Identifier\PersonIdentifierDefinition;
|
||||
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
|
||||
{
|
||||
public const NAME = 'chill-person-bundle.string-identifier';
|
||||
|
||||
private const ONLY_NUMBERS = 'only_numbers';
|
||||
private const FIXED_LENGTH = 'fixed_length';
|
||||
|
||||
public static function getName(): string
|
||||
{
|
||||
return self::NAME;
|
||||
@@ -45,4 +49,21 @@ final readonly class StringIdentifier implements PersonIdentifierEngineInterface
|
||||
{
|
||||
return '' === trim($identifier->getValue()['content'] ?? '');
|
||||
}
|
||||
|
||||
public function validate(ExecutionContextInterface $context, PersonIdentifier $identifier, PersonIdentifierDefinition $definition): void
|
||||
{
|
||||
$config = $definition->getData();
|
||||
$content = (string) ($identifier->getValue()['content'] ?? '');
|
||||
|
||||
if (($config[self::ONLY_NUMBERS] ?? false) && !preg_match('/^[0-9]+$/', $content)) {
|
||||
$context->buildViolation('person_identifier.only_number')
|
||||
->addViolation();
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user