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

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

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

Loading…
Cancel
Save