33namespace Utopia \Database \Validator ;
44
55use Utopia \Database \Database ;
6- use Utopia \Database \Exception ;
76use Utopia \Validator ;
87
98class Spatial extends Validator
109{
1110 private string $ spatialType ;
11+ protected string $ message = '' ;
1212
1313 public function __construct (string $ spatialType )
1414 {
1515 $ this ->spatialType = $ spatialType ;
1616 }
1717
18- /**
19- * Validate spatial data according to its type
20- *
21- * @param mixed $value
22- * @param string $type
23- * @return bool
24- * @throws Exception
25- */
26- public static function validate (mixed $ value , string $ type ): bool
27- {
28- if (!is_array ($ value )) {
29- throw new Exception ('Spatial data must be provided as an array ' );
30- }
31-
32- switch ($ type ) {
33- case Database::VAR_POINT :
34- return self ::validatePoint ($ value );
35-
36- case Database::VAR_LINESTRING :
37- return self ::validateLineString ($ value );
38-
39- case Database::VAR_POLYGON :
40- return self ::validatePolygon ($ value );
41-
42- default :
43- throw new Exception ('Unknown spatial type: ' . $ type );
44- }
45- }
46-
4718 /**
4819 * Validate POINT data
49- *
50- * @param array<mixed,mixed> $value
51- * @return bool
52- * @throws Exception
5320 */
54- protected static function validatePoint (array $ value ): bool
21+ protected function validatePoint (array $ value ): bool
5522 {
5623 if (count ($ value ) !== 2 ) {
57- throw new Exception ('Point must be an array of two numeric values [x, y] ' );
24+ $ this ->message = 'Point must be an array of two numeric values [x, y] ' ;
25+ return false ;
5826 }
5927
6028 if (!is_numeric ($ value [0 ]) || !is_numeric ($ value [1 ])) {
61- throw new Exception ('Point coordinates must be numeric values ' );
29+ $ this ->message = 'Point coordinates must be numeric values ' ;
30+ return false ;
6231 }
6332
6433 return true ;
6534 }
6635
6736 /**
6837 * Validate LINESTRING data
69- *
70- * @param array<mixed,mixed> $value
71- * @return bool
72- * @throws Exception
7338 */
74- protected static function validateLineString (array $ value ): bool
39+ protected function validateLineString (array $ value ): bool
7540 {
7641 if (count ($ value ) < 2 ) {
77- throw new Exception ('LineString must contain at least one point ' );
42+ $ this ->message = 'LineString must contain at least two points ' ;
43+ return false ;
7844 }
7945
8046 foreach ($ value as $ point ) {
8147 if (!is_array ($ point ) || count ($ point ) !== 2 ) {
82- throw new Exception ('Each point in LineString must be an array of two values [x, y] ' );
48+ $ this ->message = 'Each point in LineString must be an array of two values [x, y] ' ;
49+ return false ;
8350 }
8451
8552 if (!is_numeric ($ point [0 ]) || !is_numeric ($ point [1 ])) {
86- throw new Exception ('Each point in LineString must have numeric coordinates ' );
53+ $ this ->message = 'Each point in LineString must have numeric coordinates ' ;
54+ return false ;
8755 }
8856 }
8957
@@ -92,36 +60,39 @@ protected static function validateLineString(array $value): bool
9260
9361 /**
9462 * Validate POLYGON data
95- *
96- * @param array<mixed,mixed> $value
97- * @return bool
98- * @throws Exception
9963 */
100- protected static function validatePolygon (array $ value ): bool
64+ protected function validatePolygon (array $ value ): bool
10165 {
10266 if (empty ($ value )) {
103- throw new Exception ('Polygon must contain at least one ring ' );
67+ $ this ->message = 'Polygon must contain at least one ring ' ;
68+ return false ;
10469 }
10570
10671 // Detect single-ring polygon: [[x, y], [x, y], ...]
10772 $ isSingleRing = isset ($ value [0 ]) && is_array ($ value [0 ]) &&
108- count ($ value [0 ]) === 2 && is_numeric ($ value [0 ][0 ]) && is_numeric ($ value [0 ][1 ]);
73+ count ($ value [0 ]) === 2 &&
74+ is_numeric ($ value [0 ][0 ]) &&
75+ is_numeric ($ value [0 ][1 ]);
10976
11077 if ($ isSingleRing ) {
111- $ value = [$ value ]; // Wrap single ring into multi-ring format
78+ $ value = [$ value ]; // wrap single ring
11279 }
11380
11481 foreach ($ value as $ ring ) {
11582 if (!is_array ($ ring ) || empty ($ ring )) {
116- throw new Exception ('Each ring in Polygon must be an array of points ' );
83+ $ this ->message = 'Each ring in Polygon must be an array of points ' ;
84+ return false ;
11785 }
11886
11987 foreach ($ ring as $ point ) {
12088 if (!is_array ($ point ) || count ($ point ) !== 2 ) {
121- throw new Exception ('Each point in Polygon ring must be an array of two values [x, y] ' );
89+ $ this ->message = 'Each point in Polygon ring must be an array of two values [x, y] ' ;
90+ return false ;
12291 }
92+
12393 if (!is_numeric ($ point [0 ]) || !is_numeric ($ point [1 ])) {
124- throw new Exception ('Each point in Polygon ring must have numeric coordinates ' );
94+ $ this ->message = 'Each point in Polygon ring must have numeric coordinates ' ;
95+ return false ;
12596 }
12697 }
12798 }
@@ -131,51 +102,30 @@ protected static function validatePolygon(array $value): bool
131102
132103 /**
133104 * Check if a value is valid WKT string
134- *
135- * @param string $value
136- * @return bool
137105 */
138106 public static function isWKTString (string $ value ): bool
139107 {
140108 $ value = trim ($ value );
141109 return (bool ) preg_match ('/^(POINT|LINESTRING|POLYGON)\s*\(/i ' , $ value );
142110 }
143111
144- /**
145- * Get validator description
146- *
147- * @return string
148- */
149112 public function getDescription (): string
150113 {
151- return 'Value must be a valid ' . $ this ->spatialType . ' format (array or WKT string) ' ;
114+ return 'Value must be a valid ' . $ this ->spatialType . " : { $ this -> message }" ;
152115 }
153116
154- /**
155- * Is array
156- *
157- * @return bool
158- */
159117 public function isArray (): bool
160118 {
161119 return false ;
162120 }
163121
164- /**
165- * Get Type
166- *
167- * @return string
168- */
169122 public function getType (): string
170123 {
171124 return 'spatial ' ;
172125 }
173126
174127 /**
175- * Is valid
176- *
177- * @param mixed $value
178- * @return bool
128+ * Main validation entrypoint
179129 */
180130 public function isValid ($ value ): bool
181131 {
@@ -184,20 +134,27 @@ public function isValid($value): bool
184134 }
185135
186136 if (is_string ($ value )) {
187- // Check if it's a valid WKT string
188137 return self ::isWKTString ($ value );
189138 }
190139
191140 if (is_array ($ value )) {
192- // Validate the array format according to the specific spatial type
193- try {
194- self ::validate ($ value , $ this ->spatialType );
195- return true ;
196- } catch (\Exception $ e ) {
197- return false ;
141+ switch ($ this ->spatialType ) {
142+ case Database::VAR_POINT :
143+ return $ this ->validatePoint ($ value );
144+
145+ case Database::VAR_LINESTRING :
146+ return $ this ->validateLineString ($ value );
147+
148+ case Database::VAR_POLYGON :
149+ return $ this ->validatePolygon ($ value );
150+
151+ default :
152+ $ this ->message = 'Unknown spatial type: ' . $ this ->spatialType ;
153+ return false ;
198154 }
199155 }
200156
157+ $ this ->message = 'Spatial value must be array or WKT string ' ;
201158 return false ;
202159 }
203160}
0 commit comments