From cf58b761245f86a935551c6e5679e88719fc7a3b Mon Sep 17 00:00:00 2001 From: Diogo Rosa de Matos Date: Thu, 29 Mar 2018 15:53:40 -0300 Subject: [PATCH] Add support \DateTimeInterface (#956) * Add support \DateTimeInterface * Update PhpUnit and remove unnecessary overhead * Update refactor * Update php5.5 compatibility * Change intval to cast * Cast expression optimization --- .gitignore | 1 + Firestore/src/ValueMapper.php | 17 +++++++++++++---- Firestore/tests/Unit/ValueMapperTest.php | 17 +++++++++++++---- 3 files changed, 27 insertions(+), 8 deletions(-) diff --git a/.gitignore b/.gitignore index c65362505885..5b1e4e62d1c2 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,7 @@ composer.phar composer.lock docs/json/**/* vendor/ +.idea/ # do not delete keys/ diff --git a/Firestore/src/ValueMapper.php b/Firestore/src/ValueMapper.php index 8477f0680f26..2a1be72af0e1 100644 --- a/Firestore/src/ValueMapper.php +++ b/Firestore/src/ValueMapper.php @@ -17,16 +17,16 @@ namespace Google\Cloud\Firestore; +use Google\Cloud\Core\ArrayTrait; use Google\Cloud\Core\Blob; -use Google\Cloud\Core\Int64; -use Google\Protobuf\NullValue; +use Google\Cloud\Core\DebugInfoTrait; use Google\Cloud\Core\GeoPoint; +use Google\Cloud\Core\Int64; use Google\Cloud\Core\Timestamp; -use Google\Cloud\Core\ArrayTrait; use Google\Cloud\Core\ValidateTrait; -use Google\Cloud\Core\DebugInfoTrait; use Google\Cloud\Core\ValueMapperTrait; use Google\Cloud\Firestore\Connection\ConnectionInterface; +use Google\Protobuf\NullValue; /** * Normalizes values between Google Cloud PHP and Cloud Firestore. @@ -402,6 +402,15 @@ private function encodeObjectValue($value) return $this->encodeAssociativeArrayValue((array) $value); } + if ($value instanceof \DateTimeInterface) { + return [ + 'timestampValue' => [ + 'seconds' => $value->format('U'), + 'nanos' => (int)($value->format('u') * 1000) + ] + ]; + } + if ($value instanceof Blob) { return ['bytesValue' => (string) $value]; } diff --git a/Firestore/tests/Unit/ValueMapperTest.php b/Firestore/tests/Unit/ValueMapperTest.php index 34ae00944ab1..68de46b96863 100644 --- a/Firestore/tests/Unit/ValueMapperTest.php +++ b/Firestore/tests/Unit/ValueMapperTest.php @@ -26,7 +26,6 @@ use Google\Cloud\Firestore\DocumentReference; use Google\Cloud\Firestore\FieldPath; use Google\Cloud\Firestore\FieldValue; -use Google\Cloud\Firestore\FirestoreClient; use Google\Cloud\Firestore\ValueMapper; use Google\Protobuf\NullValue; use PHPUnit\Framework\TestCase; @@ -192,9 +191,11 @@ public function decodedValues() $blobValue = 'hello world'; $blob = new Blob($blobValue); - $now = (string) time(); - $nanos = 10; - $timestamp = new Timestamp(\DateTimeImmutable::createFromFormat('U', (string) $now), $nanos); + $datetime = \DateTimeImmutable::createFromFormat('U.u', microtime(true)); + $now = (string) $datetime->format('U'); + $micros = (int) $datetime->format('u'); + $nanos = (int) $datetime->format('u') * 1000 + 10; + $timestamp = new Timestamp(\DateTimeImmutable::createFromFormat('U', $now), $nanos); $lat = 100.01; $lng = 100.25; @@ -298,6 +299,14 @@ function ($val) { function ($val) use ($blobValue) { $this->assertEquals($blobValue, $val['bytesValue']); } + ], [ + $datetime, + function ($val) use ($now, $micros) { + $this->assertEquals([ + 'seconds' => $now, + 'nanos' => intval($micros * 1000) + ], $val['timestampValue']); + } ], [ $timestamp, function ($val) use ($now, $nanos) {