14
14
use AsyncAws \CodeGenerator \Generator \Composer \RequirementsRegistry ;
15
15
use AsyncAws \CodeGenerator \Generator \GeneratorHelper ;
16
16
use AsyncAws \CodeGenerator \Generator \Naming \NamespaceRegistry ;
17
+ use AsyncAws \Core \Exception \InvalidArgument ;
17
18
18
19
/**
19
20
* Serialize a request body to a flattened array with "." as separator.
24
25
*/
25
26
class QuerySerializer implements Serializer
26
27
{
28
+ use UseClassesTrait;
29
+
27
30
/**
28
31
* @var NamespaceRegistry
29
32
*/
@@ -45,10 +48,12 @@ public function getHeaders(Operation $operation): string
45
48
return '["content-type" => "application/x-www-form-urlencoded"] ' ;
46
49
}
47
50
48
- public function generateRequestBody (Operation $ operation , StructureShape $ shape ): array
51
+ public function generateRequestBody (Operation $ operation , StructureShape $ shape ): SerializerResultBody
49
52
{
53
+ $ this ->usedClassesInit ();
50
54
if (null !== $ payloadProperty = $ shape ->getPayload ()) {
51
55
if ($ shape ->getMember ($ payloadProperty )->isRequired ()) {
56
+ $ this ->usedClassesAdd (InvalidArgument::class);
52
57
$ body = 'if (null === $v = $this->PROPERTY) {
53
58
throw new InvalidArgument(sprintf( \'Missing parameter "NAME" for "%s". The value cannot be null. \', __CLASS__));
54
59
}
@@ -57,20 +62,21 @@ public function generateRequestBody(Operation $operation, StructureShape $shape)
57
62
$ body = '$body = $this->PROPERTY ?? ""; ' ;
58
63
}
59
64
60
- return [ strtr ($ body , [
65
+ return new SerializerResultBody ( strtr ($ body , [
61
66
'PROPERTY ' => GeneratorHelper::normalizeName ($ payloadProperty ),
62
67
'NAME ' => $ payloadProperty ,
63
- ]), false ] ;
68
+ ]), false , $ this -> usedClassesFlush ()) ;
64
69
}
65
70
66
- return [ strtr ('$body = http_build_query([ \'Action \' => OPERATION_NAME, \'Version \' => API_VERSION] + $this->requestBody(), \'\', \'& \', \PHP_QUERY_RFC1738); ' , [
71
+ return new SerializerResultBody ( strtr ('$body = http_build_query([ \'Action \' => OPERATION_NAME, \'Version \' => API_VERSION] + $this->requestBody(), \'\', \'& \', \PHP_QUERY_RFC1738); ' , [
67
72
'OPERATION_NAME ' => var_export ($ operation ->getName (), true ),
68
73
'API_VERSION ' => var_export ($ operation ->getApiVersion (), true ),
69
- ]), true ] ;
74
+ ]), true , $ this -> usedClassesFlush ()) ;
70
75
}
71
76
72
- public function generateRequestBuilder (StructureShape $ shape , bool $ needsChecks ): array
77
+ public function generateRequestBuilder (StructureShape $ shape , bool $ needsChecks ): SerializerResultBuilder
73
78
{
79
+ $ this ->usedClassesInit ();
74
80
$ body = implode ("\n" , array_map (function (StructureMember $ member ) use ($ needsChecks ) {
75
81
if (null !== $ member ->getLocation ()) {
76
82
return '' ;
@@ -85,6 +91,7 @@ public function generateRequestBuilder(StructureShape $shape, bool $needsChecks)
85
91
$ inputElement = '$v ' ;
86
92
} elseif ($ member ->isRequired ()) {
87
93
if ($ needsChecks ) {
94
+ $ this ->usedClassesAdd (InvalidArgument::class);
88
95
$ body = 'if (null === $v = $this->PROPERTY) {
89
96
throw new InvalidArgument(sprintf( \'Missing parameter "NAME" for "%s". The value cannot be null. \', __CLASS__));
90
97
}
@@ -113,14 +120,14 @@ public function generateRequestBuilder(StructureShape $shape, bool $needsChecks)
113
120
]);
114
121
}, $ shape ->getMembers ()));
115
122
116
- return [ 'array ' , strtr ('
123
+ return new SerializerResultBuilder ( 'array ' , strtr ('
117
124
$payload = [];
118
125
CHILDREN_CODE
119
126
120
127
return $payload;
121
128
' , [
122
129
'CHILDREN_CODE ' => $ body ,
123
- ])] ;
130
+ ]), $ this -> usedClassesFlush ()) ;
124
131
}
125
132
126
133
private function getQueryName (Member $ member , string $ default ): string
@@ -206,6 +213,7 @@ private function dumpArrayMap(string $output, string $input, string $contextProp
206
213
$ mapKeyShape = $ shape ->getKey ()->getShape ();
207
214
if (!empty ($ mapKeyShape ->getEnum ())) {
208
215
$ enumClassName = $ this ->namespaceRegistry ->getEnum ($ mapKeyShape );
216
+ $ this ->usedClassesAdd (InvalidArgument::class);
209
217
$ validateEnum = strtr ('if (!ENUM_CLASS::exists($mapKey)) {
210
218
throw new InvalidArgument(sprintf( \'Invalid key for "%s". The value "%s" is not a valid "ENUM_CLASS". \', __CLASS__, $mapKey));
211
219
} ' , [
@@ -228,7 +236,7 @@ private function dumpArrayMap(string $output, string $input, string $contextProp
228
236
'INPUT ' => $ input ,
229
237
'VALIDATE_ENUM ' => $ validateEnum ,
230
238
'OUTPUT_KEY ' => sprintf ('%s.$index.%s ' , $ output , $ this ->getQueryName ($ shape ->getKey (), 'key ' )),
231
- 'MEMBER_CODE ' => $ memberCode = $ this ->dumpArrayElement (sprintf ('%s.$index.%s ' , $ output , $ this ->getQueryName ($ shape ->getValue (), 'value ' )), '$mapValue ' , $ contextProperty , $ shape ->getValue ()->getShape ()),
239
+ 'MEMBER_CODE ' => $ this ->dumpArrayElement (sprintf ('%s.$index.%s ' , $ output , $ this ->getQueryName ($ shape ->getValue (), 'value ' )), '$mapValue ' , $ contextProperty , $ shape ->getValue ()->getShape ()),
232
240
]);
233
241
}
234
242
@@ -245,7 +253,7 @@ private function dumpArrayList(string $output, string $input, string $contextPro
245
253
' ,
246
254
[
247
255
'INPUT ' => $ input ,
248
- 'MEMBER_CODE ' => $ memberCode = $ this ->dumpArrayElement (sprintf ('%s.$index ' , $ output ), '$mapValue ' , $ contextProperty , $ memberShape ),
256
+ 'MEMBER_CODE ' => $ this ->dumpArrayElement (sprintf ('%s.$index ' , $ output ), '$mapValue ' , $ contextProperty , $ memberShape ),
249
257
]);
250
258
}
251
259
@@ -258,6 +266,7 @@ private function dumpArrayScalar(string $output, string $input, string $contextP
258
266
];
259
267
if (!empty ($ shape ->getEnum ())) {
260
268
$ enumClassName = $ this ->namespaceRegistry ->getEnum ($ shape );
269
+ $ this ->usedClassesAdd (InvalidArgument::class);
261
270
$ body = 'if (!ENUM_CLASS::exists(INPUT)) {
262
271
throw new InvalidArgument(sprintf( \'Invalid parameter "PROPERTY" for "%s". The value "%s" is not a valid "ENUM_CLASS". \', __CLASS__, INPUT));
263
272
}
0 commit comments