Skip to content

Commit 703ec88

Browse files
committed
add x-referenced support
1 parent 138a0fa commit 703ec88

File tree

2 files changed

+64
-21
lines changed

2 files changed

+64
-21
lines changed

api.php

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3817,6 +3817,25 @@ private function getServerUrl(): String
38173817
return sprintf('%s://%s%s/%s', $protocol, $host, $port, $path);
38183818
}
38193819

3820+
private function getAllTableReferences(): array
3821+
{
3822+
$tableReferences = array();
3823+
foreach ($this->reflection->getTableNames() as $tableName) {
3824+
$table = $this->reflection->getTable($tableName);
3825+
foreach ($table->getColumnNames() as $columnName) {
3826+
$column = $table->getColumn($columnName);
3827+
$referencedTableName = $column->getFk();
3828+
if ($referencedTableName) {
3829+
if (!isset($tableReferences[$referencedTableName])) {
3830+
$tableReferences[$referencedTableName] = array();
3831+
}
3832+
$tableReferences[$referencedTableName][] = "$tableName.$columnName";
3833+
}
3834+
}
3835+
}
3836+
return $tableReferences;
3837+
}
3838+
38203839
public function build(): OpenApiDefinition
38213840
{
38223841
$this->openapi->set("openapi", "3.0.0");
@@ -3836,8 +3855,10 @@ public function build(): OpenApiDefinition
38363855
$this->openapi->set("components|responses|rows_affected|description", "number of rows affected (integer)");
38373856
$this->openapi->set("components|responses|rows_affected|content|application/json|schema|type", "integer");
38383857
$this->openapi->set("components|responses|rows_affected|content|application/json|schema|format", "int64");
3858+
$tableReferences = $this->getAllTableReferences();
38393859
foreach ($tableNames as $tableName) {
3840-
$this->setComponentSchema($tableName);
3860+
$references = isset($tableReferences[$tableName])?$tableReferences[$tableName]:array();
3861+
$this->setComponentSchema($tableName, $references);
38413862
$this->setComponentResponse($tableName);
38423863
$this->setComponentRequestBody($tableName);
38433864
}
@@ -3927,7 +3948,7 @@ private function setPath(String $tableName) /*: void*/
39273948
}
39283949
}
39293950

