@@ -31,16 +31,15 @@ public function generate(string $className): Schema
3131 $ class = new ReflectionClass ($ className );
3232 $ attributes = $ this ->reader ->readClassAttributes ($ class );
3333
34- return $ this ->generateFromClass ($ class , $ attributes );
34+ return $ this ->generateFromClass ($ class , new Type (TypeName:: RECORD , $ attributes) );
3535 }
3636
3737 /**
3838 * @param ReflectionClass<object> $class
3939 */
40- private function generateFromClass (ReflectionClass $ class , SchemaAttributes $ attributes ): Schema
40+ private function generateFromClass (ReflectionClass $ class , Type $ type ): Schema
4141 {
42- $ type = $ attributes ->types ()[0 ];
43- $ schema = $ this ->schemaFromType ($ type ->value (), $ attributes );
42+ $ schema = $ this ->schemaFromTypes ($ type );
4443
4544 if (!$ schema instanceof Schema \RecordType) {
4645 return $ schema ;
@@ -54,9 +53,20 @@ private function generateFromClass(ReflectionClass $class, SchemaAttributes $att
5453 return $ schema ;
5554 }
5655
57- private function schemaFromType ( string $ type , SchemaAttributes $ attributes ): Schema
56+ private function schemaFromTypes ( Type ... $ types ): Schema
5857 {
59- switch ($ type ) {
58+ if (\count ($ types ) > 1 ) {
59+ $ unionSchemas = \array_map (function (Type $ type ) {
60+ return $ this ->schemaFromTypes ($ type );
61+ }, $ types );
62+
63+ return Schema::union (...$ unionSchemas );
64+ }
65+
66+ $ type = $ types [0 ];
67+ $ attributes = $ type ->getAttributes ();
68+
69+ switch ($ type ->getTypeName ()) {
6070 case TypeName::RECORD :
6171 if ($ attributes ->has (AttributeName::TARGET_CLASS )) {
6272 return $ this ->generate ($ attributes ->get (AttributeName::TARGET_CLASS ));
@@ -125,16 +135,7 @@ private function parseField(ReflectionProperty $property, Schema\RecordType $roo
125135 return $ rootSchema ;
126136 }
127137
128- $ mainType = $ attributes ->types ()[0 ];
129- $ fieldSchema = $ this ->schemaFromType ($ mainType ->value (), $ mainType ->attributes ());
130-
131- if (1 < \count ($ attributes ->types ())) {
132- $ unionSchemas = \array_map (function (SchemaAttribute $ type ) {
133- return $ this ->schemaFromType ($ type ->value (), $ type ->attributes ());
134- }, $ attributes ->types ());
135-
136- $ fieldSchema = Schema::union (...$ unionSchemas );
137- }
138+ $ fieldSchema = $ this ->schemaFromTypes (...$ attributes ->types ());
138139
139140 $ fieldArgs = [
140141 $ attributes ->has (AttributeName::NAME ) ? $ attributes ->get (AttributeName::NAME ) : $ property ->getName (),
@@ -165,25 +166,16 @@ private function parseField(ReflectionProperty $property, Schema\RecordType $roo
165166
166167 private function applyAttributes (Schema $ schema , SchemaAttributes $ attributes ): Schema
167168 {
168- $ variadicValues = [
169- AttributeName::SYMBOLS ,
170- AttributeName::ALIASES ,
171- ];
172-
173- $ schemaValues = [
174- AttributeName::ITEMS ,
175- AttributeName::VALUES ,
176- ];
177-
178169 foreach ($ attributes ->options () as $ attribute ) {
179- if (\in_array ( $ attribute-> name (), $ variadicValues ) && \is_array ( $ attribute -> value ()) ) {
170+ if ($ attribute instanceof VariadicAttribute ) {
180171 $ schema = $ schema ->{$ attribute ->name ()}(...$ attribute ->value ());
181172
182173 continue ;
183174 }
184175
185- if (\in_array ($ attribute ->name (), $ schemaValues ) && \is_string ($ attribute ->value ())) {
186- $ schema = $ schema ->{$ attribute ->name ()}($ this ->schemaFromType ($ attribute ->value (), new SchemaAttributes ()));
176+ if ($ attribute instanceof TypeOnlyAttribute) {
177+ $ types = $ attribute ->attributes ()->types ();
178+ $ schema = $ schema ->{$ attribute ->name ()}($ this ->schemaFromTypes (...$ types ));
187179
188180 continue ;
189181 }
0 commit comments