-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathSpatial.php
More file actions
185 lines (153 loc) · 5 KB
/
Spatial.php
File metadata and controls
185 lines (153 loc) · 5 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<?php
namespace Utopia\Database\Validator;
use Utopia\Database\Database;
use Utopia\Validator;
class Spatial extends Validator
{
private string $spatialType;
protected string $message = '';
public function __construct(string $spatialType)
{
$this->spatialType = $spatialType;
}
/**
* Validate POINT data
*
* @param array<mixed> $value
* @return bool
*/
protected function validatePoint(array $value): bool
{
if (count($value) !== 2) {
$this->message = 'Point must be an array of two numeric values [x, y]';
return false;
}
if (!is_numeric($value[0]) || !is_numeric($value[1])) {
$this->message = 'Point coordinates must be numeric values';
return false;
}
return true;
}
/**
* Validate LINESTRING data
*
* @param array<mixed> $value
* @return bool
*/
protected function validateLineString(array $value): bool
{
if (count($value) < 2) {
$this->message = 'LineString must contain at least two points';
return false;
}
foreach ($value as $point) {
if (!is_array($point) || count($point) !== 2) {
$this->message = 'Each point in LineString must be an array of two values [x, y]';
return false;
}
if (!is_numeric($point[0]) || !is_numeric($point[1])) {
$this->message = 'Each point in LineString must have numeric coordinates';
return false;
}
}
return true;
}
/**
* Validate POLYGON data
*
* @param array<mixed> $value
* @return bool
*/
protected function validatePolygon(array $value): bool
{
if (empty($value)) {
$this->message = 'Polygon must contain at least one ring';
return false;
}
// Detect single-ring polygon: [[x, y], [x, y], ...]
$isSingleRing = isset($value[0]) && is_array($value[0]) &&
count($value[0]) === 2 &&
is_numeric($value[0][0]) &&
is_numeric($value[0][1]);
if ($isSingleRing) {
$value = [$value]; // wrap single ring
}
foreach ($value as $ringIndex => $ring) {
if (!is_array($ring) || empty($ring)) {
$this->message = "Ring #{$ringIndex} must be an array of points";
return false;
}
if (count($ring) < 4) {
$this->message = "Ring #{$ringIndex} must contain at least 4 points to form a closed polygon";
return false;
}
foreach ($ring as $pointIndex => $point) {
if (!is_array($point) || count($point) !== 2) {
$this->message = "Point #{$pointIndex} in ring #{$ringIndex} must be an array of two values [x, y]";
return false;
}
if (!is_numeric($point[0]) || !is_numeric($point[1])) {
$this->message = "Coordinates of point #{$pointIndex} in ring #{$ringIndex} must be numeric";
return false;
}
}
// Check that the ring is closed (first point == last point)
if ($ring[0] !== $ring[count($ring) - 1]) {
$this->message = "Ring #{$ringIndex} must be closed (first point must equal last point)";
return false;
}
}
return true;
}
/**
* Check if a value is valid WKT string
*/
public static function isWKTString(string $value): bool
{
$value = trim($value);
return (bool) preg_match('/^(POINT|LINESTRING|POLYGON)\s*\(/i', $value);
}
public function getDescription(): string
{
return 'Value must be a valid ' . $this->spatialType . ": {$this->message}";
}
public function isArray(): bool
{
return false;
}
public function getType(): string
{
return self::TYPE_ARRAY;
}
public function getSpatialType(): string
{
return $this->spatialType;
}
/**
* Main validation entrypoint
*/
public function isValid($value): bool
{
if (is_null($value)) {
return true;
}
if (is_string($value)) {
return self::isWKTString($value);
}
if (is_array($value)) {
switch ($this->spatialType) {
case Database::VAR_POINT:
return $this->validatePoint($value);
case Database::VAR_LINESTRING:
return $this->validateLineString($value);
case Database::VAR_POLYGON:
return $this->validatePolygon($value);
default:
$this->message = 'Unknown spatial type: ' . $this->spatialType;
return false;
}
}
$this->message = 'Spatial value must be array or WKT string';
return false;
}
}