-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFrenchRnaValidator.php
More file actions
executable file
·38 lines (32 loc) · 1.21 KB
/
FrenchRnaValidator.php
File metadata and controls
executable file
·38 lines (32 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<?php
declare(strict_types=1);
namespace AssoConnect\ValidatorBundle\Validator\Constraints;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
class FrenchRnaValidator extends ConstraintValidator
{
/**
* @inheritDoc
*/
public function validate(mixed $value, Constraint $constraint): void
{
if (!$constraint instanceof FrenchRna) {
throw new UnexpectedTypeException($constraint, FrenchRna::class);
}
if (null === $value || '' === $value) {
return;
}
// Source : https://www.data.gouv.fr/fr/datasets/repertoire-national-des-associations/
// the "ABGMCRSTJ" is a specific letter for the DOM-TOM and Corse
if (
!is_string($value) ||
preg_match('/(^W\d[\dABGMCRSTJ]\d{7}$)|(^\d[\dABGMRT]\d[PS]((02[BA])|(\d{3}))\d{7}$)/', $value) !== 1
) {
$this->context->buildViolation($constraint->message)
->setParameter('{{ value }}', $this->formatValue($value))
->setCode(FrenchRna::INVALID_FORMAT_ERROR)
->addViolation();
}
}
}