Skip to content

Commit 665d1bf

Browse files
committed
Some GeoJSON fixes
1 parent 605a7a6 commit 665d1bf

File tree

3 files changed

+17
-3
lines changed

3 files changed

+17
-3
lines changed

src/Tqdev/PhpCrudApi/GeoJson/FeatureCollection.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,27 @@ class FeatureCollection implements \JsonSerializable
55
{
66
private $features;
77

8-
public function __construct(array $features)
8+
private $results;
9+
10+
public function __construct(array $features, int $results)
911
{
1012
$this->features = $features;
13+
$this->results = $results;
1114
}
1215

1316
public function serialize()
1417
{
1518
return [
1619
'type' => 'FeatureCollection',
1720
'features' => $this->features,
21+
'results' => $this->results,
1822
];
1923
}
2024

2125
public function jsonSerialize()
2226
{
23-
return $this->serialize();
27+
return array_filter($this->serialize(), function ($v) {
28+
return $v !== 0;
29+
});
2430
}
2531
}

src/Tqdev/PhpCrudApi/GeoJson/GeoJsonService.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public function _list(string $tableName, array $params): FeatureCollection
6161
foreach ($records->getRecords() as $record) {
6262
$features[] = $this->convertRecordToFeature($record, $geometryColumnName);
6363
}
64-
return new FeatureCollection($features);
64+
return new FeatureCollection($features, $records->getResults());
6565
}
6666

6767
public function read(string $tableName, string $id, array $params): Feature

tests/functional/001_records/083_list_users_as_geojson.log

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,11 @@ Content-Type: application/json
55
Content-Length: 269
66

77
{"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}

0 commit comments

Comments
 (0)