Skip to content

Commit

Permalink
fix: formatting for GeoJSON
Browse files Browse the repository at this point in the history
  • Loading branch information
mwargan committed Dec 29, 2023
1 parent bef8b1e commit 035f282
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 28 deletions.
6 changes: 3 additions & 3 deletions app/Http/Controllers/MarkerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions app/Http/Resources/MapResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
Expand Down
22 changes: 22 additions & 0 deletions app/Http/Resources/MarkerGeoJsonCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\ResourceCollection;

class MarkerGeoJsonCollection extends ResourceCollection
{
/**
* Transform the resource collection into an array.
*
* @param \Illuminate\Http\Request $request
* @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
*/
public function toArray($request)
{
return [
'type' => 'FeatureCollection',
'features' => $this->collection
];
}
}
7 changes: 7 additions & 0 deletions app/Http/Resources/MarkerGeoJsonResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
49 changes: 26 additions & 23 deletions tests/Unit/MarkerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
],
],
],
],
Expand Down

0 comments on commit 035f282

Please sign in to comment.