From 51c300c4d5a66d72ae021b39cd8bbae943cb471a Mon Sep 17 00:00:00 2001 From: Michael Dowling Date: Thu, 21 May 2015 14:58:49 -0700 Subject: [PATCH] Now converting timestamps to DateTimeResult objects --- build/docs/classes/ExampleBuilder.php | 4 ++- src/Api/DateTimeResult.php | 41 +++++++++++++++++++++++++++ src/Api/Parser/AbstractRestParser.php | 18 +++++++++++- src/Api/Parser/JsonParser.php | 7 +++++ src/Api/Parser/XmlParser.php | 9 +++++- tests/Api/DateTimeResultTest.php | 31 ++++++++++++++++++++ tests/Api/Parser/ComplianceTest.php | 5 ++-- 7 files changed, 109 insertions(+), 6 deletions(-) create mode 100644 src/Api/DateTimeResult.php create mode 100644 tests/Api/DateTimeResultTest.php diff --git a/build/docs/classes/ExampleBuilder.php b/build/docs/classes/ExampleBuilder.php index 951289ff50..a06723ce37 100644 --- a/build/docs/classes/ExampleBuilder.php +++ b/build/docs/classes/ExampleBuilder.php @@ -89,7 +89,9 @@ private function getSimpleValue(array $shape) case 'stream': return ''; case 'timestamp': - return ''; + return $this->isInput + ? '' + : ''; case 'float': return ''; default: diff --git a/src/Api/DateTimeResult.php b/src/Api/DateTimeResult.php new file mode 100644 index 0000000000..aa3cf7e63e --- /dev/null +++ b/src/Api/DateTimeResult.php @@ -0,0 +1,41 @@ +format('c'); + } + + /** + * Serialize the date as an ISO 8601 date when serializing as JSON. + * + * @return mixed|string + */ + public function jsonSerialize() + { + return (string) $this; + } +} diff --git a/src/Api/Parser/AbstractRestParser.php b/src/Api/Parser/AbstractRestParser.php index 0288a89fc3..a6ee533e97 100644 --- a/src/Api/Parser/AbstractRestParser.php +++ b/src/Api/Parser/AbstractRestParser.php @@ -1,6 +1,7 @@ getHeaderLine($shape['locationName'] ?: $name); + $value = $response->getHeaderLine($shape['locationName'] ?: $name); + $type = $shape->getType(); + + if ($type === 'blob') { + $value = base64_decode($value); + } elseif ($type === 'timestamp') { + try { + $value = new DateTimeResult($value); + } catch (\Exception $e) { + // If the value cannot be parsed, then do not add it to the + // output structure. + return; + } + } + + $result[$name] = $value; } /** diff --git a/src/Api/Parser/JsonParser.php b/src/Api/Parser/JsonParser.php index 426c2b04f1..d61f8af608 100644 --- a/src/Api/Parser/JsonParser.php +++ b/src/Api/Parser/JsonParser.php @@ -1,6 +1,7 @@ 'parse_boolean', 'integer' => 'parse_integer', 'float' => 'parse_float', - 'double' => 'parse_float' + 'double' => 'parse_float', + 'timestamp' => 'parse_timestamp', ]; $type = $shape['type']; @@ -120,4 +122,9 @@ private function parse_boolean(Shape $shape, $value) { return $value == 'true' ? true : false; } + + private function parse_timestamp(Shape $shape, $value) + { + return new DateTimeResult($value); + } } diff --git a/tests/Api/DateTimeResultTest.php b/tests/Api/DateTimeResultTest.php new file mode 100644 index 0000000000..cfb86c5aa0 --- /dev/null +++ b/tests/Api/DateTimeResultTest.php @@ -0,0 +1,31 @@ +assertEquals($t, $d->format('U')); + } + + public function testCastToIso8601String() + { + $t = time(); + $d = DateTimeResult::fromEpoch($t); + $this->assertSame(gmdate('c', $t), (string) $d); + } + + public function testJsonSerialzesAsIso8601() + { + $t = time(); + $d = DateTimeResult::fromEpoch($t); + $this->assertSame('"' . gmdate('c', $t). '"', json_encode($d)); + } +} diff --git a/tests/Api/Parser/ComplianceTest.php b/tests/Api/Parser/ComplianceTest.php index adc3721908..2bd3e03f20 100644 --- a/tests/Api/Parser/ComplianceTest.php +++ b/tests/Api/Parser/ComplianceTest.php @@ -99,9 +99,8 @@ private function fixTimestamps(&$data, Shape $shape) } break; case 'Aws\Api\TimestampShape': - if (is_string($data)) { - $data = strtotime($data); - } + // Format the DateTimeResult as a Unix timestamp. + $data = $data->format('U'); break; } }