@@ -16,10 +16,10 @@ final class Schema
16
16
* @param string $name
17
17
* @param string $namespace
18
18
* @param string $className
19
- * @param OpenAPiSchema $operation
19
+ * @param OpenAPiSchema $schema
20
20
* @return iterable<Node>
21
21
*/
22
- public static function generate (string $ name , string $ namespace , string $ className , OpenAPiSchema $ operation ): Node
22
+ public static function generate (string $ name , string $ namespace , string $ className , OpenAPiSchema $ schema , array $ schemaClassNameMap ): Node
23
23
{
24
24
$ factory = new BuilderFactory ();
25
25
$ stmt = $ factory ->namespace ($ namespace );
@@ -30,30 +30,42 @@ public static function generate(string $name, string $namespace, string $classNa
30
30
new Node \Const_ (
31
31
'SCHEMA_TITLE ' ,
32
32
new Node \Scalar \String_ (
33
- $ operation ->title ?? $ name
33
+ $ schema ->title ?? $ name
34
34
)
35
35
),
36
36
],
37
37
Class_::MODIFIER_PUBLIC
38
38
)
39
39
)->addStmt (
40
+ new Node \Stmt \ClassConst (
41
+ [
42
+ new Node \Const_ (
43
+ 'SPL_HASH ' ,
44
+ new Node \Scalar \String_ (
45
+ spl_object_hash ($ schema )
46
+ )
47
+ ),
48
+ ],
49
+ Class_::MODIFIER_PUBLIC
50
+ ))->addStmt (
40
51
new Node \Stmt \ClassConst (
41
52
[
42
53
new Node \Const_ (
43
54
'SCHEMA_DESCRIPTION ' ,
44
55
new Node \Scalar \String_ (
45
- $ operation ->description ?? ''
56
+ $ schema ->description ?? ''
46
57
)
47
58
),
48
59
],
49
60
Class_::MODIFIER_PUBLIC
50
61
)
51
62
);
52
63
53
- foreach ($ operation ->properties as $ propertyName => $ property ) {
64
+ foreach ($ schema ->properties as $ propertyName => $ property ) {
54
65
$ propertyStmt = $ factory ->property ($ propertyName )->makePrivate ();
66
+ $ docBlock = [];
55
67
if (strlen ($ property ->description ) > 0 ) {
56
- $ propertyStmt -> setDocComment ( ' /** ' . $ property ->description . ' **/ ' ) ;
68
+ $ docBlock [] = $ property ->description ;
57
69
}
58
70
$ method = $ factory ->method ($ propertyName )->makePublic ()/*->setReturnType('string')*/ ->addStmt (
59
71
new Node \Stmt \Return_ (
@@ -64,6 +76,9 @@ public static function generate(string $name, string $namespace, string $classNa
64
76
)
65
77
);
66
78
if (is_string ($ property ->type )) {
79
+ if ($ property ->type === 'array ' && array_key_exists (spl_object_hash ($ property ->items ), $ schemaClassNameMap )) {
80
+ $ docBlock [] = '@var array< ' . $ namespace . '\\' . $ schemaClassNameMap [spl_object_hash ($ property ->items )] . '> ' ;
81
+ }
67
82
$ propertyStmt ->setType (str_replace ([
68
83
'integer ' ,
69
84
'any ' ,
@@ -79,22 +94,12 @@ public static function generate(string $name, string $namespace, string $classNa
79
94
'' ,
80
95
], $ property ->type ));
81
96
}
82
- $ class ->addStmt ($ propertyStmt )->addStmt ($ method );
83
97
84
- $ param = (new Param (
85
- $ propertyName
86
- ))/*->setType(
87
- str_replace([
88
- 'integer',
89
- 'any',
90
- ], [
91
- 'int',
92
- '',
93
- ], $property->type)
94
- )*/ ;
95
- if ($ property ->default !== null ) {
96
- $ param ->setDefault ($ property ->default );
98
+ if (count ($ docBlock ) > 0 ) {
99
+ $ propertyStmt ->setDocComment ('/** ' . PHP_EOL . ' * ' . implode (PHP_EOL . ' * ' , $ docBlock ) . PHP_EOL .' */ ' );
97
100
}
101
+
102
+ $ class ->addStmt ($ propertyStmt )->addStmt ($ method );
98
103
}
99
104
100
105
return $ stmt ->addStmt ($ class )->getNode ();
0 commit comments