Skip to content

Commit

Permalink
Update API.md and add tests for toFeatureCollectionJson
Browse files Browse the repository at this point in the history
  • Loading branch information
MayGrass committed Mar 29, 2024
1 parent 367c343 commit d8df0f0
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
2 changes: 1 addition & 1 deletion API.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Geometry classes can be also created by these static methods:

* `toArray()` - Serializes the geometry object into a GeoJSON associative array.
* `toJson()` - Serializes the geometry object into an GeoJSON string.
* `toFeatureCollectionJson()` - Serializes the geometry object into an GeoJSON's FeatureCollection string.
* `toFeatureCollectionJson(array $properties = [])` - Serializes the geometry object into an GeoJSON's FeatureCollection string, with optional `properties` to be added to each feature.
* `toWkt()` - Serializes the geometry object into a WKT.
* `toWkb()` - Serializes the geometry object into a WKB.
* `getCoordinates()` - Returns the coordinates of the geometry object.
Expand Down
4 changes: 4 additions & 0 deletions src/Objects/Geometry.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,10 @@ public function toArray(): array
}

/**
* @param array<array<string, mixed>> $properties
*
* @return string
*
* @throws JsonException
*/
public function toFeatureCollectionJson(array $properties = []): string
Expand Down
8 changes: 6 additions & 2 deletions tests/Objects/GeometryCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,14 @@
]),
new Point(0, 180),
]);
$properties = [
['name' => 'Polygon property', 'description' => 'Polygon description'],
['name' => 'Point property', 'description' => 'Point description'],
];

$featureCollectionJson = $geometryCollection->toFeatureCollectionJson();
$featureCollectionJson = $geometryCollection->toFeatureCollectionJson($properties);

$expectedFeatureCollectionJson = '{"type":"FeatureCollection","features":[{"type":"Feature","properties":[],"geometry":{"type":"Polygon","coordinates":[[[180,0],[179,1],[178,2],[177,3],[180,0]]]}},{"type":"Feature","properties":[],"geometry":{"type":"Point","coordinates":[180,0]}}]}';
$expectedFeatureCollectionJson = '{"type":"FeatureCollection","features":[{"type":"Feature","properties":{"name":"Polygon property","description":"Polygon description"},"geometry":{"type":"Polygon","coordinates":[[[180,0],[179,1],[178,2],[177,3],[180,0]]]}},{"type":"Feature","properties":{"name":"Point property","description":"Point description"},"geometry":{"type":"Point","coordinates":[180,0]}}]}';
expect($featureCollectionJson)->toBe($expectedFeatureCollectionJson);
});

Expand Down

0 comments on commit d8df0f0

Please sign in to comment.