Skip to content

[Validator] feature coordinates validator #61313

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: 7.4
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/Symfony/Component/Validator/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,8 @@ CHANGELOG
}
}
```

* Add Latitude and Longitude Constraints

7.3
---

Expand Down
33 changes: 33 additions & 0 deletions src/Symfony/Component/Validator/Constraints/Latitude.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Validator\Constraints;

use Symfony\Component\Validator\Constraint;

#[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]
final class Latitude extends Constraint
{
public const INVALID_LATITUDE_ERROR = '2f01c7bf-43ec-487c-a173-bcc305d3bbd1';

protected const ERROR_NAMES = [
self::INVALID_LATITUDE_ERROR => 'INVALID_LATITUDE_ERROR',
];

public function __construct(
public string $mode = 'strict',
public string $message = 'This value must contain valid latitude coordinates.',
?array $groups = null,
mixed $payload = null,
) {
parent::__construct(null, $groups, $payload);
}
}
51 changes: 51 additions & 0 deletions src/Symfony/Component/Validator/Constraints/LatitudeValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Validator\Constraints;

use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
use Symfony\Component\Validator\Exception\UnexpectedValueException;

final class LatitudeValidator extends ConstraintValidator
{
public function validate(mixed $value, Constraint $constraint): void
{
if (!$constraint instanceof Latitude) {
throw new UnexpectedTypeException($constraint, Latitude::class);
}

if (null === $value || '' === $value) {
return;
}

if (!\is_scalar($value) && !$value instanceof \Stringable) {
throw new UnexpectedValueException($value, 'string');
}

// Accept only strings or numbers
if (!\is_string($value) && !is_numeric($value)) {
$this->context->buildViolation($constraint->message)
->setParameter('{{ value }}', $value)
->addViolation();

return;
}

if (!preg_match('/^[-+]?([1-8]?\d(\.\d+)?|90(\.0+)?)$/', (string) $value)) {
$this->context->buildViolation($constraint->message)
->setParameter('{{ value }}', $value)
->setCode(Latitude::INVALID_LATITUDE_ERROR)
->addViolation();
}
}
}
33 changes: 33 additions & 0 deletions src/Symfony/Component/Validator/Constraints/Longitude.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Validator\Constraints;

use Symfony\Component\Validator\Constraint;

#[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]
final class Longitude extends Constraint
{
public const INVALID_LONGITUDE_ERROR = '2984c3a9-702d-40bb-b53e-74d81de37ea2';

protected const ERROR_NAMES = [
self::INVALID_LONGITUDE_ERROR => 'INVALID_LONGITUDE_ERROR',
];

public function __construct(
public string $mode = 'strict',
public string $message = 'This value must contain valid longitude coordinates.',
?array $groups = null,
mixed $payload = null,
) {
parent::__construct(null, $groups, $payload);
}
}
51 changes: 51 additions & 0 deletions src/Symfony/Component/Validator/Constraints/LongitudeValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Validator\Constraints;

use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
use Symfony\Component\Validator\Exception\UnexpectedValueException;

final class LongitudeValidator extends ConstraintValidator
{
public function validate(mixed $value, Constraint $constraint): void
{
if (!$constraint instanceof Longitude) {
throw new UnexpectedTypeException($constraint, Longitude::class);
}

if (null === $value || '' === $value) {
return;
}

if (!\is_scalar($value) && !$value instanceof \Stringable) {
throw new UnexpectedValueException($value, 'string');
}

// Accept only strings or numbers
if (!\is_string($value) && !is_numeric($value)) {
$this->context->buildViolation($constraint->message)
->setParameter('{{ value }}', $value)
->addViolation();

return;
}

if (!preg_match('/^[-+]?(180(\.0+)?|((1[0-7]\d)|([1-9]?\d))(\.\d+)?)$/', (string) $value)) {
$this->context->buildViolation($constraint->message)
->setParameter('{{ value }}', '"'.$value.'"')
->setCode(Longitude::INVALID_LONGITUDE_ERROR)
->addViolation();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,14 @@
<source>This value is not a valid Twig template.</source>
<target state="needs-review-translation">Hierdie waarde is nie 'n geldige Twig-sjabloon nie.</target>
</trans-unit>
<trans-unit id="122">
<source>This value must contain valid latitude coordinates.</source>
<target state="needs-translation">This value must contain valid latitude coordinates.</target>
</trans-unit>
<trans-unit id="123">
<source>This value must contain valid longitude coordinates.</source>
<target state="needs-translation">This value must contain valid longitude coordinates.</target>
</trans-unit>
</body>
</file>
</xliff>
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,14 @@
<source>This value is not a valid Twig template.</source>
<target state="needs-review-translation">هذه القيمة ليست نموذج Twig صالح.</target>
</trans-unit>
<trans-unit id="122">
<source>This value must contain valid latitude coordinates.</source>
<target>هذه القيمة يجب أن تحتوي على إحداثيات خط عرض صالحة.</target>
</trans-unit>
<trans-unit id="123">
<source>This value must contain valid longitude coordinates.</source>
<target>هذه القيمة يجب أن تحتوي على إحداثيات خط طول صالحة.</target>
</trans-unit>
</body>
</file>
</xliff>
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,14 @@
<source>This value is not a valid Twig template.</source>
<target state="needs-review-translation">Bu dəyər etibarlı Twig şablonu deyil.</target>
</trans-unit>
<trans-unit id="122">
<source>This value must contain valid latitude coordinates.</source>
<target state="needs-translation">This value must contain valid latitude coordinates.</target>
</trans-unit>
<trans-unit id="123">
<source>This value must contain valid longitude coordinates.</source>
<target state="needs-translation">This value must contain valid longitude coordinates.</target>
</trans-unit>
</body>
</file>
</xliff>
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,14 @@
<source>This value is not a valid Twig template.</source>
<target>Гэта значэнне не з'яўляецца сапраўдным шаблонам Twig.</target>
</trans-unit>
<trans-unit id="122">
<source>This value must contain valid latitude coordinates.</source>
<target state="needs-translation">This value must contain valid latitude coordinates.</target>
</trans-unit>
<trans-unit id="123">
<source>This value must contain valid longitude coordinates.</source>
<target state="needs-translation">This value must contain valid longitude coordinates.</target>
</trans-unit>
</body>
</file>
</xliff>
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,14 @@
<source>This value is not a valid Twig template.</source>
<target state="needs-review-translation">Тази стойност не е валиден Twig шаблон.</target>
</trans-unit>
<trans-unit id="122">
<source>This value must contain valid latitude coordinates.</source>
<target state="needs-translation">This value must contain valid latitude coordinates.</target>
</trans-unit>
<trans-unit id="123">
<source>This value must contain valid longitude coordinates.</source>
<target state="needs-translation">This value must contain valid longitude coordinates.</target>
</trans-unit>
</body>
</file>
</xliff>
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,14 @@
<source>This value is not a valid Twig template.</source>
<target state="needs-review-translation">Ova vrijednost nije važeći Twig šablon.</target>
</trans-unit>
<trans-unit id="122">
<source>This value must contain valid latitude coordinates.</source>
<target state="needs-translation">This value must contain valid latitude coordinates.</target>
</trans-unit>
<trans-unit id="123">
<source>This value must contain valid longitude coordinates.</source>
<target state="needs-translation">This value must contain valid longitude coordinates.</target>
</trans-unit>
</body>
</file>
</xliff>
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,14 @@
<source>This value is not a valid Twig template.</source>
<target state="needs-review-translation">Aquest valor no és una plantilla Twig vàlida.</target>
</trans-unit>
<trans-unit id="122">
<source>This value must contain valid latitude coordinates.</source>
<target state="needs-translation">This value must contain valid latitude coordinates.</target>
</trans-unit>
<trans-unit id="123">
<source>This value must contain valid longitude coordinates.</source>
<target state="needs-translation">This value must contain valid longitude coordinates.</target>
</trans-unit>
</body>
</file>
</xliff>
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,14 @@
<source>This value is not a valid Twig template.</source>
<target>Tato hodnota není platná Twig šablona.</target>
</trans-unit>
<trans-unit id="122">
<source>This value must contain valid latitude coordinates.</source>
<target state="needs-translation">This value must contain valid latitude coordinates.</target>
</trans-unit>
<trans-unit id="123">
<source>This value must contain valid longitude coordinates.</source>
<target state="needs-translation">This value must contain valid longitude coordinates.</target>
</trans-unit>
</body>
</file>
</xliff>
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,14 @@
<source>This value is not a valid Twig template.</source>
<target state="needs-review-translation">Nid yw'r gwerth hwn yn dempled Twig dilys.</target>
</trans-unit>
<trans-unit id="122">
<source>This value must contain valid latitude coordinates.</source>
<target state="needs-translation">This value must contain valid latitude coordinates.</target>
</trans-unit>
<trans-unit id="123">
<source>This value must contain valid longitude coordinates.</source>
<target state="needs-translation">This value must contain valid longitude coordinates.</target>
</trans-unit>
</body>
</file>
</xliff>
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,14 @@
<source>This value is not a valid Twig template.</source>
<target state="needs-review-translation">Denne værdi er ikke en gyldig Twig-skabelon.</target>
</trans-unit>
<trans-unit id="122">
<source>This value must contain valid latitude coordinates.</source>
<target state="needs-translation">This value must contain valid latitude coordinates.</target>
</trans-unit>
<trans-unit id="123">
<source>This value must contain valid longitude coordinates.</source>
<target state="needs-translation">This value must contain valid longitude coordinates.</target>
</trans-unit>
</body>
</file>
</xliff>
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,14 @@
<source>This value is not a valid Twig template.</source>
<target>Dieser Wert ist kein valides Twig-Template.</target>
</trans-unit>
<trans-unit id="122">
<source>This value must contain valid latitude coordinates.</source>
<target state="needs-translation">This value must contain valid latitude coordinates.</target>
</trans-unit>
<trans-unit id="123">
<source>This value must contain valid longitude coordinates.</source>
<target state="needs-translation">This value must contain valid longitude coordinates.</target>
</trans-unit>
</body>
</file>
</xliff>
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,14 @@
<source>This value is not a valid Twig template.</source>
<target state="needs-review-translation">Αυτή η τιμή δεν είναι έγκυρο πρότυπο Twig.</target>
</trans-unit>
<trans-unit id="122">
<source>This value must contain valid latitude coordinates.</source>
<target state="needs-translation">This value must contain valid latitude coordinates.</target>
</trans-unit>
<trans-unit id="123">
<source>This value must contain valid longitude coordinates.</source>
<target state="needs-translation">This value must contain valid longitude coordinates.</target>
</trans-unit>
</body>
</file>
</xliff>
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,14 @@
<source>This value is not a valid Twig template.</source>
<target>This value is not a valid Twig template.</target>
</trans-unit>
<trans-unit id="122">
<source>This value must contain valid latitude coordinates.</source>
<target>This value must contain valid latitude coordinates.</target>
</trans-unit>
<trans-unit id="123">
<source>This value must contain valid longitude coordinates.</source>
<target>This value must contain valid longitude coordinates.</target>
</trans-unit>
</body>
</file>
</xliff>
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,14 @@
<source>This value is not a valid Twig template.</source>
<target state="needs-review-translation">Este valor no es una plantilla Twig válida.</target>
</trans-unit>
<trans-unit id="122">
<source>This value must contain valid latitude coordinates.</source>
<target state="needs-translation">This value must contain valid latitude coordinates.</target>
</trans-unit>
<trans-unit id="123">
<source>This value must contain valid longitude coordinates.</source>
<target state="needs-translation">This value must contain valid longitude coordinates.</target>
</trans-unit>
</body>
</file>
</xliff>
Loading
Loading