8
8
use AsyncAws \CodeGenerator \Definition \MapShape ;
9
9
use AsyncAws \CodeGenerator \Definition \Member ;
10
10
use AsyncAws \CodeGenerator \Definition \Operation ;
11
+ use AsyncAws \CodeGenerator \Definition \StructureMember ;
11
12
use AsyncAws \CodeGenerator \Definition \StructureShape ;
12
13
use AsyncAws \CodeGenerator \Generator \CodeGenerator \TypeGenerator ;
13
14
use AsyncAws \CodeGenerator \Generator \Composer \RequirementsRegistry ;
@@ -304,6 +305,84 @@ public function generate(Operation $operation): ClassName
304
305
return $ className ;
305
306
}
306
307
308
+ private function inputClassRequestPartGettersEnumGenerator (Member $ member , string $ requestPart , string $ input ): string
309
+ {
310
+ $ memberShape = $ member ->getShape ();
311
+ $ validateEnum = '' ;
312
+ if (!empty ($ memberShape ->getEnum ())) {
313
+ $ enumClassName = $ this ->namespaceRegistry ->getEnum ($ memberShape );
314
+ $ validateEnum = strtr ('if (!ENUM_CLASS::exists(VALUE)) {
315
+ throw new InvalidArgument(sprintf( \'Invalid parameter "MEMBER_NAME" for "%s". The value "%s" is not a valid "ENUM_CLASS". \', __CLASS__, INPUT));
316
+ } ' , [
317
+ 'VALUE ' => $ this ->stringify ($ input , $ member , $ requestPart ),
318
+ 'ENUM_CLASS ' => $ enumClassName ->getName (),
319
+ 'INPUT ' => $ input ,
320
+ ]);
321
+ }
322
+
323
+ return $ validateEnum ;
324
+ }
325
+
326
+ private function inputClassRequestPartGettersHookGenerator (StructureMember $ member , Operation $ operation , string $ requestPart , string $ input ): string
327
+ {
328
+ $ applyHook = '' ;
329
+ foreach ($ operation ->getService ()->getHooks ($ requestPart . '_parameters ' ) as $ hook ) {
330
+ if (!\in_array ($ member ->getLocationName () ?? $ member ->getName (), $ hook ->getFilters ())) {
331
+ continue ;
332
+ }
333
+
334
+ $ applyHook .= $ this ->hookGenerator ->generate ($ hook , 'VALUE ' );
335
+ }
336
+
337
+ if (!$ applyHook ) {
338
+ return '' ;
339
+ }
340
+
341
+ return strtr ($ applyHook , [
342
+ 'VALUE ' => $ this ->stringify ($ input , $ member , $ requestPart ),
343
+ ]);
344
+ }
345
+
346
+ private function inputClassRequestPartGetters (StructureMember $ member , Operation $ operation , string $ requestPart , string $ input , string $ output ): string
347
+ {
348
+ $ memberShape = $ member ->getShape ();
349
+ if ($ memberShape instanceof ListShape) {
350
+ if ('header ' !== $ requestPart ) {
351
+ throw new \InvalidArgumentException (sprintf ('ListShape in request part "%s" is not yet implemented ' , $ requestPart ));
352
+ }
353
+
354
+ $ bodyCode = '
355
+ APPLY_HOOK
356
+ $items = [];
357
+ foreach (INPUT as $value) {
358
+ VALIDATE_ENUM
359
+ $items[] = VALUE;
360
+ }
361
+ OUTPUT = implode( \', \', $items);
362
+ ' ;
363
+
364
+ return strtr ($ bodyCode , [
365
+ 'INPUT ' => $ input ,
366
+ 'OUTPUT ' => $ output ,
367
+ 'VALUE ' => $ this ->stringify ('$value ' , $ memberShape ->getMember (), $ requestPart ),
368
+ 'VALIDATE_ENUM ' => $ this ->inputClassRequestPartGettersEnumGenerator ($ memberShape ->getMember (), $ requestPart , '$value ' ),
369
+ 'APPLY_HOOK ' => $ this ->inputClassRequestPartGettersHookGenerator ($ member , $ operation , $ requestPart , $ input ),
370
+ ]);
371
+ }
372
+
373
+ $ bodyCode = '
374
+ VALIDATE_ENUM
375
+ APPLY_HOOK
376
+ OUTPUT = VALUE; ' ;
377
+
378
+ return strtr ($ bodyCode , [
379
+ 'VALIDATE_ENUM ' => $ this ->inputClassRequestPartGettersEnumGenerator ($ member , $ requestPart , $ input ),
380
+ 'APPLY_HOOK ' => $ this ->inputClassRequestPartGettersHookGenerator ($ member , $ operation , $ requestPart , $ input ),
381
+ 'OUTPUT ' => $ output ,
382
+ 'VALUE ' => $ this ->stringify ($ input , $ member , $ requestPart ),
383
+ ]);
384
+ }
385
+
307
386
private function inputClassRequestGetters (StructureShape $ inputShape , ClassBuilder $ classBuilder , Operation $ operation ): void
308
387
{
309
388
$ serializer = $ this ->serializer ->get ($ operation ->getService ());
@@ -324,67 +403,39 @@ private function inputClassRequestGetters(StructureShape $inputShape, ClassBuild
324
403
if ($ requestPart !== $ member ->getLocation ()) {
325
404
continue ;
326
405
}
327
-
328
406
if ('querystring ' === $ requestPart && $ usesEndpointDiscovery ) {
329
407
if (0 !== strpos ($ classBuilder ->getClassName ()->getFqdn (), 'AsyncAws\Core \\' )) {
330
408
$ this ->requirementsRegistry ->addRequirement ('async-aws/core ' , '^1.19 ' );
331
409
}
332
410
}
333
411
334
- $ memberShape = $ member ->getShape ();
412
+ if (!isset ($ body [$ requestPart ])) {
413
+ $ body [$ requestPart ] = $ varName . ' = []; ' . "\n" ;
414
+ }
415
+
335
416
if ($ member ->isRequired ()) {
336
417
$ bodyCode = 'if (null === $v = $this->PROPERTY) {
337
- throw new InvalidArgument(sprintf( \'Missing parameter "NAME " for "%s". The value cannot be null. \', __CLASS__));
418
+ throw new InvalidArgument(sprintf( \'Missing parameter "MEMBER_NAME " for "%s". The value cannot be null. \', __CLASS__));
338
419
}
339
- VALIDATE_ENUM
340
- APPLY_HOOK
341
- VAR_NAME["LOCATION"] = VALUE; ' ;
342
- $ inputElement = '$v ' ;
420
+ GETTER_CODE ' ;
421
+ $ input = '$v ' ;
422
+ $ output = 'VAR_NAME["LOCATION"] ' ;
343
423
} else {
344
424
$ bodyCode = 'if (null !== $this->PROPERTY) {
345
- VALIDATE_ENUM
346
- APPLY_HOOK
347
- VAR_NAME["LOCATION"] = VALUE;
425
+ GETTER_CODE
348
426
} ' ;
349
- $ inputElement = '$this-> ' . GeneratorHelper::normalizeName ($ member ->getName ());
427
+ $ input = '$this->PROPERTY ' ;
428
+ $ output = 'VAR_NAME["LOCATION"] ' ;
350
429
}
351
- $ validateEnum = '' ;
352
- if (!empty ($ memberShape ->getEnum ())) {
353
- $ enumClassName = $ this ->namespaceRegistry ->getEnum ($ memberShape );
354
- $ validateEnum = strtr ('if (!ENUM_CLASS::exists(VALUE)) {
355
- throw new InvalidArgument(sprintf( \'Invalid parameter "NAME" for "%s". The value "%s" is not a valid "ENUM_CLASS". \', __CLASS__, $this->PROPERTY));
356
- } ' , [
357
- 'VALUE ' => $ this ->stringify ($ inputElement , $ member , $ requestPart ),
358
- 'ENUM_CLASS ' => $ enumClassName ->getName (),
359
- 'PROPERTY ' => GeneratorHelper::normalizeName ($ member ->getName ()),
360
- 'NAME ' => $ member ->getName (),
361
- ]);
362
- }
363
-
364
- $ applyHook = '' ;
365
- foreach ($ operation ->getService ()->getHooks ($ requestPart . '_parameters ' ) as $ hook ) {
366
- if (!\in_array ($ member ->getLocationName () ?? $ member ->getName (), $ hook ->getFilters ())) {
367
- continue ;
368
- }
369
-
370
- $ applyHook .= $ this ->hookGenerator ->generate ($ hook , 'VALUE ' );
371
- }
372
- $ applyHook = strtr ($ applyHook , [
373
- 'VALUE ' => $ this ->stringify ($ inputElement , $ member , $ requestPart ),
374
- ]);
375
430
376
- $ bodyCode = strtr ($ bodyCode , [
377
- 'PROPERTY ' => GeneratorHelper::normalizeName ($ member ->getName ()),
378
- 'NAME ' => $ member ->getName (),
431
+ $ bodyCode = strtr (strtr ($ bodyCode , [
432
+ 'GETTER_CODE ' => $ this ->inputClassRequestPartGetters ($ member , $ operation , $ requestPart , $ input , $ output ),
433
+ ]), [
434
+ 'MEMBER_NAME ' => $ member ->getName (),
379
435
'VAR_NAME ' => $ varName ,
436
+ 'PROPERTY ' => GeneratorHelper::normalizeName ($ member ->getName ()),
380
437
'LOCATION ' => $ member ->getLocationName () ?? $ member ->getName (),
381
- 'VALIDATE_ENUM ' => $ validateEnum ,
382
- 'APPLY_HOOK ' => $ applyHook ,
383
- 'VALUE ' => $ this ->stringify ($ inputElement , $ member , $ requestPart ),
384
438
]);
385
- if (!isset ($ body [$ requestPart ])) {
386
- $ body [$ requestPart ] = $ varName . ' = []; ' . "\n" ;
387
- }
388
439
$ body [$ requestPart ] .= $ bodyCode . "\n" ;
389
440
}
390
441
}
0 commit comments