Browse Source

Some GeoJSON fixes

Maurits van der Schee 6 years ago
parent
commit
665d1bf979

+ 8
- 2
src/Tqdev/PhpCrudApi/GeoJson/FeatureCollection.php View File

@@ -5,9 +5,12 @@ class FeatureCollection implements \JsonSerializable
5 5
 {
6 6
     private $features;
7 7
 
8
-    public function __construct(array $features)
8
+    private $results;
9
+
10
+    public function __construct(array $features, int $results)
9 11
     {
10 12
         $this->features = $features;
13
+        $this->results = $results;
11 14
     }
12 15
 
13 16
     public function serialize()
@@ -15,11 +18,14 @@ class FeatureCollection implements \JsonSerializable
15 18
         return [
16 19
             'type' => 'FeatureCollection',
17 20
             'features' => $this->features,
21
+            'results' => $this->results,
18 22
         ];
19 23
     }
20 24
 
21 25
     public function jsonSerialize()
22 26
     {
23
-        return $this->serialize();
27
+        return array_filter($this->serialize(), function ($v) {
28
+            return $v !== 0;
29
+        });
24 30
     }
25 31
 }

+ 1
- 1
src/Tqdev/PhpCrudApi/GeoJson/GeoJsonService.php View File

@@ -61,7 +61,7 @@ class GeoJsonService
61 61
         foreach ($records->getRecords() as $record) {
62 62
             $features[] = $this->convertRecordToFeature($record, $geometryColumnName);
63 63
         }
64
-        return new FeatureCollection($features);
64
+        return new FeatureCollection($features, $records->getResults());
65 65
     }
66 66
 
67 67
     public function read(string $tableName, string $id, array $params): Feature

+ 8
- 0
tests/functional/001_records/083_list_users_as_geojson.log View File

@@ -5,3 +5,11 @@ Content-Type: application/json
5 5
 Content-Length: 269
6 6
 
7 7
 {"type":"FeatureCollection","features":[{"type":"Feature","properties":{"id":1,"username":"user1","password":"testtest2"},"geometry":{"type":"Point","coordinates":[30,20]}},{"type":"Feature","properties":{"id":2,"username":"user2","password":"pass2"},"geometry":null}]}
8
+===
9
+GET /geojson/users?page=1,1
10
+===
11
+200
12
+Content-Type: application/json
13
+Content-Length: 186
14
+
15
+{"type":"FeatureCollection","features":[{"type":"Feature","properties":{"id":1,"username":"user1","password":"testtest2"},"geometry":{"type":"Point","coordinates":[30,20]}}],"results":2}

Loading…
Cancel
Save