1414use AsyncAws \CodeGenerator \Generator \Composer \RequirementsRegistry ;
1515use AsyncAws \CodeGenerator \Generator \GeneratorHelper ;
1616use AsyncAws \CodeGenerator \Generator \Naming \NamespaceRegistry ;
17+ use AsyncAws \Core \Exception \InvalidArgument ;
1718
1819/**
1920 * Serialize a request body to a flattened array with "." as separator.
2425 */
2526class QuerySerializer implements Serializer
2627{
28+ use UseClassesTrait;
29+
2730 /**
2831 * @var NamespaceRegistry
2932 */
@@ -45,10 +48,12 @@ public function getHeaders(Operation $operation): string
4548 return '["content-type" => "application/x-www-form-urlencoded"] ' ;
4649 }
4750
48- public function generateRequestBody (Operation $ operation , StructureShape $ shape ): array
51+ public function generateRequestBody (Operation $ operation , StructureShape $ shape ): SerializerResultBody
4952 {
53+ $ this ->usedClassesInit ();
5054 if (null !== $ payloadProperty = $ shape ->getPayload ()) {
5155 if ($ shape ->getMember ($ payloadProperty )->isRequired ()) {
56+ $ this ->usedClassesAdd (InvalidArgument::class);
5257 $ body = 'if (null === $v = $this->PROPERTY) {
5358 throw new InvalidArgument(sprintf( \'Missing parameter "NAME" for "%s". The value cannot be null. \', __CLASS__));
5459 }
@@ -57,20 +62,21 @@ public function generateRequestBody(Operation $operation, StructureShape $shape)
5762 $ body = '$body = $this->PROPERTY ?? ""; ' ;
5863 }
5964
60- return [ strtr ($ body , [
65+ return new SerializerResultBody ( strtr ($ body , [
6166 'PROPERTY ' => GeneratorHelper::normalizeName ($ payloadProperty ),
6267 'NAME ' => $ payloadProperty ,
63- ]), false ] ;
68+ ]), false , $ this -> usedClassesFlush ()) ;
6469 }
6570
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); ' , [
6772 'OPERATION_NAME ' => var_export ($ operation ->getName (), true ),
6873 'API_VERSION ' => var_export ($ operation ->getApiVersion (), true ),
69- ]), true ] ;
74+ ]), true , $ this -> usedClassesFlush ()) ;
7075 }
7176
72- public function generateRequestBuilder (StructureShape $ shape , bool $ needsChecks ): array
77+ public function generateRequestBuilder (StructureShape $ shape , bool $ needsChecks ): SerializerResultBuilder
7378 {
79+ $ this ->usedClassesInit ();
7480 $ body = implode ("\n" , array_map (function (StructureMember $ member ) use ($ needsChecks ) {
7581 if (null !== $ member ->getLocation ()) {
7682 return '' ;
@@ -85,6 +91,7 @@ public function generateRequestBuilder(StructureShape $shape, bool $needsChecks)
8591 $ inputElement = '$v ' ;
8692 } elseif ($ member ->isRequired ()) {
8793 if ($ needsChecks ) {
94+ $ this ->usedClassesAdd (InvalidArgument::class);
8895 $ body = 'if (null === $v = $this->PROPERTY) {
8996 throw new InvalidArgument(sprintf( \'Missing parameter "NAME" for "%s". The value cannot be null. \', __CLASS__));
9097 }
@@ -113,14 +120,14 @@ public function generateRequestBuilder(StructureShape $shape, bool $needsChecks)
113120 ]);
114121 }, $ shape ->getMembers ()));
115122
116- return [ 'array ' , strtr ('
123+ return new SerializerResultBuilder ( 'array ' , strtr ('
117124 $payload = [];
118125 CHILDREN_CODE
119126
120127 return $payload;
121128 ' , [
122129 'CHILDREN_CODE ' => $ body ,
123- ])] ;
130+ ]), $ this -> usedClassesFlush ()) ;
124131 }
125132
126133 private function getQueryName (Member $ member , string $ default ): string
@@ -206,6 +213,7 @@ private function dumpArrayMap(string $output, string $input, string $contextProp
206213 $ mapKeyShape = $ shape ->getKey ()->getShape ();
207214 if (!empty ($ mapKeyShape ->getEnum ())) {
208215 $ enumClassName = $ this ->namespaceRegistry ->getEnum ($ mapKeyShape );
216+ $ this ->usedClassesAdd (InvalidArgument::class);
209217 $ validateEnum = strtr ('if (!ENUM_CLASS::exists($mapKey)) {
210218 throw new InvalidArgument(sprintf( \'Invalid key for "%s". The value "%s" is not a valid "ENUM_CLASS". \', __CLASS__, $mapKey));
211219 } ' , [
@@ -228,7 +236,7 @@ private function dumpArrayMap(string $output, string $input, string $contextProp
228236 'INPUT ' => $ input ,
229237 'VALIDATE_ENUM ' => $ validateEnum ,
230238 '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 ()),
232240 ]);
233241 }
234242
@@ -245,7 +253,7 @@ private function dumpArrayList(string $output, string $input, string $contextPro
245253 ' ,
246254 [
247255 '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 ),
249257 ]);
250258 }
251259
@@ -258,6 +266,7 @@ private function dumpArrayScalar(string $output, string $input, string $contextP
258266 ];
259267 if (!empty ($ shape ->getEnum ())) {
260268 $ enumClassName = $ this ->namespaceRegistry ->getEnum ($ shape );
269+ $ this ->usedClassesAdd (InvalidArgument::class);
261270 $ body = 'if (!ENUM_CLASS::exists(INPUT)) {
262271 throw new InvalidArgument(sprintf( \'Invalid parameter "PROPERTY" for "%s". The value "%s" is not a valid "ENUM_CLASS". \', __CLASS__, INPUT));
263272 }
0 commit comments