Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/CodeGenerator/src/Generator/PaginationGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,9 @@ private function generateOutputPaginationLoader(string $iterator, string $common
if (!$moreResult) {
$moreCondition = '';
foreach ($outputToken as $property) {
$moreCondition .= $this->generateGetter('$page', $property, (bool) $common);
$moreCondition .= 'null !== ' . $this->generateGetter('$page', $property, (bool) $common);

break;
}
} else {
$moreCondition = $this->generateGetter('$page', $moreResult, (bool) $common);
Expand Down
177 changes: 100 additions & 77 deletions src/CodeGenerator/src/Generator/ResponseParser/RestXmlParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ class RestXmlParser implements Parser
*/
private $imports = [];

/**
* @var array<string, true>
*/
private $generatedFunctions = [];

public function __construct(NamespaceRegistry $namespaceRegistry, RequirementsRegistry $requirementsRegistry, TypeGenerator $typeGenerator)
{
$this->namespaceRegistry = $namespaceRegistry;
Expand All @@ -61,6 +66,7 @@ public function generate(StructureShape $shape, bool $throwOnError = true): Pars
{
$properties = [];
$this->functions = [];
$this->generatedFunctions = [];
$this->imports = [];
if (null !== $payload = $shape->getPayload()) {
$member = $shape->getMember($payload);
Expand All @@ -80,19 +86,10 @@ public function generate(StructureShape $shape, bool $throwOnError = true): Pars
continue;
}

if (!$member->isNullable() && !$member->isRequired()) {
$properties[] = strtr('if (null !== $v = (PROPERTY_ACCESSOR)) {
$this->PROPERTY_NAME = $v;
}', [
'PROPERTY_NAME' => GeneratorHelper::normalizeName($member->getName()),
'PROPERTY_ACCESSOR' => $this->parseXmlElement($this->getInputAccessor('$data', $member), $member->getShape(), $member->isRequired(), false),
]);
} else {
$properties[] = strtr('$this->PROPERTY_NAME = PROPERTY_ACCESSOR;', [
'PROPERTY_NAME' => GeneratorHelper::normalizeName($member->getName()),
'PROPERTY_ACCESSOR' => $this->parseXmlElement($this->getInputAccessor('$data', $member), $member->getShape(), $member->isRequired(), false),
]);
}
$properties[] = strtr('$this->PROPERTY_NAME = PROPERTY_ACCESSOR;', [
'PROPERTY_NAME' => GeneratorHelper::normalizeName($member->getName()),
'PROPERTY_ACCESSOR' => $this->parseXmlElement($this->getInputAccessor('$data', $member), $member->getShape(), $member->isRequired(), false),
]);
}
}

Expand Down Expand Up @@ -204,20 +201,35 @@ private function parseXmlElement(string $input, Shape $shape, bool $required, bo

private function parseXmlResponseStructure(StructureShape $shape, string $input, bool $required): string
{
$properties = [];
foreach ($shape->getMembers() as $member) {
$properties[] = strtr('PROPERTY_NAME => PROPERTY_ACCESSOR,', [
'PROPERTY_NAME' => var_export($member->getName(), true),
'PROPERTY_ACCESSOR' => $this->parseXmlElement($this->getInputAccessor($input, $member), $member->getShape(), $member->isRequired(), true),
]);
$functionName = 'populateResult' . ucfirst($shape->getName());
if (!isset($this->generatedFunctions[$functionName])) {
// prevent recursion
$this->generatedFunctions[$functionName] = true;

$properties = [];
foreach ($shape->getMembers() as $member) {
$properties[] = strtr('PROPERTY_NAME => PROPERTY_ACCESSOR,', [
'PROPERTY_NAME' => var_export($member->getName(), true),
'PROPERTY_ACCESSOR' => $this->parseXmlElement($this->getInputAccessor('$xml', $member), $member->getShape(), $member->isRequired(), true),
]);
}

$body = 'return new CLASS_NAME([
PROPERTIES
]);';

$className = $this->namespaceRegistry->getObject($shape);
$this->imports[] = $className;

$this->functions[$functionName] = $this->createPopulateMethod($functionName, strtr($body, [
'CLASS_NAME' => $className->getName(),
'PROPERTIES' => implode("\n", $properties),
]), $shape);
}

return strtr('REQUIRED new CLASS_NAME([
PROPERTIES
])', [
'REQUIRED' => $required ? '' : '!' . $input . ' ? null : ',
'CLASS_NAME' => $this->namespaceRegistry->getObject($shape)->getName(),
'PROPERTIES' => implode("\n", $properties),
return strtr($required ? '$this->FUNCTION_NAME(INPUT)' : '0 === INPUT->count() ? null : $this->FUNCTION_NAME(INPUT)', [
'INPUT' => $input,
'FUNCTION_NAME' => $functionName,
]);
}

