Skip to content

Commit 95f4802

Browse files
committedAug 8, 2022
fix for #837
1 parent be39f33 commit 95f4802

14 files changed

+196
-115
lines changed
 

Diff for: ‎README.md

+2
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ These are all the configuration options and their default value between brackets
6060
- "command": Extra SQL to initialize the database connection (none)
6161
- "tables": Comma separated list of tables to publish (defaults to 'all')
6262
- "mapping": Comma separated list of table/column mappings (no mappping)
63+
- "geometrySrid": SRID assumed when converting from WKT to geometry (`4326`)
6364
- "middlewares": List of middlewares to load (`cors`)
6465
- "controllers": List of controllers to load (`records,geojson,openapi,status`)
6566
- "customControllers": List of user custom controllers to load (no default)
@@ -605,6 +606,7 @@ For spatial support there is an extra set of filters that can be applied on geom
605606
- "siv": spatial is valid (geometry is valid)
606607

607608
These filters are based on OGC standards and so is the WKT specification in which the geometry columns are represented.
609+
Note that the SRID that is assumed when converting from WKT to geometry is specified by the config variable `geometrySrid` and defaults to 4326 (WGS 84).
608610

609611
#### GeoJSON
610612

Diff for: ‎api.include.php

+53-27
Original file line numberDiff line numberDiff line change
@@ -5037,10 +5037,12 @@ public function ping(ServerRequestInterface $request): ResponseInterface
50375037
class ColumnConverter
50385038
{
50395039
private $driver;
5040+
private $geometrySrid;
50405041

5041-
public function __construct(string $driver)
5042+
public function __construct(string $driver, int $geometrySrid)
50425043
{
50435044
$this->driver = $driver;
5045+
$this->geometrySrid = $geometrySrid;
50445046
}
50455047

50465048
public function convertColumnValue(ReflectedColumn $column): string
@@ -5066,12 +5068,13 @@ public function convertColumnValue(ReflectedColumn $column): string
50665068
}
50675069
}
50685070
if ($column->isGeometry()) {
5071+
$srid = $this->geometrySrid;
50695072
switch ($this->driver) {
50705073
case 'mysql':
50715074
case 'pgsql':
5072-
return "ST_GeomFromText(?)";
5075+
return "ST_GeomFromText(?,$srid)";
50735076
case 'sqlsrv':
5074-
return "geometry::STGeomFromText(?,0)";
5077+
return "geometry::STGeomFromText(?,$srid)";
50755078
}
50765079
}
50775080
return '?';
@@ -5114,10 +5117,10 @@ class ColumnsBuilder
51145117
private $driver;
51155118
private $converter;
51165119

5117-
public function __construct(string $driver)
5120+
public function __construct(string $driver, int $geometrySrid)
51185121
{
51195122
$this->driver = $driver;
5120-
$this->converter = new ColumnConverter($driver);
5123+
$this->converter = new ColumnConverter($driver, $geometrySrid);
51215124
}
51225125

