-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMoneyValidator.php
More file actions
44 lines (36 loc) · 1.48 KB
/
MoneyValidator.php
File metadata and controls
44 lines (36 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<?php
declare(strict_types=1);
namespace AssoConnect\ValidatorBundle\Validator\Constraints;
use AssoConnect\ValidatorBundle\Dto\ValidatorAndConstraint;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Constraints\GreaterThanOrEqual;
use Symfony\Component\Validator\Constraints\GreaterThanOrEqualValidator;
use Symfony\Component\Validator\Constraints\LessThanOrEqual;
use Symfony\Component\Validator\Constraints\LessThanValidator;
use Symfony\Component\Validator\Constraints\Type;
use Symfony\Component\Validator\Constraints\TypeValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
class MoneyValidator extends ComposeValidator
{
public function getSupportedConstraint(): string
{
return Money::class;
}
public function getValidatorsAndConstraints(mixed $value, Constraint $constraint): array
{
if (!$constraint instanceof Money) {
throw new UnexpectedTypeException($constraint, Money::class);
}
if (is_float($value) || is_integer($value)) {
return [
new ValidatorAndConstraint(new GreaterThanOrEqualValidator(), new GreaterThanOrEqual($constraint->min)),
new ValidatorAndConstraint(new LessThanValidator(), new LessThanOrEqual($constraint->max)),
];
}
return [new ValidatorAndConstraint(new TypeValidator(), new Type('float'))];
}
protected function isEmptyStringAccepted(): bool
{
return false;
}
}