Skip to content

Commit 1c7335a

Browse files
committed
Merge remote-tracking branch 'upstream/master'
2 parents 824f8ac + a9481db commit 1c7335a

File tree

4 files changed

+329
-10
lines changed

4 files changed

+329
-10
lines changed

api.php

Lines changed: 164 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6550,7 +6550,7 @@ public function __construct(string $driver)
65506550
],
65516551
// source: https://docs.microsoft.com/en-us/sql/connect/jdbc/using-basic-data-types?view=sql-server-2017
65526552
'sqlsrv' => [
6553-
'varbinary(0)' => 'blob',
6553+
'varbinary()' => 'blob',
65546554
'bit' => 'boolean',
65556555
'datetime' => 'timestamp',
65566556
'datetime2' => 'timestamp',
@@ -8406,7 +8406,7 @@ public function __construct(ReflectionService $reflection, $base)
84068406
{
84078407
$this->openapi = new OpenApiDefinition($base);
84088408
$this->records = new OpenApiRecordsBuilder($this->openapi, $reflection);
8409-
$this->columns = new OpenApiColumnsBuilder($this->openapi, $reflection);
8409+
$this->columns = new OpenApiColumnsBuilder($this->openapi);
84108410
}
84118411

84128412
private function getServerUrl(): string
@@ -8426,6 +8426,7 @@ public function build(): OpenApiDefinition
84268426
$this->openapi->set("servers|0|url", $this->getServerUrl());
84278427
}
84288428
$this->records->build();
8429+
//$this->columns->build();
84298430
return $this->openapi;
84308431
}
84318432
}
@@ -8441,16 +8442,174 @@ public function build(): OpenApiDefinition
84418442
class OpenApiColumnsBuilder
84428443
{
84438444
private $openapi;
8444-
private $reflection;
8445+
private $operations = [
8446+
'database' => [
8447+
'read' => 'get',
8448+
],
8449+
'table' => [
8450+
'create' => 'post',
8451+
'read' => 'get',
8452+
'update' => 'put', //rename
8453+
'delete' => 'delete',
8454+
],
8455+
'column' => [
8456+
'create' => 'post',
8457+
'read' => 'get',
8458+
'update' => 'put',
8459+
'delete' => 'delete',
8460+
]
8461+
];
84458462

8446-
public function __construct(OpenApiDefinition $openapi, ReflectionService $reflection)
8463+
public function __construct(OpenApiDefinition $openapi)
84478464
{
84488465
$this->openapi = $openapi;
8449-
$this->reflection = $reflection;
84508466
}
84518467

84528468
public function build() /*: void*/
84538469
{
8470+
$this->setPaths();
8471+
$this->openapi->set("components|responses|boolSuccess|description", "boolean indicating success or failure");
8472+
$this->openapi->set("components|responses|boolSuccess|content|application/json|schema|type", "boolean");
8473+
$this->setComponentSchema();
8474+
$this->setComponentResponse();
8475+
$this->setComponentRequestBody();
8476+
$this->setComponentParameters();
8477+
foreach (array_keys($this->operations) as $index => $type) {
8478+
$this->setTag($index, $type);
8479+
}
8480+
}
8481+
8482+
private function setPaths() /*: void*/
8483+
{
8484+
foreach (array_keys($this->operations) as $type) {
8485+
foreach ($this->operations[$type] as $operation => $method) {
8486+
$parameters = [];
8487+
switch ($type) {
8488+
case 'database':
8489+
$path = '/columns';
8490+
break;
8491+
case 'table':
8492+
$path = $operation == 'create' ? '/columns' : '/columns/{table}';
8493+
break;
8494+
case 'column':
8495+
$path = $operation == 'create' ? '/columns/{table}' : '/columns/{table}/{column}';
8496+
break;
8497+
}
8498+
if (strpos($path, '{table}')) {
8499+
$parameters[] = 'table';
8500+
}
8501+
if (strpos($path, '{column}')) {
8502+
$parameters[] = 'column';
8503+
}
8504+
foreach ($parameters as $p => $parameter) {
8505+
$this->openapi->set("paths|$path|$method|parameters|$p|\$ref", "#/components/parameters/$parameter");
8506+
}
8507+
$operationType = $operation . ucfirst($type);
8508+
if (in_array($operation, ['create', 'update'])) {
8509+
$this->openapi->set("paths|$path|$method|requestBody|\$ref", "#/components/requestBodies/$operationType");
8510+
}
8511+
$this->openapi->set("paths|$path|$method|tags|0", "$type");
8512+
$this->openapi->set("paths|$path|$method|description", "$operation $type");
8513+
switch ($operation) {
8514+
case 'read':
8515+
$this->openapi->set("paths|$path|$method|responses|200|\$ref", "#/components/responses/$operationType");
8516+
break;
8517+
case 'create':
8518+
case 'update':
8519+
case 'delete':
8520+
$this->openapi->set("paths|$path|$method|responses|200|\$ref", "#/components/responses/boolSuccess");
8521+
break;
8522+
}
8523+
}
8524+
}
8525+
}
8526+
8527+
private function setComponentSchema() /*: void*/
8528+
{
8529+
foreach (array_keys($this->operations) as $type) {
8530+
foreach (array_keys($this->operations[$type]) as $operation) {
8531+
if ($operation == 'delete') {
8532+
continue;
8533+
}
8534+
$operationType = $operation . ucfirst($type);
8535+
$prefix = "components|schemas|$operationType";
8536+
$this->openapi->set("$prefix|type", "object");
8537+
switch ($type) {
8538+
case 'database':
8539+
$this->openapi->set("$prefix|properties|tables|type", 'array');
8540+
$this->openapi->set("$prefix|properties|tables|items|\$ref", "#/components/responses/readTable");
8541+
break;
8542+
case 'table':
8543+
$this->openapi->set("$prefix|properties|name|type", 'string');
8544+
$this->openapi->set("$prefix|properties|type|type", 'string');
8545+
$this->openapi->set("$prefix|properties|columns|type", 'array');
8546+
$this->openapi->set("$prefix|properties|columns|items|\$ref", "#/components/responses/readColumn");
8547+
break;
8548+
case 'column':
8549+
$this->openapi->set("$prefix|properties|name|type", 'string');
8550+
$this->openapi->set("$prefix|properties|type|type", 'string');
8551+
$this->openapi->set("$prefix|properties|length|type", 'integer');
8552+
$this->openapi->set("$prefix|properties|length|format", "int64");
8553+
$this->openapi->set("$prefix|properties|precision|type", 'integer');
8554+
$this->openapi->set("$prefix|properties|precision|format", "int64");
8555+
$this->openapi->set("$prefix|properties|scale|type", 'integer');
8556+
$this->openapi->set("$prefix|properties|scale|format", "int64");
8557+
$this->openapi->set("$prefix|properties|nullable|type", 'boolean');
8558+
$this->openapi->set("$prefix|properties|pk|type", 'boolean');
8559+
$this->openapi->set("$prefix|properties|fk|type", 'string');
8560+
break;
8561+
}
8562+
}
8563+
}
8564+
}
8565+
8566+
private function setComponentResponse() /*: void*/
8567+
{
8568+
foreach (array_keys($this->operations) as $type) {
8569+
foreach (array_keys($this->operations[$type]) as $operation) {
8570+
if ($operation != 'read') {
8571+
continue;
8572+
}
8573+
$operationType = $operation . ucfirst($type);
8574+
$this->openapi->set("components|responses|$operationType|description", "single $type record");
8575+
$this->openapi->set("components|responses|$operationType|content|application/json|schema|\$ref", "#/components/schemas/$operationType");
8576+
}
8577+
}
8578+
}
8579+
8580+
private function setComponentRequestBody() /*: void*/
8581+
{
8582+
foreach (array_keys($this->operations) as $type) {
8583+
foreach (array_keys($this->operations[$type]) as $operation) {
8584+
if (!in_array($operation, ['create', 'update'])) {
8585+
continue;
8586+
}
8587+
$operationType = $operation . ucfirst($type);
8588+
$this->openapi->set("components|requestBodies|$operationType|description", "single $type record");
8589+
$this->openapi->set("components|requestBodies|$operationType|content|application/json|schema|\$ref", "#/components/schemas/$operationType");
8590+
}
8591+
}
8592+
}
8593+
8594+
private function setComponentParameters() /*: void*/
8595+
{
8596+
$this->openapi->set("components|parameters|table|name", "table");
8597+
$this->openapi->set("components|parameters|table|in", "path");
8598+
$this->openapi->set("components|parameters|table|schema|type", "string");
8599+
$this->openapi->set("components|parameters|table|description", "table name");
8600+
$this->openapi->set("components|parameters|table|required", true);
8601+
8602+
$this->openapi->set("components|parameters|column|name", "column");
8603+
$this->openapi->set("components|parameters|column|in", "path");
8604+
$this->openapi->set("components|parameters|column|schema|type", "string");
8605+
$this->openapi->set("components|parameters|column|description", "column name");
8606+
$this->openapi->set("components|parameters|column|required", true);
8607+
}
8608+
8609+
private function setTag(int $index, string $type) /*: void*/
8610+
{
8611+
$this->openapi->set("tags|$index|name", "$type");
8612+
$this->openapi->set("tags|$index|description", "$type operations");
84548613
}
84558614
}
84568615
}

