@@ -4479,7 +4479,7 @@ class Feature implements \JsonSerializable
4479
4479
private $ properties ;
4480
4480
private $ geometry ;
4481
4481
4482
- public function __construct (array $ properties , Geometry $ geometry )
4482
+ public function __construct (array $ properties , /*? Geometry*/ $ geometry )
4483
4483
{
4484
4484
$ this ->properties = $ properties ;
4485
4485
$ this ->geometry = $ geometry ;
@@ -4506,35 +4506,28 @@ class FeatureCollection implements \JsonSerializable
4506
4506
{
4507
4507
private $ features ;
4508
4508
4509
- public function __construct (array $ features )
4510
- {
4511
- $ this ->features = $ features ;
4512
- }
4509
+ private $ results ;
4513
4510
4514
- public static function fromListDocument ( ListDocument $ records , string $ geometryColumnName ): FeatureCollection
4511
+ public function __construct ( array $ features , int $ results )
4515
4512
{
4516
- $ features = array ();
4517
- foreach ($ records ->getRecords () as $ record ) {
4518
- if (isset ($ record [$ geometryColumnName ])) {
4519
- $ geometry = Geometry::fromWkt ($ record [$ geometryColumnName ]);
4520
- unset($ record [$ geometryColumnName ]);
4521
- $ features [] = new Feature ($ record , $ geometry );
4522
- }
4523
- }
4524
- return new FeatureCollection ($ features );
4513
+ $ this ->features = $ features ;
4514
+ $ this ->results = $ results ;
4525
4515
}
4526
4516
4527
4517
public function serialize ()
4528
4518
{
4529
4519
return [
4530
4520
'type ' => 'FeatureCollection ' ,
4531
4521
'features ' => $ this ->features ,
4522
+ 'results ' => $ this ->results ,
4532
4523
];
4533
4524
}
4534
4525
4535
4526
public function jsonSerialize ()
4536
4527
{
4537
- return $ this ->serialize ();
4528
+ return array_filter ($ this ->serialize (), function ($ v ) {
4529
+ return $ v !== 0 ;
4530
+ });
4538
4531
}
4539
4532
}
4540
4533
@@ -4556,6 +4549,11 @@ public function hasTable(string $table): bool
4556
4549
return $ this ->reflection ->hasTable ($ table );
4557
4550
}
4558
4551
4552
+ public function getType (string $ table ): string
4553
+ {
4554
+ return $ this ->reflection ->getType ($ table );
4555
+ }
4556
+
4559
4557
private function getGeometryColumnName (string $ tableName , string $ geometryParam ): string
4560
4558
{
4561
4559
$ table = $ this ->reflection ->getTable ($ tableName );
@@ -4571,17 +4569,35 @@ private function getGeometryColumnName(string $tableName, string $geometryParam)
4571
4569
return "" ;
4572
4570
}
4573
4571
4572
+ private function convertRecordToFeature ( /*object*/ $ record , string $ geometryColumnName )
4573
+ {
4574
+ $ geometry = null ;
4575
+ if (isset ($ record [$ geometryColumnName ])) {
4576
+ $ geometry = Geometry::fromWkt ($ record [$ geometryColumnName ]);
4577
+ }
4578
+ $ properties = array_diff_key ($ record , [$ geometryColumnName => true ]);
4579
+ return new Feature ($ properties , $ geometry );
4580
+ }
4581
+
4574
4582
public function _list (string $ tableName , array $ params ): FeatureCollection
4575
4583
{
4576
- $ geometryParam = isset ($ params ['geometry ' ]) ? $ params ['geometry ' ] : '' ;
4584
+ $ geometryParam = isset ($ params ['geometry ' ]) ? $ params ['geometry ' ][ 0 ] : '' ;
4577
4585
$ geometryColumnName = $ this ->getGeometryColumnName ($ tableName , $ geometryParam );
4578
4586
$ records = $ this ->records ->_list ($ tableName , $ params );
4579
- return FeatureCollection::fromListDocument ($ records , $ geometryColumnName );
4587
+
4588
+ $ features = array ();
4589
+ foreach ($ records ->getRecords () as $ record ) {
4590
+ $ features [] = $ this ->convertRecordToFeature ($ record , $ geometryColumnName );
4591
+ }
4592
+ return new FeatureCollection ($ features , $ records ->getResults ());
4580
4593
}
4581
4594
4582
- public function read (string $ tableName , string $ id , array $ params ) /*: ?object*/
4595
+ public function read (string $ tableName , string $ id , array $ params ): Feature
4583
4596
{
4584
- return $ this ->records ->read ($ tableName , $ id , $ params );
4597
+ $ geometryParam = isset ($ params ['geometry ' ]) ? $ params ['geometry ' ][0 ] : '' ;
4598
+ $ geometryColumnName = $ this ->getGeometryColumnName ($ tableName , $ geometryParam );
4599
+ $ record = $ this ->records ->read ($ tableName , $ id , $ params );
4600
+ return $ this ->convertRecordToFeature ($ record , $ geometryColumnName );
4585
4601
}
4586
4602
}
4587
4603
@@ -4599,7 +4615,6 @@ class Geometry implements \JsonSerializable
4599
4615
"MultiLineString " ,
4600
4616
"Polygon " ,
4601
4617
"MultiPolygon " ,
4602
- "GeometryCollection " ,
4603
4618
];
4604
4619
4605
4620
public function __construct (string $ type , array $ coordinates )
@@ -4612,17 +4627,25 @@ public static function fromWkt(string $wkt): Geometry
4612
4627
{
4613
4628
$ bracket = strpos ($ wkt , '( ' );
4614
4629
$ type = strtoupper (trim (substr ($ wkt , 0 , $ bracket )));
4630
+ $ supported = false ;
4615
4631
foreach (Geometry::$ types as $ typeName ) {
4616
4632
if (strtoupper ($ typeName ) == $ type ) {
4617
4633
$ type = $ typeName ;
4634
+ $ supported = true ;
4618
4635
}
4619
4636
}
4637
+ if (!$ supported ) {
4638
+ throw new \Exception ('Geometry type not supported: ' . $ type );
4639
+ }
4620
4640
$ coordinates = substr ($ wkt , $ bracket );
4621
- $ coordinates = preg_replace ('|([0-9\-\.]+ )+([0-9\-\.]+)| ' , '[\1\2] ' , $ coordinates );
4622
- $ coordinates = str_replace (['( ' , ') ' , ' ' ], ['[ ' , '] ' , ', ' ], $ coordinates );
4641
+ if (substr ($ type , -5 ) != 'Point ' || ($ type == 'MultiPoint ' && $ coordinates [1 ] != '( ' )) {
4642
+ $ coordinates = preg_replace ('|([0-9\-\.]+ )+([0-9\-\.]+)| ' , '[\1\2] ' , $ coordinates );
4643
+ }
4644
+ $ coordinates = str_replace (['( ' , ') ' , ', ' , ' ' ], ['[ ' , '] ' , ', ' , ', ' ], $ coordinates );
4645
+ $ json = $ coordinates ;
4623
4646
$ coordinates = json_decode ($ coordinates );
4624
- if ($ type == ' Point ' ) {
4625
- $ coordinates = $ coordinates [ 0 ] ;
4647
+ if (! $ coordinates ) {
4648
+ throw new \ Exception ( ' Could not decode WKT: ' . $ wkt ) ;
4626
4649
}
4627
4650
return new Geometry ($ type , $ coordinates );
4628
4651
}
0 commit comments