Browse Source

add x-referenced support

Maurits van der Schee 6 years ago
parent
commit
703ec88569
2 changed files with 64 additions and 21 deletions
  1. 37
    16
      api.php
  2. 27
    5
      src/Tqdev/PhpCrudApi/OpenApi/OpenApiBuilder.php

+ 37
- 16
api.php View File

@@ -3817,6 +3817,25 @@ class OpenApiBuilder
3817 3817
         return sprintf('%s://%s%s/%s', $protocol, $host, $port, $path);
3818 3818
     }
3819 3819
 
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
+
3820 3839
     public function build(): OpenApiDefinition
3821 3840
     {
3822 3841
         $this->openapi->set("openapi", "3.0.0");
@@ -3836,8 +3855,10 @@ class OpenApiBuilder
3836 3855
         $this->openapi->set("components|responses|rows_affected|description", "number of rows affected (integer)");
3837 3856
         $this->openapi->set("components|responses|rows_affected|content|application/json|schema|type", "integer");
3838 3857
         $this->openapi->set("components|responses|rows_affected|content|application/json|schema|format", "int64");
3858
+        $tableReferences = $this->getAllTableReferences();
3839 3859
         foreach ($tableNames as $tableName) {
3840
-            $this->setComponentSchema($tableName);
3860
+            $references = isset($tableReferences[$tableName])?$tableReferences[$tableName]:array();
3861
+            $this->setComponentSchema($tableName, $references);
3841 3862
             $this->setComponentResponse($tableName);
3842 3863
             $this->setComponentRequestBody($tableName);
3843 3864
         }
@@ -3927,7 +3948,7 @@ class OpenApiBuilder
3927 3948
         }
3928 3949
     }
3929 3950
 
3930
-    private function setComponentSchema(String $tableName) /*: void*/
3951
+    private function setComponentSchema(String $tableName, array $references) /*: void*/
3931 3952
     {
3932 3953
         $table = $this->reflection->getTable($tableName);
3933 3954
         $type = $table->getType();
@@ -3967,6 +3988,7 @@ class OpenApiBuilder
3967 3988
                 }
3968 3989
                 if ($column->getPk()) {
3969 3990
                     $this->openapi->set("$prefix|properties|$columnName|x-primary-key", true);
3991
+                    $this->openapi->set("$prefix|properties|$columnName|x-referenced", $references);
3970 3992
                 }
3971 3993
                 $fk = $column->getFk();
3972 3994
                 if ($fk) {
@@ -4032,19 +4054,19 @@ class OpenApiBuilder
4032 4054
         $this->openapi->set("components|parameters|filter|schema|items|type", "string");
4033 4055
         $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");
4034 4056
         $this->openapi->set("components|parameters|filter|required", false);
4035
-        
4057
+
4036 4058
         $this->openapi->set("components|parameters|include|name", "include");
4037 4059
         $this->openapi->set("components|parameters|include|in", "query");
4038 4060
         $this->openapi->set("components|parameters|include|schema|type", "string");
4039 4061
         $this->openapi->set("components|parameters|include|description", "Columns you want to include in the output (comma separated). Example: posts.*,categories.name");
4040 4062
         $this->openapi->set("components|parameters|include|required", false);
4041
-        
4063
+
4042 4064
         $this->openapi->set("components|parameters|exclude|name", "exclude");
4043 4065
         $this->openapi->set("components|parameters|exclude|in", "query");
4044 4066
         $this->openapi->set("components|parameters|exclude|schema|type", "string");
4045 4067
         $this->openapi->set("components|parameters|exclude|description", "Columns you want to exclude from the output (comma separated). Example: posts.content");
4046 4068
         $this->openapi->set("components|parameters|exclude|required", false);
4047
-        
4069
+
4048 4070
         $this->openapi->set("components|parameters|order|name", "order");
4049 4071
         $this->openapi->set("components|parameters|order|in", "query");
4050 4072
         $this->openapi->set("components|parameters|order|schema|type", "array");
@@ -5411,19 +5433,18 @@ class Api
5411 5433
         } catch (\Throwable $e) {
5412 5434
             if ($e instanceof \PDOException) {
5413 5435
                 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, '');
5424 5443
                 }