51235126
public function getOffsetLimit(int $offset, int $limit): string
@@ -5238,10 +5241,12 @@ public function getIncrement(ReflectedTable $table, array $columnValues): string
52385241
class ConditionsBuilder
52395242
{
52405243
private $driver;
5244+
private $geometrySrid;
52415245

5242-
public function __construct(string $driver)
5246+
public function __construct(string $driver, int $geometrySrid)
52435247
{
52445248
$this->driver = $driver;
5249+
$this->geometrySrid = $geometrySrid;
52455250
}
52465251

52475252
private function getConditionSql(Condition $condition, array &$arguments): string
@@ -5402,14 +5407,15 @@ private function hasSpatialArgument(string $operator): bool
54025407

54035408
private function getSpatialFunctionCall(string $functionName, string $column, bool $hasArgument): string
54045409
{
5410+
$srid = $this->geometrySrid;
54055411
switch ($this->driver) {
54065412
case 'mysql':
54075413
case 'pgsql':
5408-
$argument = $hasArgument ? 'ST_GeomFromText(?)' : '';
5414+
$argument = $hasArgument ? "ST_GeomFromText(?,$srid)" : '';
54095415
return "$functionName($column, $argument)=TRUE";
54105416
case 'sqlsrv':
54115417
$functionName = str_replace('ST_', 'ST', $functionName);
5412-
$argument = $hasArgument ? 'geometry::STGeomFromText(?,0)' : '';
5418+
$argument = $hasArgument ? "geometry::STGeomFromText(?,$srid)" : '';
54135419
return "$column.$functionName($argument)=1";
54145420
case 'sqlite':
54155421
$argument = $hasArgument ? '?' : '0';
@@ -5572,6 +5578,7 @@ class GenericDB
55725578
private $conditions;
55735579
private $columns;
55745580
private $converter;
5581+
private $geometrySrid;
55755582

55765583
private function getDsn(): string
55775584
{
@@ -5655,13 +5662,13 @@ private function initPdo(): bool
56555662
$this->mapper = new RealNameMapper($this->mapping);
56565663
$this->reflection = new GenericReflection($this->pdo, $this->driver, $this->database, $this->tables, $this->mapper);
56575664
$this->definition = new GenericDefinition($this->pdo, $this->driver, $this->database, $this->tables, $this->mapper);
5658-
$this->conditions = new ConditionsBuilder($this->driver);
5659-
$this->columns = new ColumnsBuilder($this->driver);
5665+
$this->conditions = new ConditionsBuilder($this->driver, $this->geometrySrid);
5666+
$this->columns = new ColumnsBuilder($this->driver, $this->geometrySrid);
56605667
$this->converter = new DataConverter($this->driver);
56615668
return $result;
56625669
}
56635670

5664-
public function __construct(string $driver, string $address, int $port, string $database, string $command, array $tables, array $mapping, string $username, string $password)
5671+
public function __construct(string $driver, string $address, int $port, string $database, string $command, array $tables, array $mapping, string $username, string $password, int $geometrySrid)
56655672
{
56665673
$this->driver = $driver;
56675674
$this->address = $address;
@@ -5672,10 +5679,11 @@ public function __construct(string $driver, string $address, int $port, string $
56725679
$this->mapping = $mapping;
56735680
$this->username = $username;
56745681
$this->password = $password;
5682+
$this->geometrySrid = $geometrySrid;
56755683
$this->initPdo();
56765684
}
56775685

5678-
public function reconstruct(string $driver, string $address, int $port, string $database, string $command, array $tables, array $mapping, string $username, string $password): bool
5686+
public function reconstruct(string $driver, string $address, int $port, string $database, string $command, array $tables, array $mapping, string $username, string $password, int $geometrySrid): bool
56795687
{
56805688
if ($driver) {
56815689
$this->driver = $driver;
@@ -5704,6 +5712,9 @@ public function reconstruct(string $driver, string $address, int $port, string $
57045712
if ($password) {
57055713
$this->password = $password;
57065714
}
5715+
if ($geometrySrid) {
5716+
$this->geometrySrid = $geometrySrid;
5717+
}
57075718
return $this->initPdo();
57085719
}
57095720

@@ -8817,13 +8828,13 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
88178828

88188829
class ReconnectMiddleware extends Middleware
88198830
{
8820-
private $reflection;
8831+
private $config;
88218832
private $db;
88228833

88238834
public function __construct(Router $router, Responder $responder, Config $config, string $middleware, ReflectionService $reflection, GenericDB $db)
88248835
{
88258836
parent::__construct($router, $responder, $config, $middleware);
8826-
$this->reflection = $reflection;
8837+
$this->config = $config;
88278838
$this->db = $db;
88288839
}
88298840

@@ -8833,7 +8844,7 @@ private function getDriver(): string
88338844
if ($driverHandler) {
88348845
return call_user_func($driverHandler);
88358846
}
8836-
return '';
8847+
return $this->config->getDriver();
88378848
}
88388849

88398850
private function getAddress(): string
@@ -8842,7 +8853,7 @@ private function getAddress(): string
88428853
if ($addressHandler) {
88438854
return call_user_func($addressHandler);
88448855
}
8845-
return '';
8856+
return $this->config->getAddress();
88468857
}
88478858

88488859
private function getPort(): int
@@ -8851,7 +8862,7 @@ private function getPort(): int
88518862
if ($portHandler) {
88528863
return call_user_func($portHandler);
88538864
}
8854-
return 0;
8865+
return $this->config->getPort();
88558866
}
88568867

88578868
private function getDatabase(): string
@@ -8860,7 +8871,7 @@ private function getDatabase(): string
88608871
if ($databaseHandler) {
88618872
return call_user_func($databaseHandler);
88628873
}
8863-
return '';
8874+
return $this->config->getDatabase();
88648875
}
88658876

88668877
private function getCommand(): string
@@ -8869,7 +8880,7 @@ private function getCommand(): string
88698880
if ($commandHandler) {
88708881
return call_user_func($commandHandler);
88718882
}
8872-
return '';
8883+
return $this->config->getCommand();
88738884
}
88748885

88758886
private function getTables(): array
@@ -8878,7 +8889,7 @@ private function getTables(): array
88788889
if ($tablesHandler) {
88798890
return call_user_func($tablesHandler);
88808891
}
8881-
return [];
8892+
return $this->config->getTables();
88828893
}
88838894

88848895
private function getMapping(): array
@@ -8887,7 +8898,7 @@ private function getMapping(): array
88878898
if ($mappingHandler) {
88888899
return call_user_func($mappingHandler);
88898900
}
8890-
return [];
8901+
return $this->config->getMapping();
88918902
}
88928903

