diff --git a/app/Http/Controllers/MarkerController.php b/app/Http/Controllers/MarkerController.php index 064b244b..981170e8 100644 --- a/app/Http/Controllers/MarkerController.php +++ b/app/Http/Controllers/MarkerController.php @@ -3,7 +3,7 @@ namespace App\Http\Controllers; use App\Http\Requests\StoreMarkerRequest; -use App\Http\Resources\MarkerGeoJsonResource; +use App\Http\Resources\MarkerGeoJsonCollection; use App\Models\Map; use App\Models\Marker; use App\Models\MarkerLocation; @@ -51,7 +51,7 @@ public function indexAll(Request $request) // If the requested format is GeoJSON, return the GeoJSON resource collection if ($request->input('format') === 'geojson') { - return MarkerGeoJsonResource::collection($data); + return new MarkerGeoJsonCollection($data); } return $data; @@ -77,7 +77,7 @@ public function index(Request $request, Map $map) // If the requested format is GeoJSON, return the GeoJSON resource collection if ($request->input('format') === 'geojson') { - return MarkerGeoJsonResource::collection($data); + return new MarkerGeoJsonCollection($data); } return $data; diff --git a/app/Http/Resources/MapResource.php b/app/Http/Resources/MapResource.php index bd7089cd..be704c58 100644 --- a/app/Http/Resources/MapResource.php +++ b/app/Http/Resources/MapResource.php @@ -14,10 +14,10 @@ class MapResource extends JsonResource */ public function toArray($request) { - // If $this->whenLoaded('markers'), and the request asks for format=geojson, return MarkerGeoJsonResource, otherwise just return markers. + // If $this->whenLoaded('markers'), and the request asks for format=geojson, return MarkerGeoJsonCollection, otherwise just return markers. $markers = $this->whenLoaded('markers', function () use ($request) { if ($request->input('format') === 'geojson') { - return MarkerGeoJsonResource::collection($this->markers); + return new MarkerGeoJsonCollection($this->markers); } return $this->markers; }); diff --git a/app/Http/Resources/MarkerGeoJsonCollection.php b/app/Http/Resources/MarkerGeoJsonCollection.php new file mode 100644 index 00000000..3f1a2203 --- /dev/null +++ b/app/Http/Resources/MarkerGeoJsonCollection.php @@ -0,0 +1,22 @@ + 'FeatureCollection', + 'features' => $this->collection + ]; + } +} diff --git a/app/Http/Resources/MarkerGeoJsonResource.php b/app/Http/Resources/MarkerGeoJsonResource.php index 73f3b92d..97c7b3c3 100644 --- a/app/Http/Resources/MarkerGeoJsonResource.php +++ b/app/Http/Resources/MarkerGeoJsonResource.php @@ -6,6 +6,13 @@ class MarkerGeoJsonResource extends JsonResource { + /** + * The "data" wrapper that should be applied. + * + * @var string|null + */ + public static $wrap = 'features'; + /** * Transform the resource into an array. * diff --git a/tests/Unit/MarkerTest.php b/tests/Unit/MarkerTest.php index c37f8528..c70ff65a 100644 --- a/tests/Unit/MarkerTest.php +++ b/tests/Unit/MarkerTest.php @@ -89,30 +89,33 @@ public function testSeeAllMapMarkersInGeoJsonFormat() // The type for each $response->assertJsonStructure([ - '*' => [ - 'type', - 'geometry' => [ + 'type', + 'features' => [ + '*' => [ 'type', - 'coordinates', - ], - 'properties' => [ - 'id', - 'description', - 'link', - 'elevation', - 'created_at', - 'zoom', - 'expires_at', - 'meta', - 'elevation', - 'address', - 'locations_count', - 'heading', - 'pitch', - 'roll', - 'speed', - 'category' => [ - 'name', + 'geometry' => [ + 'type', + 'coordinates', + ], + 'properties' => [ + 'id', + 'description', + 'link', + 'elevation', + 'created_at', + 'zoom', + 'expires_at', + 'meta', + 'elevation', + 'address', + 'locations_count', + 'heading', + 'pitch', + 'roll', + 'speed', + 'category' => [ + 'name', + ], ], ], ],