Skip to content

Commit

Permalink
Now converting timestamps to DateTimeResult objects
Browse files Browse the repository at this point in the history
  • Loading branch information
mtdowling committed May 21, 2015
1 parent a7c7095 commit 51c300c
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 6 deletions.
4 changes: 3 additions & 1 deletion build/docs/classes/ExampleBuilder.php
Expand Up @@ -89,7 +89,9 @@ private function getSimpleValue(array $shape)
case 'stream':
return '<Psr\\Http\\Message\\StreamableInterface>';
case 'timestamp':
return '<integer || string || DateTime>';
return $this->isInput
? '<integer || string || DateTime>'
: '<DateTime>';
case 'float':
return '<float>';
default:
Expand Down
41 changes: 41 additions & 0 deletions src/Api/DateTimeResult.php
@@ -0,0 +1,41 @@
<?php
namespace Aws\Api;

/**
* DateTime overrides that make DateTime work more seamlessly as a string,
* with JSON documents, and with JMESPath.
*/
class DateTimeResult extends \DateTime implements \JsonSerializable
{
/**
* Create a new DateTimeResult from a unix timestamp.
*
* @param $unixTimestamp
*
* @return DateTimeResult
*/
public static function fromEpoch($unixTimestamp)
{
return new self(gmdate('c', $unixTimestamp));
}

/**
* Serialize the DateTimeResult as an ISO 8601 date string.
*
* @return string
*/
public function __toString()
{
return $this->format('c');
}

/**
* Serialize the date as an ISO 8601 date when serializing as JSON.
*
* @return mixed|string
*/
public function jsonSerialize()
{
return (string) $this;
}
}
18 changes: 17 additions & 1 deletion src/Api/Parser/AbstractRestParser.php
@@ -1,6 +1,7 @@
<?php
namespace Aws\Api\Parser;

use Aws\Api\DateTimeResult;
use Aws\Api\Shape;
use Aws\Api\StructureShape;
use Aws\Result;
Expand Down Expand Up @@ -87,7 +88,22 @@ private function extractHeader(
ResponseInterface $response,
&$result
) {
$result[$name] = $response->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;
}

/**
Expand Down
7 changes: 7 additions & 0 deletions src/Api/Parser/JsonParser.php
@@ -1,6 +1,7 @@
<?php
namespace Aws\Api\Parser;

use Aws\Api\DateTimeResult;
use Aws\Api\Shape;

/**
Expand Down Expand Up @@ -37,6 +38,12 @@ public function parse(Shape $shape, $value)
}
return $target;

case 'timestamp':
// The Unix epoch (or Unix time or POSIX time or Unix
// timestamp) is the number of seconds that have elapsed since
// January 1, 1970 (midnight UTC/GMT).
return DateTimeResult::fromEpoch($value);

case 'blob':
return base64_decode($value);

Expand Down
9 changes: 8 additions & 1 deletion src/Api/Parser/XmlParser.php
@@ -1,6 +1,7 @@
<?php
namespace Aws\Api\Parser;

use Aws\Api\DateTimeResult;
use Aws\Api\ListShape;
use Aws\Api\MapShape;
use Aws\Api\Shape;
Expand All @@ -26,7 +27,8 @@ private function dispatch($shape, \SimpleXMLElement $value)
'boolean' => 'parse_boolean',
'integer' => 'parse_integer',
'float' => 'parse_float',
'double' => 'parse_float'
'double' => 'parse_float',
'timestamp' => 'parse_timestamp',
];

$type = $shape['type'];
Expand Down Expand Up @@ -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);
}
}
31 changes: 31 additions & 0 deletions tests/Api/DateTimeResultTest.php
@@ -0,0 +1,31 @@
<?php
namespace Aws\Test\Api;

use Aws\Api\DateTimeResult;

/**
* @covers \Aws\Api\DateTimeResult
*/
class DateTimeResultTest extends \PHPUnit_Framework_TestCase
{
public function testCreatesFromEpoch()
{
$t = time();
$d = DateTimeResult::fromEpoch($t);
$this->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));
}
}
5 changes: 2 additions & 3 deletions tests/Api/Parser/ComplianceTest.php
Expand Up @@ -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;
}
}
Expand Down

0 comments on commit 51c300c

Please sign in to comment.