Skip to content
This repository was archived by the owner on Feb 28, 2025. It is now read-only.

Commit 118184d

Browse files
authored
PHPLIB-1337 Add tests on Geospatial Query Operators (#25)
* PHPLIB-1337 Add tests on Geospatial Query Operators * Fix nested $geometry
1 parent f665eb3 commit 118184d

15 files changed

+742
-39
lines changed

generator/config/query/geoIntersects.yaml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,42 @@ arguments:
1111
name: geometry
1212
type:
1313
- geometry
14+
tests:
15+
-
16+
name: 'Intersects a Polygon'
17+
link: 'https://www.mongodb.com/docs/manual/reference/operator/query/geoIntersects/#intersects-a-polygon'
18+
pipeline:
19+
-
20+
$match:
21+
loc:
22+
$geoIntersects:
23+
$geometry:
24+
type: 'Polygon'
25+
coordinates:
26+
-
27+
- [ 0, 0 ]
28+
- [ 3, 6 ]
29+
- [ 6, 1 ]
30+
- [ 0, 0 ]
31+
-
32+
name: 'Intersects a Big Polygon'
33+
link: 'https://www.mongodb.com/docs/manual/reference/operator/query/geoIntersects/#intersects-a--big--polygon'
34+
pipeline:
35+
-
36+
$match:
37+
loc:
38+
$geoIntersects:
39+
$geometry:
40+
type: 'Polygon'
41+
coordinates:
42+
-
43+
- [ -100, 60 ]
44+
- [ -100, 0 ]
45+
- [ -100, -60 ]
46+
- [ 100, -60 ]
47+
- [ 100, 60 ]
48+
- [ -100, 60 ]
49+
crs:
50+
type: 'name'
51+
properties:
52+
name: 'urn:x-mongodb:crs:strictwinding:EPSG:4326'

generator/config/query/geoWithin.yaml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,42 @@ arguments:
1111
name: geometry
1212
type:
1313
- geometry
14+
tests:
15+
-
16+
name: 'Within a Polygon'
17+
link: 'https://www.mongodb.com/docs/manual/reference/operator/query/geoWithin/#within-a-polygon'
18+
pipeline:
19+
-
20+
$match:
21+
loc:
22+
$geoWithin:
23+
$geometry:
24+
type: 'Polygon'
25+
coordinates:
26+
-
27+
- [ 0, 0 ]
28+
- [ 3, 6 ]
29+
- [ 6, 1 ]
30+
- [ 0, 0 ]
31+
-
32+
name: 'Within a Big Polygon'
33+
link: 'https://www.mongodb.com/docs/manual/reference/operator/query/geoWithin/#within-a--big--polygon'
34+
pipeline:
35+
-
36+
$match:
37+
loc:
38+
$geoWithin:
39+
$geometry:
40+
type: 'Polygon'
41+
coordinates:
42+
-
43+
- [ -100, 60 ]
44+
- [ -100, 0 ]
45+
- [ -100, -60 ]
46+
- [ 100, -60 ]
47+
- [ 100, 60 ]
48+
- [ -100, 60 ]
49+
crs:
50+
type: 'name'
51+
properties:
52+
name: 'urn:x-mongodb:crs:strictwinding:EPSG:4326'

generator/config/query/geometry.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@ arguments:
1919
name: crs
2020
type:
2121
- object
22+
optional: true

generator/config/query/near.yaml

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name: $near
33
link: 'https://www.mongodb.com/docs/manual/reference/operator/query/near/'
44
type:
55
- fieldQuery
6-
encode: object
6+
encode: dollar_object
77
description: |
88
Returns geospatial objects in proximity to a point. Requires a geospatial index. The 2dsphere and 2d indexes support $near.
99
arguments:
@@ -14,14 +14,30 @@ arguments:
1414
-
1515
name: maxDistance
1616
type:
17-
- int
17+
- number
1818
optional: true
1919
description: |
2020
Distance in meters. Limits the results to those documents that are at most the specified distance from the center point.
2121
-
2222
name: minDistance
2323
type:
24-
- int
24+
- number
2525
optional: true
2626
description: |
2727
Distance in meters. Limits the results to those documents that are at least the specified distance from the center point.
28+
tests:
29+
-
30+
name: 'Query on GeoJSON Data'
31+
link: 'https://www.mongodb.com/docs/manual/reference/operator/query/near/#query-on-geojson-data'
32+
pipeline:
33+
-
34+
$match:
35+
location:
36+
$near:
37+
$geometry:
38+
type: 'Point'
39+
coordinates:
40+
- -73.9667
41+
- 40.78
42+
$minDistance: 1000
43+
$maxDistance: 5000

generator/config/query/nearSphere.yaml

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name: $nearSphere
33
link: 'https://www.mongodb.com/docs/manual/reference/operator/query/nearSphere/'
44
type:
55
- fieldQuery
6-
encode: object
6+
encode: dollar_object
77
description: |
88
Returns geospatial objects in proximity to a point on a sphere. Requires a geospatial index. The 2dsphere and 2d indexes support $nearSphere.
99
arguments:
@@ -14,14 +14,28 @@ arguments:
1414
-
1515
name: maxDistance
1616
type:
17-
- int
17+
- number
1818
optional: true
1919
description: |
2020
Distance in meters.
2121
-
2222
name: minDistance
2323
type:
24-
- int
24+
- number
2525
optional: true
2626
description: |
2727
Distance in meters. Limits the results to those documents that are at least the specified distance from the center point.
28+
tests:
29+
-
30+
name: 'Specify Center Point Using GeoJSON'
31+
link: 'https://www.mongodb.com/docs/manual/reference/operator/query/nearSphere/#specify-center-point-using-geojson'
32+
pipeline:
33+
-
34+
$match:
35+
location:
36+
$nearSphere:
37+
$geometry:
38+
type: 'Point'
39+
coordinates: [-73.9667, 40.78]
40+
$minDistance: 1000
41+
$maxDistance: 5000

src/Builder/BuilderEncoder.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
use MongoDB\Exception\UnsupportedValueException;
2727
use stdClass;
2828

29+
use function array_key_exists;
2930
use function array_key_first;
3031
use function assert;
3132
use function get_debug_type;
@@ -182,7 +183,19 @@ private function encodeAsDollarObject(OperatorInterface $value): stdClass
182183
continue;
183184
}
184185

185-
$result->{'$' . $key} = $this->recursiveEncode($val);
186+
$val = $this->recursiveEncode($val);
187+
188+
if ($key === 'geometry') {
189+
if (is_object($val) && property_exists($val, '$geometry')) {
190+
$result->{'$geometry'} = $val->{'$geometry'};
191+
} elseif (is_array($val) && array_key_exists('$geometry', $val)) {
192+
$result->{'$geometry'} = $val->{'$geometry'};
193+
} else {
194+
$result->{'$geometry'} = $val;
195+
}
196+
} else {
197+
$result->{'$' . $key} = $val;
198+
}
186199
}
187200

188201
return $this->wrap($value, $result);

src/Builder/Query/FactoryTrait.php

Lines changed: 10 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Builder/Query/GeometryOperator.php

Lines changed: 5 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Builder/Query/NearOperator.php

Lines changed: 11 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Builder/Query/NearSphereOperator.php

Lines changed: 11 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)