Skip to content

Commit 61c92cf

Browse files
committed
Add Latitude Validator
1 parent f9e3400 commit 61c92cf

File tree

9 files changed

+253
-1
lines changed

9 files changed

+253
-1
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Validator\Constraints;
13+
14+
use Symfony\Component\Validator\Constraint;
15+
16+
#[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]
17+
final class Latitude extends Constraint
18+
{
19+
public const INVALID_LATITUDE_ERROR = '2f01c7bf-43ec-487c-a173-bcc305d3bbd1';
20+
21+
protected const ERROR_NAMES = [
22+
self::INVALID_LATITUDE_ERROR => 'INVALID_LATITUDE_ERROR',
23+
];
24+
25+
public function __construct(
26+
public string $mode = 'strict',
27+
public string $message = 'This value must contain valid latitude coordinates.',
28+
?array $groups = null,
29+
mixed $payload = null
30+
) {
31+
parent::__construct(null, $groups, $payload);
32+
}
33+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Validator\Constraints;
13+
14+
use Symfony\Component\Validator\Constraint;
15+
use Symfony\Component\Validator\ConstraintValidator;
16+
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
17+
use Symfony\Component\Validator\Exception\UnexpectedValueException;
18+
19+
final class LatitudeValidator extends ConstraintValidator
20+
{
21+
public function validate(mixed $value, Constraint $constraint): void
22+
{
23+
if (!$constraint instanceof Latitude) {
24+
throw new UnexpectedTypeException($constraint, Latitude::class);
25+
}
26+
27+
if (null === $value || '' === $value) {
28+
return;
29+
}
30+
31+
if (!\is_scalar($value) && !$value instanceof \Stringable) {
32+
throw new UnexpectedValueException($value, 'string');
33+
}
34+
35+
// Accept only strings or numbers
36+
if (!is_string($value) && !is_numeric($value)) {
37+
$this->context->buildViolation($constraint->message)
38+
->setParameter('{{ value }}', $value)
39+
->addViolation();
40+
return;
41+
}
42+
43+
if (!preg_match('/^[-+]?([1-8]?\d(\.\d+)?|90(\.0+)?)$/', (string) $value)) {
44+
$this->context->buildViolation($constraint->message)
45+
->setParameter('{{ value }}', $value)
46+
->setCode(Latitude::INVALID_LATITUDE_ERROR)
47+
->addViolation();
48+
}
49+
}
50+
}

src/Symfony/Component/Validator/Constraints/Longitude.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,6 @@ public function __construct(
2828
?array $groups = null,
2929
mixed $payload = null
3030
) {
31-
parent::__construct([], $groups, $payload);
31+
parent::__construct(null, $groups, $payload);
3232
}
3333
}

src/Symfony/Component/Validator/Resources/translations/validators.ar.xlf

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,14 @@
470470
<source>This value is not a valid Twig template.</source>
471471
<target state="needs-review-translation">هذه القيمة ليست نموذج Twig صالح.</target>
472472
</trans-unit>
473+
<trans-unit id="122">
474+
<source>This value must contain valid latitude coordinates.</source>
475+
<target>هذه القيمة يجب أن تحتوي على إحداثيات خط عرض صالحة.</target>
476+
</trans-unit>
477+
<trans-unit id="123">
478+
<source>This value must contain valid longitude coordinates.</source>
479+
<target>هذه القيمة يجب أن تحتوي على إحداثيات خط طول صالحة.</target>
480+
</trans-unit>
473481
</body>
474482
</file>
475483
</xliff>

src/Symfony/Component/Validator/Resources/translations/validators.en.xlf

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,14 @@
470470
<source>This value is not a valid Twig template.</source>
471471
<target>This value is not a valid Twig template.</target>
472472
</trans-unit>
473+
<trans-unit id="122">
474+
<source>This value must contain valid latitude coordinates.</source>
475+
<target>This value must contain valid latitude coordinates.</target>
476+
</trans-unit>
477+
<trans-unit id="123">
478+
<source>This value must contain valid longitude coordinates.</source>
479+
<target>This value must contain valid longitude coordinates.</target>
480+
</trans-unit>
473481
</body>
474482
</file>
475483
</xliff>

