-
Notifications
You must be signed in to change notification settings - Fork 1
/
CoordinateArrayConstraint.php
50 lines (41 loc) · 1.36 KB
/
CoordinateArrayConstraint.php
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
<?php
declare(strict_types=1);
namespace Chubbyphp\Validation\Constraint;
use Chubbyphp\Validation\Error\Error;
use Chubbyphp\Validation\Error\ErrorInterface;
use Chubbyphp\Validation\ValidatorContextInterface;
use Chubbyphp\Validation\ValidatorInterface;
final class CoordinateArrayConstraint implements ConstraintInterface
{
/**
* @param mixed $value
*
* @return array<ErrorInterface>
*/
public function validate(
string $path,
$value,
ValidatorContextInterface $context,
?ValidatorInterface $validator = null
) {
if (null === $value) {
return [];
}
if (!\is_array($value)) {
return [new Error(
$path,
'constraint.coordinatearray.invalidtype',
['type' => get_debug_type($value)]
)];
}
if (!isset($value['lat']) || !isset($value['lon'])) {
return [new Error($path, 'constraint.coordinatearray.invalidformat', ['value' => json_encode($value, JSON_THROW_ON_ERROR)])];
}
$lat = $value['lat'];
$lon = $value['lon'];
if ($lat < -90 || $lat > 90 || $lon < -180 || $lon > 180) {
return [new Error($path, 'constraint.coordinatearray.invalidvalue', ['value' => json_encode($value, JSON_THROW_ON_ERROR)])];
}
return [];
}
}