Expand All @@ -227,7 +239,7 @@ private function parseXmlResponseString(string $input, bool $required): string
return strtr('(string) INPUT', ['INPUT' => $input]);
}

return strtr('($v = INPUT) ? (string) $v : null', ['INPUT' => $input]);
return strtr('(null !== $v = INPUT[0]) ? (string) $v : null', ['INPUT' => $input]);
}

private function parseXmlResponseInteger(string $input, bool $required): string
Expand All @@ -236,7 +248,7 @@ private function parseXmlResponseInteger(string $input, bool $required): string
return strtr('(int) (string) INPUT', ['INPUT' => $input]);
}

return strtr('($v = INPUT) ? (int) (string) $v : null', ['INPUT' => $input]);
return strtr('(null !== $v = INPUT[0]) ? (int) (string) $v : null', ['INPUT' => $input]);
}

private function parseXmlResponseFloat(string $input, bool $required): string
Expand All @@ -245,7 +257,7 @@ private function parseXmlResponseFloat(string $input, bool $required): string
return strtr('(float) (string) INPUT', ['INPUT' => $input]);
}

return strtr('($v = INPUT) ? (float) (string) $v : null', ['INPUT' => $input]);
return strtr('(null !== $v = INPUT[0]) ? (float) (string) $v : null', ['INPUT' => $input]);
}

private function parseXmlResponseBool(string $input, bool $required): string
Expand All @@ -256,7 +268,7 @@ private function parseXmlResponseBool(string $input, bool $required): string
return strtr('filter_var((string) INPUT, FILTER_VALIDATE_BOOLEAN)', ['INPUT' => $input]);
}

return strtr('($v = INPUT) ? filter_var((string) $v, FILTER_VALIDATE_BOOLEAN) : null', ['INPUT' => $input]);
return strtr('(null !== $v = INPUT[0]) ? filter_var((string) $v, FILTER_VALIDATE_BOOLEAN) : null', ['INPUT' => $input]);
}

private function parseXmlResponseBlob(string $input, bool $required): string
Expand All @@ -265,7 +277,7 @@ private function parseXmlResponseBlob(string $input, bool $required): string
return strtr('base64_decode((string) INPUT)', ['INPUT' => $input]);
}

return strtr('($v = INPUT) ? base64_decode((string) $v) : null', ['INPUT' => $input]);
return strtr('(null !== $v = INPUT[0]) ? base64_decode((string) $v) : null', ['INPUT' => $input]);
}

private function parseXmlResponseTimestamp(Shape $shape, string $input, bool $required): string
Expand All @@ -281,47 +293,52 @@ private function parseXmlResponseTimestamp(Shape $shape, string $input, bool $re
}

if (!$required) {
$body = '($v = INPUT) ? ' . strtr($body, ['INPUT' => '$v']) . ' : null';
$body = '(null !== $v = INPUT[0]) ? ' . strtr($body, ['INPUT' => '$v']) . ' : null';
}

return strtr($body, ['INPUT' => $input]);
}

