Skip to content

Commit 57358ac

Browse files
authored
Merge pull request #689 from utopia-php/sptial-query-fix
reduced the spatial query dimension to avoid nested array
2 parents b5ea4d1 + bf1aad0 commit 57358ac

File tree

2 files changed

+24
-24
lines changed

2 files changed

+24
-24
lines changed

src/Database/Query.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -986,7 +986,7 @@ public static function distanceLessThan(string $attribute, array $values, int|fl
986986
*/
987987
public static function intersects(string $attribute, array $values): self
988988
{
989-
return new self(self::TYPE_INTERSECTS, $attribute, $values);
989+
return new self(self::TYPE_INTERSECTS, $attribute, [$values]);
990990
}
991991

992992
/**
@@ -998,7 +998,7 @@ public static function intersects(string $attribute, array $values): self
998998
*/
999999
public static function notIntersects(string $attribute, array $values): self
10001000
{
1001-
return new self(self::TYPE_NOT_INTERSECTS, $attribute, $values);
1001+
return new self(self::TYPE_NOT_INTERSECTS, $attribute, [$values]);
10021002
}
10031003

10041004
/**
@@ -1010,7 +1010,7 @@ public static function notIntersects(string $attribute, array $values): self
10101010
*/
10111011
public static function crosses(string $attribute, array $values): self
10121012
{
1013-
return new self(self::TYPE_CROSSES, $attribute, $values);
1013+
return new self(self::TYPE_CROSSES, $attribute, [$values]);
10141014
}
10151015

10161016
/**
@@ -1022,7 +1022,7 @@ public static function crosses(string $attribute, array $values): self
10221022
*/
10231023
public static function notCrosses(string $attribute, array $values): self
10241024
{
1025-
return new self(self::TYPE_NOT_CROSSES, $attribute, $values);
1025+
return new self(self::TYPE_NOT_CROSSES, $attribute, [$values]);
10261026
}
10271027

10281028
/**
@@ -1034,7 +1034,7 @@ public static function notCrosses(string $attribute, array $values): self
10341034
*/
10351035
public static function overlaps(string $attribute, array $values): self
10361036
{
1037-
return new self(self::TYPE_OVERLAPS, $attribute, $values);
1037+
return new self(self::TYPE_OVERLAPS, $attribute, [$values]);
10381038
}
10391039

10401040
/**
@@ -1046,7 +1046,7 @@ public static function overlaps(string $attribute, array $values): self
10461046
*/
10471047
public static function notOverlaps(string $attribute, array $values): self
10481048
{
1049-
return new self(self::TYPE_NOT_OVERLAPS, $attribute, $values);
1049+
return new self(self::TYPE_NOT_OVERLAPS, $attribute, [$values]);
10501050
}
10511051

10521052
/**
@@ -1058,7 +1058,7 @@ public static function notOverlaps(string $attribute, array $values): self
10581058
*/
10591059
public static function touches(string $attribute, array $values): self
10601060
{
1061-
return new self(self::TYPE_TOUCHES, $attribute, $values);
1061+
return new self(self::TYPE_TOUCHES, $attribute, [$values]);
10621062
}
10631063

10641064
/**
@@ -1070,6 +1070,6 @@ public static function touches(string $attribute, array $values): self
10701070
*/
10711071
public static function notTouches(string $attribute, array $values): self
10721072
{
1073-
return new self(self::TYPE_NOT_TOUCHES, $attribute, $values);
1073+
return new self(self::TYPE_NOT_TOUCHES, $attribute, [$values]);
10741074
}
10751075
}

tests/e2e/Adapter/Scopes/SpatialTests.php

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,8 @@ public function testSpatialTypeDocuments(): void
135135
'notEquals' => Query::notEqual('pointAttr', [[1.0, 1.0]]),
136136
'distanceEqual' => Query::distanceEqual('pointAttr', [5.0, 5.0], 1.4142135623730951),
137137
'distanceNotEqual' => Query::distanceNotEqual('pointAttr', [1.0, 1.0], 0.0),
138-
'intersects' => Query::intersects('pointAttr', [[6.0, 6.0]]),
139-
'notIntersects' => Query::notIntersects('pointAttr', [[1.0, 1.0]])
138+
'intersects' => Query::intersects('pointAttr', [6.0, 6.0]),
139+
'notIntersects' => Query::notIntersects('pointAttr', [1.0, 1.0])
140140
];
141141

142142
foreach ($pointQueries as $queryType => $query) {
@@ -151,8 +151,8 @@ public function testSpatialTypeDocuments(): void
151151
'notContains' => Query::notContains('lineAttr', [[5.0, 6.0]]), // Point not on the line
152152
'equals' => query::equal('lineAttr', [[[1.0, 2.0], [3.0, 4.0]]]), // Exact same linestring
153153
'notEquals' => query::notEqual('lineAttr', [[[5.0, 6.0], [7.0, 8.0]]]), // Different linestring
154-
'intersects' => Query::intersects('lineAttr', [[1.0, 2.0]]), // Point on the line should intersect
155-
'notIntersects' => Query::notIntersects('lineAttr', [[5.0, 6.0]]) // Point not on the line should not intersect
154+
'intersects' => Query::intersects('lineAttr', [1.0, 2.0]), // Point on the line should intersect
155+
'notIntersects' => Query::notIntersects('lineAttr', [5.0, 6.0]) // Point not on the line should not intersect
156156
];
157157