src/Symfony/Component/Validator/Resources/translations/validators.fr.xlf

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,14 @@
470470
<source>This value is not a valid Twig template.</source>
471471
<target>Cette valeur n'est pas un modèle Twig valide.</target>
472472
</trans-unit>
473+
<trans-unit id="122">
474+
<source>This value must contain valid latitude coordinates.</source>
475+
<target>Cette valeur doit contenir des coordonnées de latitude valides.</target>
476+
</trans-unit>
477+
<trans-unit id="123">
478+
<source>This value must contain valid longitude coordinates.</source>
479+
<target>Cette valeur doit contenir des coordonnées de longitude valides.</target>
480+
</trans-unit>
473481
</body>
474482
</file>
475483
</xliff>
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Validator\Tests\Constraints;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Validator\Constraints\Latitude;
16+
use Symfony\Component\Validator\Mapping\ClassMetadata;
17+
use Symfony\Component\Validator\Mapping\Loader\AttributeLoader;
18+
19+
class LatitudeTest extends TestCase
20+
{
21+
public function testAttributes()
22+
{
23+
$metadata = new ClassMetadata(LatitudeDummy::class);
24+
$loader = new AttributeLoader();
25+
self::assertTrue($loader->loadClassMetadata($metadata));
26+
27+
[$bConstraint] = $metadata->properties['b']->getConstraints();
28+
self::assertSame('myMessage', $bConstraint->message);
29+
self::assertSame(['Default', 'LatitudeDummy'], $bConstraint->groups);
30+
31+
[$cConstraint] = $metadata->properties['c']->getConstraints();
32+
self::assertSame(['my_group'], $cConstraint->groups);
33+
}
34+
}
35+
36+
class LatitudeDummy
37+
{
38+
#[Latitude]
39+
private $a;
40+
41+
#[Latitude(message: 'myMessage')]
42+
private $b;
43+
44+
#[Latitude(groups: ['my_group'])]
45+
private $c;
46+
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Validator\Tests\Constraints;
13+
14+
use Symfony\Component\Validator\Constraints\Latitude;
15+
use Symfony\Component\Validator\Constraints\LatitudeValidator;
16+
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
17+
18+
class LatitudeValidatorTest extends ConstraintValidatorTestCase
19+
{
20+
protected function createValidator(): LatitudeValidator
21+
{
22+
return new LatitudeValidator();
23+
}
24+
25+
/**
26+
* @dataProvider getValidValues
27+
*/
28+
public function testLatitudeIsValid($value)
29+
{
30+
$this->validator->validate($value, new Latitude());
31+
32+
$this->assertNoViolation();
33+
}
34+
35+
/**
36+
* @dataProvider getInvalidValues
37+
*/
38+
public function testInvalidValues($value)
39+
{
40+
$constraint = new Latitude(message: 'myMessageTest');
41+
42+
$this->validator->validate($value, $constraint);
43+
44+
$this->buildViolation('myMessageTest')
45+
->setParameter('{{ value }}', $value)
46+
->setCode(Latitude::INVALID_LATITUDE_ERROR)
47+
->assertRaised();
48+
}
49+
50+
public static function getValidValues()
51+
{
52+
return [
53+
[null],
54+
[''],
55+
['0'],
56+
[0],
57+
['90'],
58+
[90],
59+
['-90'],
60+
[-90],
61+
['89.9999'],
62+
[-89.9999],
63+
['45.123456'],
64+
[33.975738401584266],
65+
['+45.0'],
66+
['+0'],
67+
['+90.0'],
68+
['-0.0'],
69+
['0.0'],
70+
['45'],
71+
['-45'],
72+
['89'],
73+
['-89'],
74+
];
75+
}
76+
77+
public static function getInvalidValues()
78+
{
79+
return [
80+
['90.0001'],
81+
['-90.0001'],
82+
['91'],
83+
[-91],
84+
['180'],
85+
['-180'],
86+
['200'],
87+
[-200],
88+
['abc'],
89+
['--45'],
90+
['+'],
91+
[' '],
92+
['+90.1'],
93+
['-91'],
94+
['1,23'],
95+
['89,999'],
96+
];
97+
}
98+
}

src/Symfony/Component/Validator/Tests/Constraints/LongitudeValidatorTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ public static function getValidValues()
6363
['90'],
6464
['-90'],
6565
['45.123456'],
66+
[-6.824053096868544],
6667
['+45.0'],
6768
['+0'],
6869
['+180.0'],

0 commit comments

Comments
 (0)