3930-
private function setComponentSchema(String $tableName) /*: void*/
3951+
private function setComponentSchema(String $tableName, array $references) /*: void*/
39313952
{
39323953
$table = $this->reflection->getTable($tableName);
39333954
$type = $table->getType();
@@ -3967,6 +3988,7 @@ private function setComponentSchema(String $tableName) /*: void*/
39673988
}
39683989
if ($column->getPk()) {
39693990
$this->openapi->set("$prefix|properties|$columnName|x-primary-key", true);
3991+
$this->openapi->set("$prefix|properties|$columnName|x-referenced", $references);
39703992
}
39713993
$fk = $column->getFk();
39723994
if ($fk) {
@@ -4032,19 +4054,19 @@ private function setComponentParameters() /*: void*/
40324054
$this->openapi->set("components|parameters|filter|schema|items|type", "string");
40334055
$this->openapi->set("components|parameters|filter|description", "Filters to be applied. Each filter consists of a column, an operator and a value (comma separated). Example: id,eq,1");
40344056
$this->openapi->set("components|parameters|filter|required", false);
4035-
4057+
40364058
$this->openapi->set("components|parameters|include|name", "include");
40374059
$this->openapi->set("components|parameters|include|in", "query");
40384060
$this->openapi->set("components|parameters|include|schema|type", "string");
40394061
$this->openapi->set("components|parameters|include|description", "Columns you want to include in the output (comma separated). Example: posts.*,categories.name");
40404062
$this->openapi->set("components|parameters|include|required", false);
4041-
4063+
40424064
$this->openapi->set("components|parameters|exclude|name", "exclude");
40434065
$this->openapi->set("components|parameters|exclude|in", "query");
40444066
$this->openapi->set("components|parameters|exclude|schema|type", "string");
40454067
$this->openapi->set("components|parameters|exclude|description", "Columns you want to exclude from the output (comma separated). Example: posts.content");
40464068
$this->openapi->set("components|parameters|exclude|required", false);
4047-
4069+
40484070
$this->openapi->set("components|parameters|order|name", "order");
40494071
$this->openapi->set("components|parameters|order|in", "query");
40504072
$this->openapi->set("components|parameters|order|schema|type", "array");
@@ -5411,19 +5433,18 @@ public function handle(Request $request): Response
54115433
} catch (\Throwable $e) {
54125434
if ($e instanceof \PDOException) {
54135435
if (strpos(strtolower($e->getMessage()), 'duplicate') !== false) {
5414-
return $this->responder->error(ErrorCode::DUPLICATE_KEY_EXCEPTION, '');
5415-
}
5416-
if (strpos(strtolower($e->getMessage()), 'default value') !== false) {
5417-
return $this->responder->error(ErrorCode::DATA_INTEGRITY_VIOLATION, '');
5418-
}
5419-
if (strpos(strtolower($e->getMessage()), 'allow nulls') !== false) {
5420-
return $this->responder->error(ErrorCode::DATA_INTEGRITY_VIOLATION, '');
5421-
}
5422-
if (strpos(strtolower($e->getMessage()), 'constraint') !== false) {
5423-
return $this->responder->error(ErrorCode::DATA_INTEGRITY_VIOLATION, '');
5436+
$response = $this->responder->error(ErrorCode::DUPLICATE_KEY_EXCEPTION, '');
5437+
} elseif (strpos(strtolower($e->getMessage()), 'default value') !== false) {
5438+
$response = $this->responder->error(ErrorCode::DATA_INTEGRITY_VIOLATION, '');
5439+
} elseif (strpos(strtolower($e->getMessage()), 'allow nulls') !== false) {
5440+
$response = $this->responder->error(ErrorCode::DATA_INTEGRITY_VIOLATION, '');
5441+
} elseif (strpos(strtolower($e->getMessage()), 'constraint') !== false) {
5442+
$response = $this->responder->error(ErrorCode::DATA_INTEGRITY_VIOLATION, '');
54245443
}
54255444
}
5426-
$response = $this->responder->error(ErrorCode::ERROR_NOT_FOUND, $e->getMessage());
5445+
if (!$response) {
5446+
$response = $this->responder->error(ErrorCode::ERROR_NOT_FOUND, $e->getMessage());
5447+
}
54275448
if ($this->debug) {
54285449
$response->addHeader('X-Exception-Message', $e->getMessage());
54295450
$response->addHeader('X-Exception-File', $e->getFile() . ':' . $e->getLine());

src/Tqdev/PhpCrudApi/OpenApi/OpenApiBuilder.php

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,25 @@ private function getServerUrl(): String
5050
return sprintf('%s://%s%s/%s', $protocol, $host, $port, $path);
5151
}
5252

53+
private function getAllTableReferences(): array
54+
{
55+
$tableReferences = array();
56+
foreach ($this->reflection->getTableNames() as $tableName) {
57+
$table = $this->reflection->getTable($tableName);
58+
foreach ($table->getColumnNames() as $columnName) {
59+
$column = $table->getColumn($columnName);
60+
$referencedTableName = $column->getFk();
61+
if ($referencedTableName) {
62+
if (!isset($tableReferences[$referencedTableName])) {
63+
$tableReferences[$referencedTableName] = array();
64+
}
65+
$tableReferences[$referencedTableName][] = "$tableName.$columnName";
66+
}
67+
}
68+
}
69+
return $tableReferences;
70+
}
71+
5372
public function build(): OpenApiDefinition
5473
{
5574
$this->openapi->set("openapi", "3.0.0");
@@ -69,8 +88,10 @@ public function build(): OpenApiDefinition
6988
$this->openapi->set("components|responses|rows_affected|description", "number of rows affected (integer)");
7089
$this->openapi->set("components|responses|rows_affected|content|application/json|schema|type", "integer");
7190
$this->openapi->set("components|responses|rows_affected|content|application/json|schema|format", "int64");
91+
$tableReferences = $this->getAllTableReferences();
7292
foreach ($tableNames as $tableName) {
73-
$this->setComponentSchema($tableName);
93+
$references = isset($tableReferences[$tableName])?$tableReferences[$tableName]:array();
94+
$this->setComponentSchema($tableName, $references);
7495
$this->setComponentResponse($tableName);
7596
$this->setComponentRequestBody($tableName);
7697
}
@@ -160,7 +181,7 @@ private function setPath(String $tableName) /*: void*/
160181
}
161182
}
162183

163-
private function setComponentSchema(String $tableName) /*: void*/
184+
private function setComponentSchema(String $tableName, array $references) /*: void*/
164185
{
165186
$table = $this->reflection->getTable($tableName);
166187
$type = $table->getType();
@@ -200,6 +221,7 @@ private function setComponentSchema(String $tableName) /*: void*/
200221
}
201222
if ($column->getPk()) {
202223
$this->openapi->set("$prefix|properties|$columnName|x-primary-key", true);
224+
$this->openapi->set("$prefix|properties|$columnName|x-referenced", $references);
203225
}
204226
$fk = $column->getFk();
205227
if ($fk) {
@@ -265,19 +287,19 @@ private function setComponentParameters() /*: void*/
265287
$this->openapi->set("components|parameters|filter|schema|items|type", "string");
266288
$this->openapi->set("components|parameters|filter|description", "Filters to be applied. Each filter consists of a column, an operator and a value (comma separated). Example: id,eq,1");
267289
$this->openapi->set("components|parameters|filter|required", false);
268-
290+
269291
$this->openapi->set("components|parameters|include|name", "include");
270292
$this->openapi->set("components|parameters|include|in", "query");
271293
$this->openapi->set("components|parameters|include|schema|type", "string");
272294
$this->openapi->set("components|parameters|include|description", "Columns you want to include in the output (comma separated). Example: posts.*,categories.name");
273295
$this->openapi->set("components|parameters|include|required", false);
274-
296+
275297
$this->openapi->set("components|parameters|exclude|name", "exclude");
276298
$this->openapi->set("components|parameters|exclude|in", "query");
277299
$this->openapi->set("components|parameters|exclude|schema|type", "string");
278300
$this->openapi->set("components|parameters|exclude|description", "Columns you want to exclude from the output (comma separated). Example: posts.content");
279301
$this->openapi->set("components|parameters|exclude|required", false);
280-
302+
281303
$this->openapi->set("components|parameters|order|name", "order");
282304
$this->openapi->set("components|parameters|order|in", "query");
283305
$this->openapi->set("components|parameters|order|schema|type", "array");

0 commit comments

Comments
 (0)