@@ -16,10 +16,10 @@ final class Schema
1616 * @param string $name
1717 * @param string $namespace
1818 * @param string $className
19- * @param OpenAPiSchema $operation
19+ * @param OpenAPiSchema $schema
2020 * @return iterable<Node>
2121 */
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
2323 {
2424 $ factory = new BuilderFactory ();
2525 $ stmt = $ factory ->namespace ($ namespace );
@@ -30,30 +30,42 @@ public static function generate(string $name, string $namespace, string $classNa
3030 new Node \Const_ (
3131 'SCHEMA_TITLE ' ,
3232 new Node \Scalar \String_ (
33- $ operation ->title ?? $ name
33+ $ schema ->title ?? $ name
3434 )
3535 ),
3636 ],
3737 Class_::MODIFIER_PUBLIC
3838 )
3939 )->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 (
4051 new Node \Stmt \ClassConst (
4152 [
4253 new Node \Const_ (
4354 'SCHEMA_DESCRIPTION ' ,
4455 new Node \Scalar \String_ (
45- $ operation ->description ?? ''
56+ $ schema ->description ?? ''
4657 )
4758 ),
4859 ],
4960 Class_::MODIFIER_PUBLIC
5061 )
5162 );
5263
53- foreach ($ operation ->properties as $ propertyName => $ property ) {
64+ foreach ($ schema ->properties as $ propertyName => $ property ) {
5465 $ propertyStmt = $ factory ->property ($ propertyName )->makePrivate ();
66+ $ docBlock = [];
5567 if (strlen ($ property ->description ) > 0 ) {
56- $ propertyStmt -> setDocComment ( ' /** ' . $ property ->description . ' **/ ' ) ;
68+ $ docBlock [] = $ property ->description ;
5769 }
5870 $ method = $ factory ->method ($ propertyName )->makePublic ()/*->setReturnType('string')*/ ->addStmt (
5971 new Node \Stmt \Return_ (
@@ -64,6 +76,9 @@ public static function generate(string $name, string $namespace, string $classNa
6476 )
6577 );
6678 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+ }
6782 $ propertyStmt ->setType (str_replace ([
6883 'integer ' ,
6984 'any ' ,
@@ -79,22 +94,12 @@ public static function generate(string $name, string $namespace, string $classNa
7994 '' ,
8095 ], $ property ->type ));
8196 }
82- $ class ->addStmt ($ propertyStmt )->addStmt ($ method );
8397
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 .' */ ' );
97100 }
101+
102+ $ class ->addStmt ($ propertyStmt )->addStmt ($ method );
98103 }
99104
100105 return $ stmt ->addStmt ($ class )->getNode ();
0 commit comments