5425 5444
             }
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
+            }
5427 5448
             if ($this->debug) {
5428 5449
                 $response->addHeader('X-Exception-Message', $e->getMessage());
5429 5450
                 $response->addHeader('X-Exception-File', $e->getFile() . ':' . $e->getLine());

+ 27
- 5
src/Tqdev/PhpCrudApi/OpenApi/OpenApiBuilder.php View File

@@ -50,6 +50,25 @@ class OpenApiBuilder
50 50
         return sprintf('%s://%s%s/%s', $protocol, $host, $port, $path);
51 51
     }
52 52
 
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
+
53 72
     public function build(): OpenApiDefinition
54 73
     {
55 74
         $this->openapi->set("openapi", "3.0.0");
@@ -69,8 +88,10 @@ class OpenApiBuilder
69 88
         $this->openapi->set("components|responses|rows_affected|description", "number of rows affected (integer)");
70 89
         $this->openapi->set("components|responses|rows_affected|content|application/json|schema|type", "integer");
71 90
         $this->openapi->set("components|responses|rows_affected|content|application/json|schema|format", "int64");
91
+        $tableReferences = $this->getAllTableReferences();
72 92
         foreach ($tableNames as $tableName) {
73
-            $this->setComponentSchema($tableName);
93
+            $references = isset($tableReferences[$tableName])?$tableReferences[$tableName]:array();
94
+            $this->setComponentSchema($tableName, $references);
74 95
             $this->setComponentResponse($tableName);
75 96
             $this->setComponentRequestBody($tableName);
76 97
         }
@@ -160,7 +181,7 @@ class OpenApiBuilder
160 181
         }
161 182
     }
162 183
 
163
-    private function setComponentSchema(String $tableName) /*: void*/
184
+    private function setComponentSchema(String $tableName, array $references) /*: void*/
164 185
     {
165 186
         $table = $this->reflection->getTable($tableName);
166 187
         $type = $table->getType();
@@ -200,6 +221,7 @@ class OpenApiBuilder
200 221
                 }
201 222
                 if ($column->getPk()) {
202 223
                     $this->openapi->set("$prefix|properties|$columnName|x-primary-key", true);
224
+                    $this->openapi->set("$prefix|properties|$columnName|x-referenced", $references);
203 225
                 }
204 226
                 $fk = $column->getFk();
205 227
                 if ($fk) {
@@ -265,19 +287,19 @@ class OpenApiBuilder
265 287
         $this->openapi->set("components|parameters|filter|schema|items|type", "string");
266 288
         $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");
267 289
         $this->openapi->set("components|parameters|filter|required", false);
268
-        
290
+
269 291
         $this->openapi->set("components|parameters|include|name", "include");
270 292
         $this->openapi->set("components|parameters|include|in", "query");
271 293
         $this->openapi->set("components|parameters|include|schema|type", "string");
272 294
         $this->openapi->set("components|parameters|include|description", "Columns you want to include in the output (comma separated). Example: posts.*,categories.name");
273 295
         $this->openapi->set("components|parameters|include|required", false);
274
-        
296
+
275 297
         $this->openapi->set("components|parameters|exclude|name", "exclude");
276 298
         $this->openapi->set("components|parameters|exclude|in", "query");
277 299
         $this->openapi->set("components|parameters|exclude|schema|type", "string");
278 300
         $this->openapi->set("components|parameters|exclude|description", "Columns you want to exclude from the output (comma separated). Example: posts.content");
279 301
         $this->openapi->set("components|parameters|exclude|required", false);
280
-        
302
+
281 303
         $this->openapi->set("components|parameters|order|name", "order");
282 304
         $this->openapi->set("components|parameters|order|in", "query");
283 305
         $this->openapi->set("components|parameters|order|schema|type", "array");

Loading…
Cancel
Save