158158
foreach ($lineQueries as $queryType => $query) {
@@ -182,12 +182,12 @@ public function testSpatialTypeDocuments(): void
182182
$polyQueries = [
183183
'contains' => Query::contains('polyAttr', [[5.0, 5.0]]), // Point inside polygon
184184
'notContains' => Query::notContains('polyAttr', [[15.0, 15.0]]), // Point outside polygon
185-
'intersects' => Query::intersects('polyAttr', [[5.0, 5.0]]), // Point inside polygon should intersect
186-
'notIntersects' => Query::notIntersects('polyAttr', [[15.0, 15.0]]), // Point outside polygon should not intersect
185+
'intersects' => Query::intersects('polyAttr', [5.0, 5.0]), // Point inside polygon should intersect
186+
'notIntersects' => Query::notIntersects('polyAttr', [15.0, 15.0]), // Point outside polygon should not intersect
187187
'equals' => query::equal('polyAttr', [[[[0.0, 0.0], [0.0, 10.0], [10.0, 10.0], [0.0, 0.0]]]]), // Exact same polygon
188188
'notEquals' => query::notEqual('polyAttr', [[[[20.0, 20.0], [20.0, 30.0], [30.0, 30.0], [20.0, 20.0]]]]), // Different polygon
189-
'overlaps' => Query::overlaps('polyAttr', [[[[5.0, 5.0], [5.0, 15.0], [15.0, 15.0], [15.0, 5.0], [5.0, 5.0]]]]), // Overlapping polygon
190-
'notOverlaps' => Query::notOverlaps('polyAttr', [[[[20.0, 20.0], [20.0, 30.0], [30.0, 30.0], [30.0, 20.0], [20.0, 20.0]]]]) // Non-overlapping polygon
189+
'overlaps' => Query::overlaps('polyAttr', [[[5.0, 5.0], [5.0, 15.0], [15.0, 15.0], [15.0, 5.0], [5.0, 5.0]]]), // Overlapping polygon
190+
'notOverlaps' => Query::notOverlaps('polyAttr', [[[20.0, 20.0], [20.0, 30.0], [30.0, 30.0], [30.0, 20.0], [20.0, 20.0]]]) // Non-overlapping polygon
191191
];
192192

193193
foreach ($polyQueries as $queryType => $query) {
@@ -935,8 +935,8 @@ public function testComplexGeometricShapes(): void
935935
// Test rectangle intersects with another rectangle
936936
$overlappingRect = $database->find($collectionName, [
937937
Query::and([
938-
Query::intersects('rectangle', [[[15, 5], [15, 15], [25, 15], [25, 5], [15, 5]]]),
939-
Query::notTouches('rectangle', [[[15, 5], [15, 15], [25, 15], [25, 5], [15, 5]]])
938+
Query::intersects('rectangle', [[15, 5], [15, 15], [25, 15], [25, 5], [15, 5]]),
939+
Query::notTouches('rectangle', [[15, 5], [15, 15], [25, 15], [25, 5], [15, 5]])
940940
]),
941941
], Database::PERMISSION_READ);
942942
$this->assertNotEmpty($overlappingRect);
@@ -1042,7 +1042,7 @@ public function testComplexGeometricShapes(): void
10421042
], Database::PERMISSION_READ);
10431043
} else {
10441044
$exactSquare = $database->find($collectionName, [
1045-
Query::intersects('square', [[[5, 5], [5, 15], [15, 15], [15, 5], [5, 5]]])
1045+
Query::intersects('square', [[5, 5], [5, 15], [15, 15], [15, 5], [5, 5]])
10461046
], Database::PERMISSION_READ);
10471047
}
10481048
$this->assertNotEmpty($exactSquare);
@@ -1089,13 +1089,13 @@ public function testComplexGeometricShapes(): void
10891089

10901090
// Test triangle intersects with point
10911091
$intersectingTriangle = $database->find($collectionName, [
1092-
Query::intersects('triangle', [[25, 10]]) // Point inside triangle should intersect
1092+
Query::intersects('triangle', [25, 10]) // Point inside triangle should intersect
10931093
], Database::PERMISSION_READ);
10941094
$this->assertNotEmpty($intersectingTriangle);
10951095

10961096
// Test triangle doesn't intersect with distant point
10971097
$nonIntersectingTriangle = $database->find($collectionName, [
1098-
Query::notIntersects('triangle', [[100, 100]]) // Distant point should not intersect
1098+
Query::notIntersects('triangle', [100, 100]) // Distant point should not intersect
10991099
], Database::PERMISSION_READ);
11001100
$this->assertNotEmpty($nonIntersectingTriangle);
11011101

@@ -1159,7 +1159,7 @@ public function testComplexGeometricShapes(): void
11591159

11601160
// Test complex polygon intersects with line
11611161
$intersectingLine = $database->find($collectionName, [
1162-
Query::intersects('complex_polygon', [[[0, 10], [20, 10]]]) // Horizontal line through L-shape
1162+
Query::intersects('complex_polygon', [[0, 10], [20, 10]]) // Horizontal line through L-shape
11631163
], Database::PERMISSION_READ);
11641164
$this->assertNotEmpty($intersectingLine);
11651165

@@ -1181,13 +1181,13 @@ public function testComplexGeometricShapes(): void
11811181

11821182
// Test linestring intersects with point
11831183
$intersectingPoint = $database->find($collectionName, [
1184-
Query::intersects('multi_linestring', [[10, 10]]) // Point on diagonal line
1184+
Query::intersects('multi_linestring', [10, 10]) // Point on diagonal line
11851185
], Database::PERMISSION_READ);
11861186
$this->assertNotEmpty($intersectingPoint);
11871187

11881188
// Test linestring intersects with a horizontal line coincident at y=20
11891189
$touchingLine = $database->find($collectionName, [
1190-
Query::intersects('multi_linestring', [[[0, 20], [20, 20]]])
1190+
Query::intersects('multi_linestring', [[0, 20], [20, 20]])
11911191
], Database::PERMISSION_READ);
11921192
$this->assertNotEmpty($touchingLine);
11931193

0 commit comments

Comments
 (0)