Skip to content

Commit

Permalink
Add support for timestamp in XmlSerializer (#1804)
Browse files Browse the repository at this point in the history
  • Loading branch information
jderusse authored Nov 22, 2024
1 parent cc166c5 commit acb42d0
Showing 1 changed file with 34 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,8 @@ private function dumpXmlShape(Member $member, Shape $shape, string $output, stri
return $this->dumpXmlShapeNumeric($member, $output, $input);
case 'boolean':
return $this->dumpXmlShapeBoolean($member, $output, $input);
case 'timestamp':
return $this->dumpXmlShapeTimestamp($member, $output, $input);
}

throw new \RuntimeException(\sprintf('Type %s is not yet implemented', $shape->getType()));
Expand Down Expand Up @@ -303,4 +305,36 @@ private function dumpXmlShapeBoolean(Member $member, string $output, string $inp
'NODE_NAME' => var_export($member->getLocationName() ?? ($member instanceof StructureMember ? $member->getName() : 'member'), true),
]);
}

private function dumpXmlShapeTimestamp(Member $member, string $output, string $input): string
{
$format = $member->getShape()->get('timestampFormat') ?? 'iso8601';
switch ($format) {
case 'iso8601':
$format = 'ATOM';

break;
case 'rfc822':
$format = 'RFC822';

break;
default:
throw new \RuntimeException(\sprintf('Timestamp format %s is not yet implemented', $format));
}

if ($member instanceof StructureMember && $member->isXmlAttribute()) {
$body = 'OUTPUT->setAttribute(NODE_NAME, INPUT->format(\DateTimeInterface::FORMAT));';
} else {
$body = 'OUTPUT->appendChild($document->createElement(NODE_NAME, INPUT->format(\DateTimeInterface::FORMAT)));';
}

$replacements = [
'INPUT' => $input,
'OUTPUT' => $output,
'FORMAT' => $format,
'NODE_NAME' => var_export($member->getLocationName() ?? ($member instanceof StructureMember ? $member->getName() : 'member'), true),
];

return strtr($body, $replacements);
}
}

0 comments on commit acb42d0

Please sign in to comment.