private function parseXmlResponseList(ListShape $shape, string $input, bool $required, bool $inObject): string
{
$shapeMember = $shape->getMember();
if ($shapeMember->getShape() instanceof StructureShape) {
$listAccessorRequired = true;
$body = '
$items = [];
foreach (INPUT_PROPERTY as $item) {
$items[] = LIST_ACCESSOR;
}
$functionName = 'populateResult' . ucfirst($shape->getName());
if (!isset($this->generatedFunctions[$functionName])) {
// prevent recursion
$this->generatedFunctions[$functionName] = true;

$shapeMember = $shape->getMember();
if ($shapeMember->getShape() instanceof ListShape || $shapeMember->getShape() instanceof MapShape) {
$listAccessorRequired = false;
$body = '
$items = [];
foreach (INPUT_PROPERTY as $item) {
$a = LIST_ACCESSOR;
if (null !== $a) {
$items[] = $a;
}
}

return $items;
';
} else {
$listAccessorRequired = false;
$body = '
$items = [];
foreach (INPUT_PROPERTY as $item) {
$a = LIST_ACCESSOR;
if (null !== $a) {
$items[] = $a;
return $items;
';
} else {
$listAccessorRequired = true;
$body = '
$items = [];
foreach (INPUT_PROPERTY as $item) {
$items[] = LIST_ACCESSOR;
}
}

return $items;
';
}
return $items;
';
}

$functionName = 'populateResult' . ucfirst($shape->getName());
$this->functions[$functionName] = $this->createPopulateMethod($functionName, strtr($body, [
'LIST_ACCESSOR' => $this->parseXmlElement('$item', $shapeMember->getShape(), $listAccessorRequired, $inObject),
'INPUT_PROPERTY' => $shape->isFlattened() ? '$xml' : ('$xml->' . ($shapeMember->getLocationName() ? $shapeMember->getLocationName() : 'member')),
]), $shape);
$this->functions[$functionName] = $this->createPopulateMethod($functionName, strtr($body, [
'LIST_ACCESSOR' => $this->parseXmlElement('$item', $shapeMember->getShape(), $listAccessorRequired, $inObject),
'INPUT_PROPERTY' => $shape->isFlattened() ? '$xml' : ('$xml->' . ($shapeMember->getLocationName() ? $shapeMember->getLocationName() : 'member')),
]), $shape);
}

return strtr($required ? '$this->FUNCTION_NAME(INPUT)' : '!INPUT ? EMPTY : $this->FUNCTION_NAME(INPUT)', [
return strtr($required ? '$this->FUNCTION_NAME(INPUT)' : '(0 === ($v = INPUT)->count()) ? EMPTY : $this->FUNCTION_NAME($v)', [
'EMPTY' => !$inObject ? '[]' : 'null',
'INPUT' => $input,
'FUNCTION_NAME' => $functionName,
Expand All @@ -330,26 +347,31 @@ private function parseXmlResponseList(ListShape $shape, string $input, bool $req

private function parseXmlResponseMap(MapShape $shape, string $input, bool $required, bool $inObject): string
{
$shapeValue = $shape->getValue();
$body = '
$items = [];
foreach (INPUT as $item) {
$a = $item->MAP_VALUE;
$items[$item->MAP_KEY->__toString()] = MAP_ACCESSOR;
}
$functionName = 'populateResult' . ucfirst($shape->getName());
if (!isset($this->generatedFunctions[$functionName])) {
// prevent recursion
$this->generatedFunctions[$functionName] = true;

return $items;
';
$shapeValue = $shape->getValue();
$body = '
$items = [];
foreach (INPUT as $item) {
$a = $item->MAP_VALUE;
$items[$item->MAP_KEY->__toString()] = MAP_ACCESSOR;
}

$functionName = 'populateResult' . ucfirst($shape->getName());
$this->functions[$functionName] = $this->createPopulateMethod($functionName, strtr($body, [
'INPUT' => $shape->isFlattened() ? '$xml' : '$xml->entry',
'MAP_KEY' => $shape->getKey()->getLocationName() ?? 'key',
'MAP_VALUE' => $shape->getValue()->getLocationName() ?? 'value',
'MAP_ACCESSOR' => $this->parseXmlElement('$a', $shapeValue->getShape(), true, $inObject),
]), $shape);

return strtr($required ? '$this->FUNCTION_NAME(INPUT)' : '!INPUT ? EMPTY : $this->FUNCTION_NAME(INPUT)', [
return $items;
';

$this->functions[$functionName] = $this->createPopulateMethod($functionName, strtr($body, [
'INPUT' => $shape->isFlattened() ? '$xml' : '$xml->entry',
'MAP_KEY' => $shape->getKey()->getLocationName() ?? 'key',
'MAP_VALUE' => $shape->getValue()->getLocationName() ?? 'value',
'MAP_ACCESSOR' => $this->parseXmlElement('$a', $shapeValue->getShape(), true, $inObject),
]), $shape);
}

return strtr($required ? '$this->FUNCTION_NAME(INPUT)' : '(0 === ($v = INPUT)->count()) ? EMPTY : $this->FUNCTION_NAME($v)', [
'EMPTY' => !$inObject ? '[]' : 'null',
'INPUT' => $input,
'FUNCTION_NAME' => $functionName,
Expand All @@ -368,6 +390,7 @@ private function createPopulateMethod(string $functionName, string $body, Shape

[$returnType, $parameterType, $memberClassNames] = $this->typeGenerator->getPhpType($shape);
$method
->setReturnType($returnType)
->setComment('@return ' . $parameterType);
$this->imports = array_merge($this->imports, $memberClassNames);

Expand Down
1 change: 1 addition & 0 deletions src/Core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

### Changed

- use strict comparison `null !==` instead of `!`
- Fix CS

## 1.22.1
Expand Down
2 changes: 1 addition & 1 deletion src/Core/src/Signer/SignerV4.php
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ private function buildCanonicalQuery(Request $request): string
$query = $request->getQuery();

unset($query['X-Amz-Signature']);
if (!$query) {
if (empty($query)) {
return '';
}

Expand Down
30 changes: 20 additions & 10 deletions src/Core/src/Sts/Result/AssumeRoleResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,17 +94,27 @@ protected function populateResult(Response $response): void
$data = new \SimpleXMLElement($response->getContent());
$data = $data->AssumeRoleResult;

$this->credentials = !$data->Credentials ? null : new Credentials([
'AccessKeyId' => (string) $data->Credentials->AccessKeyId,
'SecretAccessKey' => (string) $data->Credentials->SecretAccessKey,
'SessionToken' => (string) $data->Credentials->SessionToken,
'Expiration' => new \DateTimeImmutable((string) $data->Credentials->Expiration),
$this->credentials = 0 === $data->Credentials->count() ? null : $this->populateResultCredentials($data->Credentials);
$this->assumedRoleUser = 0 === $data->AssumedRoleUser->count() ? null : $this->populateResultAssumedRoleUser($data->AssumedRoleUser);
$this->packedPolicySize = (null !== $v = $data->PackedPolicySize[0]) ? (int) (string) $v : null;
$this->sourceIdentity = (null !== $v = $data->SourceIdentity[0]) ? (string) $v : null;
}

private function populateResultAssumedRoleUser(\SimpleXMLElement $xml): AssumedRoleUser
{
return new AssumedRoleUser([
'AssumedRoleId' => (string) $xml->AssumedRoleId,
'Arn' => (string) $xml->Arn,
]);
$this->assumedRoleUser = !$data->AssumedRoleUser ? null : new AssumedRoleUser([
'AssumedRoleId' => (string) $data->AssumedRoleUser->AssumedRoleId,
'Arn' => (string) $data->AssumedRoleUser->Arn,
}

private function populateResultCredentials(\SimpleXMLElement $xml): Credentials
{
return new Credentials([
'AccessKeyId' => (string) $xml->AccessKeyId,
'SecretAccessKey' => (string) $xml->SecretAccessKey,
'SessionToken' => (string) $xml->SessionToken,
'Expiration' => new \DateTimeImmutable((string) $xml->Expiration),
]);
$this->packedPolicySize = ($v = $data->PackedPolicySize) ? (int) (string) $v : null;
$this->sourceIdentity = ($v = $data->SourceIdentity) ? (string) $v : null;
}
}
36 changes: 23 additions & 13 deletions src/Core/src/Sts/Result/AssumeRoleWithWebIdentityResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,20 +147,30 @@ protected function populateResult(Response $response): void
$data = new \SimpleXMLElement($response->getContent());
$data = $data->AssumeRoleWithWebIdentityResult;

$this->credentials = !$data->Credentials ? null : new Credentials([
'AccessKeyId' => (string) $data->Credentials->AccessKeyId,
'SecretAccessKey' => (string) $data->Credentials->SecretAccessKey,
'SessionToken' => (string) $data->Credentials->SessionToken,
'Expiration' => new \DateTimeImmutable((string) $data->Credentials->Expiration),
$this->credentials = 0 === $data->Credentials->count() ? null : $this->populateResultCredentials($data->Credentials);
$this->subjectFromWebIdentityToken = (null !== $v = $data->SubjectFromWebIdentityToken[0]) ? (string) $v : null;
$this->assumedRoleUser = 0 === $data->AssumedRoleUser->count() ? null : $this->populateResultAssumedRoleUser($data->AssumedRoleUser);
$this->packedPolicySize = (null !== $v = $data->PackedPolicySize[0]) ? (int) (string) $v : null;
$this->provider = (null !== $v = $data->Provider[0]) ? (string) $v : null;
$this->audience = (null !== $v = $data->Audience[0]) ? (string) $v : null;
$this->sourceIdentity = (null !== $v = $data->SourceIdentity[0]) ? (string) $v : null;
}

private function populateResultAssumedRoleUser(\SimpleXMLElement $xml): AssumedRoleUser
{
return new AssumedRoleUser([
'AssumedRoleId' => (string) $xml->AssumedRoleId,
'Arn' => (string) $xml->Arn,
]);
$this->subjectFromWebIdentityToken = ($v = $data->SubjectFromWebIdentityToken) ? (string) $v : null;
$this->assumedRoleUser = !$data->AssumedRoleUser ? null : new AssumedRoleUser([
'AssumedRoleId' => (string) $data->AssumedRoleUser->AssumedRoleId,
'Arn' => (string) $data->AssumedRoleUser->Arn,
}

private function populateResultCredentials(\SimpleXMLElement $xml): Credentials
{
return new Credentials([
'AccessKeyId' => (string) $xml->AccessKeyId,
'SecretAccessKey' => (string) $xml->SecretAccessKey,
'SessionToken' => (string) $xml->SessionToken,
'Expiration' => new \DateTimeImmutable((string) $xml->Expiration),
]);
$this->packedPolicySize = ($v = $data->PackedPolicySize) ? (int) (string) $v : null;
$this->provider = ($v = $data->Provider) ? (string) $v : null;
$this->audience = ($v = $data->Audience) ? (string) $v : null;
$this->sourceIdentity = ($v = $data->SourceIdentity) ? (string) $v : null;
}
}
6 changes: 3 additions & 3 deletions src/Core/src/Sts/Result/GetCallerIdentityResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ protected function populateResult(Response $response): void
$data = new \SimpleXMLElement($response->getContent());
$data = $data->GetCallerIdentityResult;

$this->userId = ($v = $data->UserId) ? (string) $v : null;
$this->account = ($v = $data->Account) ? (string) $v : null;
$this->arn = ($v = $data->Arn) ? (string) $v : null;
$this->userId = (null !== $v = $data->UserId[0]) ? (string) $v : null;
$this->account = (null !== $v = $data->Account[0]) ? (string) $v : null;
$this->arn = (null !== $v = $data->Arn[0]) ? (string) $v : null;
}
}
Loading