-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLongitudeValidator.php
More file actions
52 lines (42 loc) · 1.79 KB
/
LongitudeValidator.php
File metadata and controls
52 lines (42 loc) · 1.79 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
45
46
47
48
49
50
51
52
<?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\LessThanOrEqualValidator;
use Symfony\Component\Validator\Constraints\Regex;
use Symfony\Component\Validator\Constraints\RegexValidator;
use Symfony\Component\Validator\Constraints\Type;
use Symfony\Component\Validator\Constraints\TypeValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
class LongitudeValidator extends ComposeValidator
{
public const REGEX = '/^-?[0-9]+\.?[0-9]*$/';
public function getSupportedConstraint(): string
{
return Longitude::class;
}
protected function isEmptyStringAccepted(): bool
{
return false;
}
public function getValidatorsAndConstraints(mixed $value, Constraint $constraint): array
{
if (!$constraint instanceof Longitude) {
throw new UnexpectedTypeException($constraint, Longitude::class);
}
if (is_string($value)) {
if (preg_match(self::REGEX, $value) !== 1) {
return [new ValidatorAndConstraint(new RegexValidator(), new Regex(self::REGEX))];
}
return [
new ValidatorAndConstraint(new GreaterThanOrEqualValidator(), new GreaterThanOrEqual(-180)),
new ValidatorAndConstraint(new LessThanOrEqualValidator(), new LessThanOrEqual(180)),
];
}
return [new ValidatorAndConstraint(new TypeValidator(), new Type('string'))];
}
}