1
- <?php declare (strict_types=1 );
1
+ <?php
2
+
3
+ declare (strict_types=1 );
2
4
3
5
namespace DoclerLabs \ApiClientGenerator \Generator ;
4
6
8
10
use DoclerLabs \ApiClientGenerator \Entity \Constraint \ConstraintInterface ;
9
11
use DoclerLabs \ApiClientGenerator \Entity \Field ;
10
12
use DoclerLabs \ApiClientGenerator \Input \Specification ;
11
- use DoclerLabs \ApiClientGenerator \Naming \SchemaNaming ;
12
13
use DoclerLabs \ApiClientGenerator \Output \Php \PhpFileCollection ;
13
- use PhpParser \Node \Stmt ;
14
14
use PhpParser \Node \Stmt \ClassMethod ;
15
15
use PhpParser \Node \Stmt \Property ;
16
16
@@ -22,10 +22,11 @@ abstract public function generate(Specification $specification, PhpFileCollectio
22
22
23
23
protected function generateValidationStmts (Field $ field ): array
24
24
{
25
- return array_filter ([
26
- $ this ->generateEnumValidation ($ field ),
27
- ...$ this ->generateConstraints ($ field )
28
- ]);
25
+ return array_filter (
26
+ [
27
+ ...$ this ->generateConstraints ($ field ),
28
+ ]
29
+ );
29
30
}
30
31
31
32
protected function generateSet (Field $ field ): ClassMethod
@@ -97,68 +98,6 @@ protected function getGetMethodName(Field $field): string
97
98
return sprintf ('get%s ' , ucfirst ($ field ->getPhpVariableName ()));
98
99
}
99
100
100
- protected function generateEnumStatements (Field $ field ): array
101
- {
102
- $ statements = [];
103
- $ enumValues = $ field ->getEnumValues ();
104
- if (!empty ($ enumValues )) {
105
- $ enumConstCalls = [];
106
- foreach ($ enumValues as $ enumValue ) {
107
- $ constName = SchemaNaming::getEnumConstName ($ field , $ enumValue );
108
- $ statements [] = $ this ->builder ->constant (
109
- $ constName ,
110
- $ this ->builder ->val ($ enumValue )
111
- );
112
-
113
- $ enumConstCalls [] = $ this ->builder ->classConstFetch ('self ' , $ constName );
114
- }
115
- $ statements [] = $ this ->builder ->constant (
116
- SchemaNaming::getAllowedEnumConstName ($ field ),
117
- $ this ->builder ->array ($ enumConstCalls )
118
- );
119
- }
120
-
121
- return $ statements ;
122
- }
123
-
124
- protected function generateEnumValidation (Field $ root ): ?Stmt
125
- {
126
- $ enumValues = $ root ->getEnumValues ();
127
- if (empty ($ enumValues )) {
128
- return null ;
129
- }
130
-
131
- $ this
132
- ->addImport (RequestValidationException::class);
133
-
134
- $ propertyVar = $ this ->builder ->var ($ root ->getPhpVariableName ());
135
- $ allowedConstFetch = $ this ->builder ->classConstFetch (
136
- 'self ' ,
137
- SchemaNaming::getAllowedEnumConstName ($ root )
138
- );
139
-
140
- $ inArrayArgs = [
141
- $ propertyVar ,
142
- $ allowedConstFetch ,
143
- $ this ->builder ->val (true ),
144
- ];
145
- $ ifCondition = $ this ->builder ->not ($ this ->builder ->funcCall ('in_array ' , $ inArrayArgs ));
146
-
147
- $ exceptionMessage = $ this ->builder ->funcCall (
148
- 'sprintf ' ,
149
- [
150
- 'Invalid %s value. Given: `%s` Allowed: %s ' ,
151
- $ root ->getName (),
152
- $ propertyVar ,
153
- $ this ->builder ->funcCall ('json_encode ' , [$ allowedConstFetch ]),
154
- ]
155
- );
156
-
157
- $ ifStmt = $ this ->builder ->throw ('RequestValidationException ' , $ exceptionMessage );
158
-
159
- return $ this ->builder ->if ($ ifCondition , [$ ifStmt ]);
160
- }
161
-
162
101
protected function generateConstraints (Field $ root ): array
163
102
{
164
103
$ stmts = [];
@@ -182,7 +121,7 @@ protected function generateConstraints(Field $root): array
182
121
$ stmts [] = $ this ->builder ->if (
183
122
$ constraint ->getIfCondition ($ propertyVar , $ this ->builder ),
184
123
[
185
- $ this ->builder ->throw ('RequestValidationException ' , $ exceptionMessage )
124
+ $ this ->builder ->throw ('RequestValidationException ' , $ exceptionMessage ),
186
125
]
187
126
);
188
127
}
0 commit comments