src/Tqdev/PhpCrudApi/Database/TypeConverter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public function __construct(string $driver)
103103
],
104104
// source: https://docs.microsoft.com/en-us/sql/connect/jdbc/using-basic-data-types?view=sql-server-2017
105105
'sqlsrv' => [
106-
'varbinary(0)' => 'blob',
106+
'varbinary()' => 'blob',
107107
'bit' => 'boolean',
108108
'datetime' => 'timestamp',
109109
'datetime2' => 'timestamp',

src/Tqdev/PhpCrudApi/OpenApi/OpenApiBuilder.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public function __construct(ReflectionService $reflection, $base)
1515
{
1616
$this->openapi = new OpenApiDefinition($base);
1717
$this->records = new OpenApiRecordsBuilder($this->openapi, $reflection);
18-
$this->columns = new OpenApiColumnsBuilder($this->openapi, $reflection);
18+
$this->columns = new OpenApiColumnsBuilder($this->openapi);
1919
}
2020

2121
private function getServerUrl(): string
@@ -35,6 +35,7 @@ public function build(): OpenApiDefinition
3535
$this->openapi->set("servers|0|url", $this->getServerUrl());
3636
}
3737
$this->records->build();
38+
//$this->columns->build();
3839
return $this->openapi;
3940
}
4041
}

0 commit comments

Comments
 (0)