66
77use ApiClients \Tools \OpenApiClientGenerator \Configuration \Namespace_ ;
88use ApiClients \Tools \OpenApiClientGenerator \Registry \Schema as SchemaRegistry ;
9- use ApiClients \Tools \OpenApiClientGenerator \Representation \PropertyType ;
10- use ApiClients \Tools \OpenApiClientGenerator \Representation \Schema ;
119use cebe \openapi \spec \Schema as baseSchema ;
1210use Jawira \CaseConverter \Convert ;
11+ use ApiClients \Tools \OpenApiClientGenerator \Representation ;
12+ use PhpParser \Node ;
13+ use NumberToWords \NumberToWords ;
1314
15+ use function array_filter ;
16+ use function array_values ;
1417use function count ;
18+ use function preg_replace_callback ;
1519use function str_replace ;
1620use function strlen ;
1721
@@ -20,28 +24,32 @@ final class Property
2024 public static function gather (
2125 Namespace_ $ baseNamespace ,
2226 string $ className ,
23- string $ propertyName ,
27+ string $ sourcePropertyName ,
2428 bool $ required ,
2529 baseSchema $ property ,
2630 SchemaRegistry $ schemaRegistry ,
27- ): \ApiClients \Tools \OpenApiClientGenerator \Representation \Property {
31+ ): Representation \Property {
32+ $ enum = [];
2833 $ exampleData = null ;
2934
3035 /** @phpstan-ignore-next-line */
3136 if (count ($ property ->examples ?? []) > 0 ) {
37+ $ examples = array_values (array_filter ($ property ->examples , static fn (mixed $ value ): bool => $ value !== null ));
3238 // Main reason we're doing this is so we cause more variety in the example data when a list of examples is provided, but also consistently pick the same item so we do don't cause code churn
3339 /** @phpstan-ignore-next-line */
34- $ exampleData = $ property -> examples [strlen ($ propertyName ) % 2 ? 0 : count ($ property -> examples ) - 1 ];
40+ $ exampleData = $ examples [strlen ($ sourcePropertyName ) % 2 ? 0 : count ($ examples ) - 1 ];
3541 }
3642
3743 if ($ exampleData === null && $ property ->example !== null ) {
3844 $ exampleData = $ property ->example ;
3945 }
4046
4147 if ($ exampleData === null && count ($ property ->enum ?? []) > 0 ) {
48+ $ enum = $ property ->enum ;
49+ $ enums = array_values (array_filter ($ property ->enum , static fn (mixed $ value ): bool => $ value !== null ));
4250 // Main reason we're doing this is so we cause more variety in the enum based example data, but also consistently pick the same item so we do don't cause code churn
4351 /** @phpstan-ignore-next-line */
44- $ exampleData = $ property -> enum [strlen ($ propertyName ) % 2 ? 0 : count ($ property -> enum ) - 1 ];
52+ $ exampleData = $ enums [strlen ($ sourcePropertyName ) % 2 ? 0 : count ($ enums ) - 1 ];
4553 }
4654
4755 $ propertyName = str_replace ([
@@ -51,10 +59,17 @@ public static function gather(
5159 '$ ' ,
5260 ], [
5361 '_AT_ ' ,
54- '_PLUSES_ ' ,
55- '_MINUS_ ' ,
56- '' ,
57- ], $ propertyName );
62+ '_PLUS_ ' ,
63+ '_MIN_ ' ,
64+ '_DOLLAR_ ' ,
65+ ], $ sourcePropertyName );
66+ $ propertyName = preg_replace_callback (
67+ '/[0-9]+/ ' ,
68+ static function ($ matches ) {
69+ return '_ ' . str_replace (['- ' , ' ' ], '_ ' , NumberToWords::transformNumber ('en ' , (int ) $ matches [0 ])) . '_ ' ;
70+ },
71+ $ propertyName ,
72+ );
5873
5974 $ type = Type::gather (
6075 $ baseNamespace ,
@@ -64,25 +79,39 @@ public static function gather(
6479 $ required ,
6580 $ schemaRegistry ,
6681 );
67- if ($ type ->payload instanceof Schema) {
82+ if ($ type ->payload instanceof Representation \ Schema) {
6883 if (count ($ type ->payload ->properties ) === 0 ) {
69- $ type = new PropertyType ('scalar ' , null , null , 'mixed ' , false );
84+ $ type = new Representation \ PropertyType ('scalar ' , null , null , 'string ' , false );
7085 }
71- } elseif ($ type ->payload instanceof PropertyType && $ type ->payload ->payload instanceof Schema) {
86+ } elseif ($ type ->payload instanceof Representation \ PropertyType && $ type ->payload ->payload instanceof Representation \ Schema) {
7287 if (count ($ type ->payload ->payload ->properties ) === 0 ) {
73- $ type = new PropertyType ('scalar ' , null , null , 'mixed ' , false );
88+ $ type = new Representation \ PropertyType ('scalar ' , null , null , 'string ' , false );
7489 }
7590 }
7691
77- $ exampleData = ExampleData::gather ($ exampleData , $ type , $ propertyName );
92+ if ($ property ->type === 'array ' && is_array ($ type ->payload )) {
93+ $ arrayItemsRaw = [];
94+ $ arrayItemsNode = [];
95+
96+ foreach ($ type ->payload as $ index => $ arrayItem ) {
97+ $ arrayItemExampleData = ExampleData::gather ($ exampleData , $ arrayItem , $ propertyName . str_pad ('' , $ index + 1 , '_ ' ));
98+ $ arrayItemsRaw [] = $ arrayItemExampleData ->raw ;
99+ $ arrayItemsNode [] = new Node \Expr \ArrayItem ($ arrayItemExampleData ->node );
100+ }
78101
79- return new \ApiClients \Tools \OpenApiClientGenerator \Representation \Property (
102+ $ exampleData = new Representation \ExampleData ($ arrayItemsRaw , new Node \Expr \Array_ ($ arrayItemsNode ));
103+ } else {
104+ $ exampleData = ExampleData::gather ($ exampleData , $ type , $ propertyName );
105+ }
106+
107+ return new Representation \Property (
80108 (new Convert ($ propertyName ))->toCamel (),
81- $ propertyName ,
109+ $ sourcePropertyName ,
82110 $ property ->description ?? '' ,
83111 $ exampleData ,
84- [$ type ],
85- $ type ->nullable
112+ $ type ,
113+ $ type ->nullable ,
114+ $ enum ,
86115 );
87116 }
88117}
0 commit comments