88938904
private function getUsername(): string
@@ -8896,7 +8907,7 @@ private function getUsername(): string
88968907
if ($usernameHandler) {
88978908
return call_user_func($usernameHandler);
88988909
}
8899-
return '';
8910+
return $this->config->getUsername();
89008911
}
89018912

89028913
private function getPassword(): string
@@ -8905,7 +8916,16 @@ private function getPassword(): string
89058916
if ($passwordHandler) {
89068917
return call_user_func($passwordHandler);
89078918
}
8908-
return '';
8919+
return $this->config->getPassword();
8920+
}
8921+
8922+
private function getGeometrySrid(): int
8923+
{
8924+
$geometrySridHandler = $this->getProperty('geometrySridHandler', '');
8925+
if ($geometrySridHandler) {
8926+
return call_user_func($geometrySridHandler);
8927+
}
8928+
return $this->config->getGeometrySrid();
89098929
}
89108930

89118931
public function process(ServerRequestInterface $request, RequestHandlerInterface $next): ResponseInterface
@@ -8919,9 +8939,8 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
89198939
$mapping = $this->getMapping();
89208940
$username = $this->getUsername();
89218941
$password = $this->getPassword();
8922-
if ($driver || $address || $port || $database || $command || $tables || $mapping || $username || $password) {
8923-
$this->db->reconstruct($driver, $address, $port, $database, $command, $tables, $mapping, $username, $password);
8924-
}
8942+
$geometrySrid = $this->getGeometrySrid();
8943+
$this->db->reconstruct($driver, $address, $port, $database, $command, $tables, $mapping, $username, $password, $geometrySrid);
89258944
return $next->handle($request);
89268945
}
89278946
}
@@ -11634,7 +11653,8 @@ public function __construct(Config $config)
1163411653
$config->getTables(),
1163511654
$config->getMapping(),
1163611655
$config->getUsername(),
11637-
$config->getPassword()
11656+
$config->getPassword(),
11657+
$config->getGeometrySrid()
1163811658
);
1163911659
$prefix = sprintf('phpcrudapi-%s-', substr(md5(__FILE__), 0, 8));
1164011660
$cache = CacheFactory::create($config->getCacheType(), $prefix, $config->getCachePath());
@@ -11833,6 +11853,7 @@ class Config
1183311853
'debug' => false,
1183411854
'basePath' => '',
1183511855
'openApiBase' => '{"info":{"title":"PHP-CRUD-API","version":"1.0.0"}}',
11856+
'geometrySrid' => 4326,
1183611857
];
1183711858

1183811859
private function getDefaultDriver(array $values): string
@@ -12022,6 +12043,11 @@ public function getOpenApiBase(): array
1202212043
{
1202312044
return json_decode($this->values['openApiBase'], true);
1202412045
}
12046+
12047+
public function getGeometrySrid(): int
12048+
{
12049+
return $this->values['geometrySrid'];
12050+
}
1202512051
}
1202612052
}
1202712053

0 commit comments

Comments
 (0)
Please sign in to comment.