5
5
namespace DoclerLabs \ApiClientGenerator \Ast \Builder ;
6
6
7
7
use DoclerLabs \ApiClientGenerator \Ast \PhpVersion ;
8
+ use DoclerLabs \ApiClientGenerator \Entity \FieldType ;
8
9
use DoclerLabs \ApiClientGenerator \Entity \ImportCollection ;
9
10
use InvalidArgumentException ;
10
11
use PhpParser \BuilderFactory ;
16
17
use PhpParser \Node \Expr \Array_ ;
17
18
use PhpParser \Node \Expr \ArrayDimFetch ;
18
19
use PhpParser \Node \Expr \ArrayItem ;
20
+ use PhpParser \Node \Expr \ArrowFunction ;
19
21
use PhpParser \Node \Expr \Assign ;
20
22
use PhpParser \Node \Expr \BinaryOp ;
21
23
use PhpParser \Node \Expr \BinaryOp \BooleanAnd ;
@@ -102,11 +104,29 @@ public function closure(
102
104
return new Closure ($ subNodes );
103
105
}
104
106
107
+ public function arrowFunction (Expr $ expr , array $ params , string $ returnType , bool $ isStatic = true ): ArrowFunction
108
+ {
109
+ if (!empty ($ params )) {
110
+ $ subNodes ['params ' ] = $ params ;
111
+ }
112
+
113
+ $ subNodes ['expr ' ] = $ expr ;
114
+ $ subNodes ['returnType ' ] = $ returnType ;
115
+ $ subNodes ['static ' ] = $ isStatic ;
116
+
117
+ return new ArrowFunction ($ subNodes );
118
+ }
119
+
105
120
public function class (string $ name ): ClassBuilder
106
121
{
107
122
return new ClassBuilder ($ name );
108
123
}
109
124
125
+ public function enum (string $ name ): EnumBuilder
126
+ {
127
+ return new EnumBuilder ($ name );
128
+ }
129
+
110
130
public function array (array $ items ): Array_
111
131
{
112
132
$ arrayItems = [];
@@ -141,14 +161,15 @@ public function localProperty(
141
161
string $ type ,
142
162
string $ docType ,
143
163
bool $ nullable = false ,
144
- Expr $ default = null
164
+ Expr $ default = null ,
165
+ bool $ readonly = false
145
166
): Property {
146
167
$ property = $ this
147
168
->property ($ name )
148
169
->makePrivate ();
149
170
150
171
if (!empty ($ type ) && $ this ->phpVersion ->isPropertyTypeHintSupported ()) {
151
- if ($ nullable ) {
172
+ if ($ nullable && $ type !== FieldType:: PHP_TYPE_MIXED ) {
152
173
$ property ->setDefault (null );
153
174
$ type = '? ' . $ type ;
154
175
}
@@ -163,6 +184,13 @@ public function localProperty(
163
184
$ property ->setDefault ($ default );
164
185
}
165
186
187
+ if (
188
+ $ readonly
189
+ && $ this ->phpVersion ->isReadonlyPropertySupported ()
190
+ ) {
191
+ $ property ->makeReadonly ();
192
+ }
193
+
166
194
return $ property ->getNode ();
167
195
}
168
196
@@ -176,6 +204,11 @@ public function localNullsafePropertyFetch(string $propertyName): NullsafeProper
176
204
return new NullsafePropertyFetch ($ this ->var ('this ' ), BuilderHelpers::normalizeIdentifierOrExpr ($ propertyName ));
177
205
}
178
206
207
+ public function nullsafePropertyFetch (Expr $ var , string $ propertyName ): NullsafePropertyFetch
208
+ {
209
+ return new NullsafePropertyFetch ($ var , BuilderHelpers::normalizeIdentifierOrExpr ($ propertyName ));
210
+ }
211
+
179
212
public function localMethodCall (string $ methodName , array $ args = []): MethodCall
180
213
{
181
214
return $ this ->methodCall ($ this ->var ('this ' ), $ methodName , $ args );
0 commit comments