diff --git a/src/Entity/QRCode/QRCodePaymentIdentification.php b/src/Entity/QRCode/QRCodePaymentIdentification.php index 7845c08..cb777b1 100644 --- a/src/Entity/QRCode/QRCodePaymentIdentification.php +++ b/src/Entity/QRCode/QRCodePaymentIdentification.php @@ -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; } diff --git a/src/Validator/BankPaymentIdentificationNumber.php b/src/Validator/BankPaymentIdentificationNumber.php new file mode 100644 index 0000000..2f2b14e --- /dev/null +++ b/src/Validator/BankPaymentIdentificationNumber.php @@ -0,0 +1,31 @@ + 'NOT_BANKID_NUMBER_ERROR', + ]; + + public string $message = 'This value should be bank identification number.'; + + /** + * @param mixed|null $options + * @param string|null $message + * @param array|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; + } +} diff --git a/src/Validator/BankPaymentIdentificationNumberValidator.php b/src/Validator/BankPaymentIdentificationNumberValidator.php new file mode 100644 index 0000000..3459da2 --- /dev/null +++ b/src/Validator/BankPaymentIdentificationNumberValidator.php @@ -0,0 +1,35 @@ +context->buildViolation($constraint->message) + ->setParameter('{{ string }}', $value) + ->addViolation(); + } +} diff --git a/tests/Validator/BankPaymentIdentificationNumberValidatorTest.php b/tests/Validator/BankPaymentIdentificationNumberValidatorTest.php new file mode 100644 index 0000000..d9e08e9 --- /dev/null +++ b/tests/Validator/BankPaymentIdentificationNumberValidatorTest.php @@ -0,0 +1,40 @@ +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']]; + } +}