From d8df0f0a56a8dc7b451bc29f3cdacb420ef392ec Mon Sep 17 00:00:00 2001 From: MayGrass <15877196+MayGrass@users.noreply.github.com> Date: Fri, 29 Mar 2024 17:39:25 +0800 Subject: [PATCH] Update API.md and add tests for toFeatureCollectionJson --- API.md | 2 +- src/Objects/Geometry.php | 4 ++++ tests/Objects/GeometryCollectionTest.php | 8 ++++++-- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/API.md b/API.md index be90f03..b92313a 100644 --- a/API.md +++ b/API.md @@ -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. diff --git a/src/Objects/Geometry.php b/src/Objects/Geometry.php index 0ef92a2..3291b10 100644 --- a/src/Objects/Geometry.php +++ b/src/Objects/Geometry.php @@ -153,6 +153,10 @@ public function toArray(): array } /** + * @param array> $properties + * + * @return string + * * @throws JsonException */ public function toFeatureCollectionJson(array $properties = []): string diff --git a/tests/Objects/GeometryCollectionTest.php b/tests/Objects/GeometryCollectionTest.php index 2af8ead..c23f13a 100644 --- a/tests/Objects/GeometryCollectionTest.php +++ b/tests/Objects/GeometryCollectionTest.php @@ -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); });