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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ This Symfony bundle provides validators for common scalar values:
- [French RNA value](/src/Validator/Constraints/FrenchRnaValidator.php)
- [French SIREN value](/src/Validator/Constraints/FrenchSirenValidator.php)
- [French SIRET value](/src/Validator/Constraints/FrenchSiretValidator.php)
- [Dutch KVK number](/src/Validator/Constraints/DutchKvkNumberValidator.php)

See the different constraints source code for available options.

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"assoconnect/php-date": "^2.0",
"doctrine/orm": "^3.0",
"symfony/intl": "^7.0|^8.0",
"assoconnect/doctrine-types-bundle": "^v2.16",
"assoconnect/doctrine-types-bundle": "^2.18",
"assoconnect/php-percent":"^1.1",
"assoconnect/absolute-percent-value-bundle": "^1.5",
"webmozart/assert": "^1.11",
Expand Down
14 changes: 14 additions & 0 deletions src/Validator/Constraints/DutchKvkNumber.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 DutchKvkNumber extends Constraint
{
public const string WRONG_FORMAT_ERROR = '16a7f6c0-e16e-4db3-8ac0-4072bd00ed25';
public const string MESSAGE = 'The Dutch KVK Number {{ value }} is not valid.';
}
41 changes: 41 additions & 0 deletions src/Validator/Constraints/DutchKvkNumberValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?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;

/**
* Format-only validation of the Dutch KVK number (Kamer van Koophandel nummer),
* the registration number of organizations in the Dutch trade register: exactly
* 8 digits. KVK numbers carry no public check digit, so no checksum is verified,
* and presence stays the caller's decision.
*/
class DutchKvkNumberValidator extends ConstraintValidator
{
public function validate(mixed $value, Constraint $constraint): void
{
if (!$constraint instanceof DutchKvkNumber) {
throw new UnexpectedTypeException($constraint, DutchKvkNumber::class);
}

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

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

if (1 !== preg_match('/^\d{8}$/', $value)) {
$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,23 @@
<?php

declare(strict_types=1);

namespace AssoConnect\ValidatorBundle\Validator\ConstraintsSetProvider\Field;

use AssoConnect\DoctrineTypesBundle\Doctrine\DBAL\Types\DutchKvkNumberType;
use AssoConnect\ValidatorBundle\Validator\Constraints\DutchKvkNumber;

class DutchKvkNumberProvider implements FieldConstraintsSetProviderInterface
{
public function supports(string $type): bool
{
return DutchKvkNumberType::NAME === $type;
}

public function getConstraints(array $fieldMapping): array
{
return [
new DutchKvkNumber(),
];
}
}
51 changes: 51 additions & 0 deletions tests/Validator/Constraints/DutchKvkNumberValidatorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

declare(strict_types=1);

namespace AssoConnect\ValidatorBundle\Tests\Validator\Constraints;

use AssoConnect\ValidatorBundle\Test\ConstraintValidatorTestCase;
use AssoConnect\ValidatorBundle\Validator\Constraints\DutchKvkNumber;
use AssoConnect\ValidatorBundle\Validator\Constraints\DutchKvkNumberValidator;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;

/**
* @extends ConstraintValidatorTestCase<DutchKvkNumberValidator>
*/
class DutchKvkNumberValidatorTest extends ConstraintValidatorTestCase
{
protected function getConstraint(): Constraint
{
return new DutchKvkNumber();
}

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

public static function providerValidValues(): iterable
{
yield 'null value' => [null];
yield 'empty string' => [''];
// Adyen documentation example
yield 'valid KVK number' => ['34179503'];
yield 'valid KVK number with leading zero' => ['01234567'];
yield 'valid KVK number all zeros' => ['00000000'];
}

public static function providerInvalidValues(): iterable
{
$formatMessage = DutchKvkNumber::MESSAGE;
$formatCode = DutchKvkNumber::WRONG_FORMAT_ERROR;

yield 'too short' => ['3417950', $formatCode, $formatMessage];
yield 'too long' => ['341795031', $formatCode, $formatMessage];
yield 'contains letters' => ['3417950A', $formatCode, $formatMessage];
yield 'contains internal space' => ['3417 503', $formatCode, $formatMessage];
yield 'contains special characters' => ['3417-503', $formatCode, $formatMessage];
yield 'leading plus sign' => ['+4179503', $formatCode, $formatMessage];
yield 'single digit' => ['3', $formatCode, $formatMessage];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace AssoConnect\ValidatorBundle\Tests\Validator\ConstraintsSetProvider\Field;

use AssoConnect\ValidatorBundle\Test\FieldConstraintsSetProviderTestCase;
use AssoConnect\ValidatorBundle\Validator\Constraints\DutchKvkNumber;
use AssoConnect\ValidatorBundle\Validator\ConstraintsSetProvider\Field\DutchKvkNumberProvider;
use AssoConnect\ValidatorBundle\Validator\ConstraintsSetProvider\Field\FieldConstraintsSetProviderInterface;

class DutchKvkNumberProviderTest extends FieldConstraintsSetProviderTestCase
{
protected function getFactory(): FieldConstraintsSetProviderInterface
{
return new DutchKvkNumberProvider();
}

public static function getConstraintsForTypeProvider(): iterable
{
yield [['type' => 'dutchKvkNumber'], [new DutchKvkNumber()]];
}
}
Loading