@@ -33,13 +33,6 @@ public function openApiClassDescription(
33
33
$ lines [] = '' ;
34
34
}
35
35
36
- $ lines [] = "@OA \\Schema( \n * schema= \"{$ className }\", \n * title= \"\", " ;
37
- $ lines [] = " description= \"{$ className } entity \", " ;
38
- foreach ($ propertySchema as $ name => $ values ) {
39
- $ lines = array_merge ($ lines , $ this ->makeOpenAPIColumn ($ name , $ values ['type ' ], $ values ));
40
- }
41
- $ lines [] = ') ' ;
42
-
43
36
$ previous = false ;
44
37
foreach ($ annotations as $ annotation ) {
45
38
if (strlen ($ annotation ) > 1 && $ annotation [0 ] === '@ ' && strpos ($ annotation , ' ' ) > 0 ) {
@@ -60,14 +53,117 @@ public function openApiClassDescription(
60
53
return rtrim (" * {$ line }" );
61
54
})->toArray (), [' */ ' ]);
62
55
56
+ $ required = '' ;
57
+ foreach ($ propertySchema as $ name => $ values ) {
58
+ if ((array_key_exists ('null ' , $ values ) && $ values ['null ' ] === true )) {
59
+ continue ;
60
+ }
61
+
62
+ $ required .= strlen ($ required ) === 0 ? "' {$ name }' " : ", ' {$ name }' " ;
63
+ }
64
+
65
+ $ lines [] = '#[OA\Schema( ' ;
66
+ $ lines [] = " schema: ' {$ className }}', " ;
67
+ $ lines [] = " title: ' {$ className }', " ;
68
+ $ lines [] = " description: ' {$ className } Entity', " ;
69
+ $ lines [] = " required: [ {$ required }], " ;
70
+ $ lines [] = ' properties: [ ' ;
71
+ foreach ($ propertySchema as $ name => $ values ) {
72
+ $ lines = array_merge ($ lines , $ this ->makeOpenAPIColumn ($ name , $ values ['type ' ], $ values , 8 ));
73
+ }
74
+ $ lines [] = ' ] ' ;
75
+ $ lines [] = ')] ' ;
76
+
63
77
return implode ("\n" , $ lines );
64
78
}
65
79
66
- protected function makeOpenAPIColumn (string $ name , string $ type , array $ column ): array
80
+ public function schemaProperty (array $ propertySchema , $ name ): ?array {
81
+ $ format = null ;
82
+ $ type = $ propertySchema [$ name ]['type ' ];
83
+ switch ($ type ) {
84
+ case TableSchema::TYPE_CHAR :
85
+ case TableSchema::TYPE_STRING :
86
+ case TableSchema::TYPE_TEXT :
87
+ $ type = 'string ' ;
88
+ break ;
89
+
90
+ case TableSchema::TYPE_DECIMAL :
91
+ $ type = 'string ' ;
92
+ $ format = 'decimal ' ;
93
+ break ;
94
+
95
+ case TableSchema::TYPE_UUID :
96
+ $ type = 'string ' ;
97
+ $ format = 'uuid ' ;
98
+ break ;
99
+
100
+ case TableSchema::TYPE_DATE :
101
+ $ type = 'string ' ;
102
+ $ format = 'date ' ;
103
+ break ;
104
+
105
+ case TableSchema::TYPE_TIMESTAMP :
106
+ case TableSchema::TYPE_TIMESTAMP_FRACTIONAL :
107
+ case TableSchema::TYPE_TIMESTAMP_TIMEZONE :
108
+ case TableSchema::TYPE_DATETIME :
109
+ $ type = 'string ' ;
110
+ $ format = 'datetime ' ;
111
+ break ;
112
+
113
+ case TableSchema::TYPE_TIME :
114
+ $ type = 'string ' ;
115
+ $ format = '"time ' ;
116
+ break ;
117
+
118
+ case TableSchema::TYPE_INTEGER :
119
+ $ type = 'integer ' ;
120
+ $ format = 'int32 ' ;
121
+ break ;
122
+ case TableSchema::TYPE_BIGINTEGER :
123
+ $ type = 'integer ' ;
124
+ $ format = 'bigint ' ;
125
+ break ;
126
+
127
+ case TableSchema::TYPE_TINYINTEGER :
128
+ case TableSchema::TYPE_SMALLINTEGER :
129
+ $ type = 'integer ' ;
130
+ $ format = 'smallint ' ;
131
+ break ;
132
+
133
+ case TableSchema::TYPE_FLOAT :
134
+ $ type = 'float ' ;
135
+ break ;
136
+
137
+ case TableSchema::TYPE_JSON :
138
+ $ type = 'string ' ;
139
+ $ format = 'json ' ;
140
+ break ;
141
+ case TableSchema::TYPE_BOOLEAN :
142
+ $ type = 'boolean ' ;
143
+ break ;
144
+
145
+ default :
146
+ if ($ type [0 ] === '\\' ) {
147
+ // exclude associated properties.
148
+ return null ;
149
+ } else {
150
+ $ format = $ type ;
151
+ $ type = 'string ' ;
152
+ }
153
+ break ;
154
+ }
155
+
156
+ return [
157
+ 'type ' => $ type ,
158
+ 'format ' => $ format ,
159
+ ];
160
+ }
161
+
162
+ protected function makeOpenAPIColumn (string $ name , string $ type , array $ column , int $ indentNum = 12 ): array
67
163
{
164
+ $ indentString = str_repeat (' ' , $ indentNum );
68
165
$ result = [];
69
166
$ format = null ;
70
- $ comment = null ;
71
167
switch ($ type ) {
72
168
case TableSchema::TYPE_CHAR :
73
169
case TableSchema::TYPE_STRING :
@@ -77,45 +173,45 @@ protected function makeOpenAPIColumn(string $name, string $type, array $column):
77
173
78
174
case TableSchema::TYPE_DECIMAL :
79
175
$ type = 'string ' ;
80
- $ format = ' format=" decimal", ' ;
176
+ $ format = 'decimal ' ;
81
177
break ;
82
178
83
179
case TableSchema::TYPE_UUID :
84
180
$ type = 'string ' ;
85
- $ format = ' format=" uuid", ' ;
181
+ $ format = 'uuid ' ;
86
182
break ;
87
183
88
184
case TableSchema::TYPE_DATE :
89
185
$ type = 'string ' ;
90
- $ format = ' format=" date", ' ;
186
+ $ format = 'date ' ;
91
187
break ;
92
188
93
189
case TableSchema::TYPE_TIMESTAMP :
94
190
case TableSchema::TYPE_TIMESTAMP_FRACTIONAL :
95
191
case TableSchema::TYPE_TIMESTAMP_TIMEZONE :
96
192
case TableSchema::TYPE_DATETIME :
97
193
$ type = 'string ' ;
98
- $ format = ' format=" datetime", ' ;
194
+ $ format = 'datetime ' ;
99
195
break ;
100
196
101
197
case TableSchema::TYPE_TIME :
102
198
$ type = 'string ' ;
103
- $ format = ' format= "time", ' ;
199
+ $ format = '"time ' ;
104
200
break ;
105
201
106
202
case TableSchema::TYPE_INTEGER :
107
203
$ type = 'integer ' ;
108
- $ format = ' format=" int32", ' ;
204
+ $ format = 'int32 ' ;
109
205
break ;
110
206
case TableSchema::TYPE_BIGINTEGER :
111
207
$ type = 'integer ' ;
112
- $ format = ' format=" bigint", ' ;
208
+ $ format = 'bigint ' ;
113
209
break ;
114
210
115
211
case TableSchema::TYPE_TINYINTEGER :
116
212
case TableSchema::TYPE_SMALLINTEGER :
117
213
$ type = 'integer ' ;
118
- $ format = ' format=" smallint", ' ;
214
+ $ format = 'smallint ' ;
119
215
break ;
120
216
121
217
case TableSchema::TYPE_FLOAT :
@@ -124,7 +220,7 @@ protected function makeOpenAPIColumn(string $name, string $type, array $column):
124
220
125
221
case TableSchema::TYPE_JSON :
126
222
$ type = 'string ' ;
127
- $ format = ' format=" json", ' ;
223
+ $ format = 'json ' ;
128
224
break ;
129
225
case TableSchema::TYPE_BOOLEAN :
130
226
$ type = 'boolean ' ;
@@ -135,24 +231,24 @@ protected function makeOpenAPIColumn(string $name, string $type, array $column):
135
231
// exclude associated properties.
136
232
return $ result ;
137
233
} else {
138
- $ format = " format= \"{ $ type}\" , " ;
234
+ $ format = $ type ;
139
235
$ type = 'string ' ;
140
236
}
141
237
break ;
142
238
}
143
239
144
- if ( is_null ( $ comment) && !empty ($ column ['comment ' ])) {
145
- $ comment = str_replace ("\n" , ' ' , $ column ['comment ' ]);
146
- }
240
+ $ comment = !empty ($ column ['comment ' ])
241
+ ? str_replace ("\n" , ' ' , $ column ['comment ' ])
242
+ : '' ;
147
243
148
- $ result [] = ' @ OA\Property(' ;
149
- $ result [] = " property= \" {$ name }\" , " ;
150
- $ result [] = " type= \" {$ type }\" , " ;
244
+ $ result [] = "{ $ indentString } new OA\Property(" ;
245
+ $ result [] = "{ $ indentString } property: ' {$ name }' , " ;
246
+ $ result [] = "{ $ indentString } type: ' {$ type }' , " ;
151
247
if (!empty ($ format )) {
152
- $ result [] = $ format ;
248
+ $ result [] = "{ $ indentString } format: ' { $ format } ', " ;
153
249
}
154
- $ result [] = " description= \" {$ comment }\" , " ;
155
- $ result [] = ' ), ' ;
250
+ $ result [] = "{ $ indentString } description: ' {$ comment }' , " ;
251
+ $ result [] = "{ $ indentString } ), " ;
156
252
157
253
return $ result ;
158
254
}
@@ -162,6 +258,45 @@ public function openApiActionBody(
162
258
): string {
163
259
$ lines = [];
164
260
261
+ $ exclude = ['id ' , 'created ' , 'modified ' ];
262
+
263
+ $ lines [] = ' requestBody: new OA\RequestBody( ' ;
264
+ $ lines [] = ' required: true, ' ;
265
+ $ lines [] = ' content: new OA\JsonContent( ' ;
266
+ $ required = '' ;
267
+ foreach ($ propertySchema as $ name => $ values ) {
268
+ if (in_array ($ name , $ exclude ) || (array_key_exists ('null ' , $ values ) && $ values ['null ' ] === true )) {
269
+ continue ;
270
+ }
271
+
272
+ $ required .= strlen ($ required ) === 0 ? "' {$ name }' " : ", ' {$ name }' " ;
273
+ }
274
+ $ lines [] = " required: [ {$ required }], " ;
275
+
276
+ $ lines [] = ' properties: [ ' ;
277
+
278
+ foreach ($ propertySchema as $ name => $ values ) {
279
+ if (in_array ($ name , $ exclude )) {
280
+ continue ;
281
+ }
282
+
283
+ $ lines = array_merge ($ lines , (new Collection ($ this ->makeOpenAPIColumn ($ name , $ values ['type ' ], $ values )))->map (function ($ line ) {
284
+ return rtrim ($ line );
285
+ })->toArray ());
286
+ }
287
+
288
+ $ lines [] = ' ] ' ;
289
+ $ lines [] = ' ) ' ;
290
+ $ lines [] = ' ), ' ;
291
+
292
+ return implode ("\n" , $ lines );
293
+ }
294
+
295
+ public function openApiActionBodyDocComment (
296
+ array $ propertySchema
297
+ ): string {
298
+ $ lines = [];
299
+
165
300
$ lines [] = ' * @OA\RequestBody( ' ;
166
301
$ lines [] = ' * required=true, ' ;
167
302
$ lines [] = ' * @OA\JsonContent( ' ;
@@ -173,7 +308,7 @@ public function openApiActionBody(
173
308
}
174
309
175
310
$ lines = array_merge ($ lines , (new Collection ($ this ->makeOpenAPIColumn ($ name , $ values ['type ' ], $ values )))->map (function ($ line ) {
176
- return rtrim (" * { $ line}" );
311
+ return rtrim ($ line );
177
312
})->toArray ());
178
313
}
179
314
0 commit comments