Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/Validator/Constraints/BelgianEnterpriseNumber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace AssoConnect\ValidatorBundle\Validator\Constraints;

use Symfony\Component\Validator\Constraint;

#[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::TARGET_METHOD)]
class BelgianEnterpriseNumber extends Constraint
{
public const string WRONG_FORMAT_ERROR = '3f7d2e1c-a845-4b9f-c631-52d78e04fb6a';
public const string MESSAGE = 'The Belgian Enterprise Number {{ value }} is not valid.';
}
35 changes: 35 additions & 0 deletions src/Validator/Constraints/BelgianEnterpriseNumberValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?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;
use Symfony\Component\Validator\Exception\UnexpectedValueException;

class BelgianEnterpriseNumberValidator extends ConstraintValidator
{
public function validate(mixed $value, Constraint $constraint): void
{
if (!$constraint instanceof BelgianEnterpriseNumber) {
throw new UnexpectedTypeException($constraint, BelgianEnterpriseNumber::class);
}

if (null === $value || '' === $value) {
return;
}

if (!is_string($value)) {
throw new UnexpectedValueException($value, 'string');
}

if (!ctype_digit($value) || strlen($value) !== 10 || !in_array($value[0], ['0', '1'], true)) {
$this->context->buildViolation($constraint::MESSAGE)
->setParameter('{{ value }}', $this->formatValue($value))
->setCode($constraint::WRONG_FORMAT_ERROR)
->addViolation();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

declare(strict_types=1);

namespace AssoConnect\ValidatorBundle\Tests\Validator\Constraints;

use AssoConnect\ValidatorBundle\Test\ConstraintValidatorTestCase;
use AssoConnect\ValidatorBundle\Validator\Constraints\BelgianEnterpriseNumber;
use AssoConnect\ValidatorBundle\Validator\Constraints\BelgianEnterpriseNumberValidator;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;

/**
* @extends ConstraintValidatorTestCase<BelgianEnterpriseNumberValidator>
*/
class BelgianEnterpriseNumberValidatorTest extends ConstraintValidatorTestCase
{
protected function getConstraint(): Constraint
{
return new BelgianEnterpriseNumber();
}

public function createValidator(): ConstraintValidator
{
return new BelgianEnterpriseNumberValidator();
}

public static function providerValidValues(): iterable
{
yield 'null value' => [null];
yield 'empty string' => [''];
yield 'valid number starting with 0' => ['0123456789'];
yield 'valid number starting with 1' => ['1234567890'];
yield 'valid number starting with 0 all zeros' => ['0000000000'];
yield 'valid number starting with 1 all digits' => ['1999999999'];
}

public static function providerInvalidValues(): iterable
{
$expectedMessage = BelgianEnterpriseNumber::MESSAGE;
$code = BelgianEnterpriseNumber::WRONG_FORMAT_ERROR;

yield 'too short' => ['012345678', $code, $expectedMessage];
yield 'too long' => ['01234567890', $code, $expectedMessage];
yield 'starts with 2' => ['2123456789', $code, $expectedMessage];
yield 'starts with 9' => ['9123456789', $code, $expectedMessage];
yield 'contains letters' => ['012345678A', $code, $expectedMessage];
yield 'contains special characters' => ['01234-6789', $code, $expectedMessage];
yield 'contains spaces' => ['0123 56789', $code, $expectedMessage];
yield 'single digit' => ['0', $code, $expectedMessage];
}
}
Loading