feat: add BankPaymentIdentificationNumberValidator

This commit is contained in:
Ondrej Vlach 2024-01-17 19:05:08 +01:00
parent 9b6844486d
commit a7c7d5ed99
Signed by: ovlach
GPG Key ID: 4FF1A23B4914DE70
4 changed files with 121 additions and 35 deletions

View File

@ -3,74 +3,54 @@ declare(strict_types=1);
namespace App\Entity\QRCode;
use App\Validator\BankPaymentIdentificationNumber;
use Symfony\Component\Validator\Constraints as Assert;
/**
* Payment identification for QR code
*/
class QRCodePaymentIdentification {
#[Assert\NotBlank(message: 'messages.fill_value')]
#[Assert\Length( // <<< Ano, mohl jsem si napsat vlastni validator ...
min: 1,
max: 10,
minMessage: 'messages.not_variable_symbol',
maxMessage: 'messages.not_variable_symbol',
)] // https://en.wikipedia.org/wiki/Variable_symbol
#[Assert\Positive(
#[BankPaymentIdentificationNumber(
message: 'messages.not_variable_symbol',
)]
private string $variableSymbol;
private ?string $variableSymbol;
#[Assert\NotBlank(message: 'messages.fill_value')]
#[Assert\Length(
min: 1,
max: 10,
minMessage: 'messages.not_specific_symbol',
maxMessage: 'messages.not_specific_symbol',
)] // https://cs.wikipedia.org/wiki/Specifick%C3%BD_symbol
#[Assert\Positive(
message: 'The {{ value }} is not a variable symbol.',
#[BankPaymentIdentificationNumber(
message: 'messages.not_specific_symbol',
)]
private string $specificSymbol;
private ?string $specificSymbol;
#[Assert\NotBlank(message: 'messages.fill_value')]
#[Assert\Length(
min: 1,
max: 10,
minMessage: 'messages.not_constant_symbol',
maxMessage: 'messages.not_constant_symbol',
)] // https://www.hyponamiru.cz/en/glossary/constant-symbol/
#[Assert\Positive(
#[BankPaymentIdentificationNumber(
message: 'messages.not_constant_symbol',
)]
private string $constantSymbol;
)] // https://www.hyponamiru.cz/en/glossary/constant-symbol/
private ?string $constantSymbol;
public function getVariableSymbol(): string
public function getVariableSymbol(): ?string
{
return $this->variableSymbol;
}
public function setVariableSymbol(string $variableSymbol): void
public function setVariableSymbol(?string $variableSymbol): void
{
$this->variableSymbol = $variableSymbol;
}
public function getSpecificSymbol(): string
public function getSpecificSymbol(): ?string
{
return $this->specificSymbol;
}
public function setSpecificSymbol(string $specificSymbol): void
public function setSpecificSymbol(?string $specificSymbol): void
{
$this->specificSymbol = $specificSymbol;
}
public function getConstantSymbol(): string
public function getConstantSymbol(): ?string
{
return $this->constantSymbol;
}
public function setConstantSymbol(string $constantSymbol): void
public function setConstantSymbol(?string $constantSymbol): void
{
$this->constantSymbol = $constantSymbol;
}

View File

@ -0,0 +1,31 @@
<?php
declare(strict_types=1);
namespace App\Validator;
use Symfony\Component\Validator\Constraint;
#[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]
class BankPaymentIdentificationNumber extends Constraint
{
public const NOT_CURRENCY_ERROR = '478a18e7-95ba-473d-9101-cabd45e49115';
protected const ERROR_NAMES = [
self::NOT_CURRENCY_ERROR => 'NOT_BANKID_NUMBER_ERROR',
];
public string $message = 'This value should be bank identification number.';
/**
* @param mixed|null $options
* @param string|null $message
* @param array<string>|null $groups
* @param mixed|null $payload
*/
public function __construct(mixed $options = null, string $message = null, array $groups = null, mixed $payload = null)
{
parent::__construct($options ?? [], $groups, $payload);
$this->message = $message ?? $this->message;
}
}

View File

@ -0,0 +1,35 @@
<?php
declare(strict_types=1);
namespace App\Validator;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
use Symfony\Component\Validator\Exception\UnexpectedValueException;
class BankPaymentIdentificationNumberValidator extends ConstraintValidator
{
public function validate(mixed $value, Constraint $constraint): void
{
if (!$constraint instanceof BankPaymentIdentificationNumber) {
throw new UnexpectedTypeException($constraint, Money::class);
}
if (null === $value || '' === $value) {
return ;
}
if (is_string($value) === false) {
throw new UnexpectedValueException($value, "string");
}
if (strlen($value) <= 10 && filter_var($value, FILTER_VALIDATE_INT) !== false) {
return ;
}
$this->context->buildViolation($constraint->message)
->setParameter('{{ string }}', $value)
->addViolation();
}
}

View File

@ -0,0 +1,40 @@
<?php
declare(strict_types=1);
namespace App\Tests\Validator;
use App\Validator\BankPaymentIdentificationNumber;
use App\Validator\BankPaymentIdentificationNumberValidator;
class BankPaymentIdentificationNumberValidatorTest extends ValidatorTestCase
{
/**
* @dataProvider successDp
* @param string $generatedValue
* @return void
*/
public function testSuccess(string $generatedValue): void {
$bankPaymentIdentificationValidator = new BankPaymentIdentificationNumberValidator();
$bankPaymentIdentificationValidator->initialize($this->createExecutionContext(false));
$bankPaymentIdentificationValidator->validate($generatedValue, new BankPaymentIdentificationNumber(['message' => 'foo']));
}
private function successDp(): array {
return [['122'], ['1234567890'], ['1']];
}
/**
* @dataProvider failureDp
* @param string $generatedValue
* @return void
*/
public function testFailure(string $generatedValue): void {
$bankPaymentIdentificationValidator = new BankPaymentIdentificationNumberValidator();
$bankPaymentIdentificationValidator->initialize($this->createExecutionContext(true));
$bankPaymentIdentificationValidator->validate($generatedValue, new BankPaymentIdentificationNumber(['message' => 'foo']));
}
private function failureDp(): array {
return [['122.b'], ['a.a'], ['a'], ['2.040'], ['2,a'], ['122.1'], ['12345678901']];
}
}