Skip to content

Commit 0f72b9b

Browse files
updated spatial index validation step
1 parent 88998ed commit 0f72b9b

File tree

1 file changed

+25
-11
lines changed

1 file changed

+25
-11
lines changed

src/Database/Validator/Index.php

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -338,26 +338,40 @@ public function getType(): string
338338
*/
339339
public function checkSpatialIndex(Document $index): bool
340340
{
341+
$type = $index->getAttribute('type');
342+
if ($type !== Database::INDEX_SPATIAL) {
343+
return true;
344+
}
345+
346+
if (!$this->spatialIndexSupport) {
347+
$this->message = 'Spatial indexes are not supported';
348+
return false;
349+
}
350+
341351
$attributes = $index->getAttribute('attributes', []);
342-
$orders = $index->getAttribute('orders', []);
352+
$orders = $index->getAttribute('orders', []);
353+
343354
foreach ($attributes as $attributeName) {
344-
$attribute = $this->attributes[\strtolower($attributeName)] ?? new Document();
345-
$attributeType = $attribute->getAttribute('type');
346-
$required = $attribute->getAttribute('required');
347-
if (in_array($attributeType, Database::SPATIAL_TYPES) && !$this->spatialIndexSupport) {
348-
$this->message = "Spatial index is not supported";
355+
$attribute = $this->attributes[\strtolower($attributeName)] ?? new Document();
356+
$attributeType = $attribute->getAttribute('type', '');
357+
358+
if (!\in_array($attributeType, Database::SPATIAL_TYPES, true)) {
359+
$this->message = 'Spatial index can only be created on spatial attributes (point, linestring, polygon). Attribute "' . $attributeName . '" is of type "' . $attributeType . '"';
349360
return false;
350361
}
351-
if (in_array($attributeType, Database::SPATIAL_TYPES) && !$required && !$this->spatialIndexNullSupport) {
362+
363+
$required = (bool) $attribute->getAttribute('required', false);
364+
if (!$required && !$this->spatialIndexNullSupport) {
352365
$this->message = 'Spatial indexes do not allow null values. Mark the attribute "' . $attributeName . '" as required or create the index on a column with no null values.';
353366
return false;
354367
}
355-
if (in_array($attributeType, Database::SPATIAL_TYPES) && !empty($orders) && !$this->spatialIndexOrderSupport) {
356-
$this->message = 'Spatial indexes with explicit orders are not supported. Remove the orders to create this index.';
357-
return false;
358-
}
368+
}
359369

370+
if (!empty($orders) && !$this->spatialIndexOrderSupport) {
371+
$this->message = 'Spatial indexes with explicit orders are not supported. Remove the orders to create this index.';
372+
return false;
360373
}
374+
361375
return true;
362376
}
363377
}

0 commit comments

Comments
 (0)