Repository navigation
Expand file tree
/
Copy pathLineStringValidator.php
More file actions
59 lines (50 loc) · 2.17 KB
/
Copy pathLineStringValidator.php
File metadata and controls
59 lines (50 loc) · 2.17 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
53
54
55
56
57
58
59
<?php
/**
* This file is part of the spatial project.
*
* PHP 8.4 | 8.5
*
* Copyright Alexandre Tranchant <alexandre.tranchant@gmail.com> 2024-2026
* Copyright Longitude One 2024-2026
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace LongitudeOne\SpatialValidator\Validator\Constraints;
use LongitudeOne\Core\Enum\GeometryTypeEnum;
use LongitudeOne\SpatialTypes\Interfaces\LineStringInterface;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
use Symfony\Component\Validator\Exception\UnexpectedValueException;
/** Compose point, component and elementary line rules. */
final class LineStringValidator extends ConstraintValidator
{
/**
* @param mixed $value Spatial object to validate
* @param Constraint $constraint Applied constraint
*/
public function validate(mixed $value, Constraint $constraint): void
{
if (!$constraint instanceof LineString) {
throw new UnexpectedTypeException($constraint, LineString::class);
}
// Allow optional spatial values; use NotNull separately when a value is required.
if (null === $value) {
return;
}
if (!$value instanceof LineStringInterface) {
throw new UnexpectedValueException($value, LineStringInterface::class);
}
if (GeometryTypeEnum::LINESTRING !== $value->getType()) {
throw new UnexpectedValueException($value, GeometryTypeEnum::LINESTRING->value);
}
$this->context->getValidator()->inContext($this->context)->validate($value, [
new Components(groups: $constraint->groups, payload: $constraint->payload),
new MinimumPointCount(groups: $constraint->groups, payload: $constraint->payload),
new NonEmptyPoints(groups: $constraint->groups, payload: $constraint->payload),
new NoConsecutiveDuplicatePoints(groups: $constraint->groups, payload: $constraint->payload),
], $this->context->getGroup());
}
}