feat: add MoneyValidator
fix: MoneyValidator MoneyValidator fix: moneyvalidator (fakt) moneyValidator
This commit is contained in:
parent
1437ce4558
commit
3cb6f7488f
@ -4,6 +4,7 @@ declare(strict_types=1);
|
||||
namespace App\Entity\QRCode;
|
||||
|
||||
use App\Validator\Currency;
|
||||
use App\Validator\Money;
|
||||
use Symfony\Component\Validator\Constraints as Assert;
|
||||
|
||||
/**
|
||||
@ -15,6 +16,7 @@ class QRCodeMoney
|
||||
message: 'messages.not_valid_amount',
|
||||
)]
|
||||
#[Assert\NotBlank(message: 'messages.fill_value')]
|
||||
#[Money(message: 'messages.not_valid_amount')]
|
||||
private ?string $amount;
|
||||
|
||||
// TODO: getCurrencies validation
|
||||
|
31
src/Validator/Money.php
Normal file
31
src/Validator/Money.php
Normal 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 Money extends Constraint
|
||||
{
|
||||
public const NOT_CURRENCY_ERROR = '478618e7-95ba-473d-9101-cabd45e49115';
|
||||
|
||||
protected const ERROR_NAMES = [
|
||||
self::NOT_CURRENCY_ERROR => 'NOT_CURRENCY_ERROR',
|
||||
];
|
||||
|
||||
public string $message = 'This value should be currency.';
|
||||
|
||||
/**
|
||||
* @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;
|
||||
}
|
||||
}
|
34
src/Validator/MoneyValidator.php
Normal file
34
src/Validator/MoneyValidator.php
Normal file
@ -0,0 +1,34 @@
|
||||
<?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 MoneyValidator extends ConstraintValidator {
|
||||
public function validate(mixed $value, Constraint $constraint): void
|
||||
{
|
||||
if (!$constraint instanceof Money) {
|
||||
throw new UnexpectedTypeException($constraint, Money::class);
|
||||
}
|
||||
|
||||
if (null === $value || '' === $value) {
|
||||
return ;
|
||||
}
|
||||
|
||||
if (is_string($value) === false) {
|
||||
throw new UnexpectedValueException($value, "string");
|
||||
}
|
||||
|
||||
if (preg_match('/^\d+([\.,]\d{0,2})?$/', $value)) {
|
||||
return ;
|
||||
}
|
||||
|
||||
$this->context->buildViolation($constraint->message)
|
||||
->setParameter('{{ string }}', $value)
|
||||
->addViolation();
|
||||
}
|
||||
}
|
40
tests/Validator/MoneyValidatorTest.php
Normal file
40
tests/Validator/MoneyValidatorTest.php
Normal file
@ -0,0 +1,40 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Tests\Validator;
|
||||
|
||||
use App\Validator\Money;
|
||||
use App\Validator\MoneyValidator;
|
||||
|
||||
class MoneyValidatorTest extends ValidatorTestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider successDp
|
||||
* @param string $generatedValue
|
||||
* @return void
|
||||
*/
|
||||
public function testSuccess(string $generatedValue): void {
|
||||
$moneyValidator = new MoneyValidator();
|
||||
$moneyValidator->initialize($this->createExecutionContext(false));
|
||||
$moneyValidator->validate($generatedValue, new Money(['message' => 'foo']));
|
||||
}
|
||||
|
||||
private function successDp(): array {
|
||||
return [['122'], ['122.0'], ['122,00'], ['122'], ['2.05'], ['2,04']];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider failureDp
|
||||
* @param string $generatedValue
|
||||
* @return void
|
||||
*/
|
||||
public function testFailure(string $generatedValue): void {
|
||||
$moneyValidator = new MoneyValidator();
|
||||
$moneyValidator->initialize($this->createExecutionContext(true));
|
||||
$moneyValidator->validate($generatedValue, new Money(['message' => 'foo']));
|
||||
}
|
||||
|
||||
private function failureDp(): array {
|
||||
return [['122.b'], ['a.a'], ['a'], ['2.040'], ['2,a']];
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user