Skip to content

Commit

Permalink
Add support \DateTimeInterface (googleapis#956)
Browse files Browse the repository at this point in the history
* Add support \DateTimeInterface

* Update PhpUnit and remove unnecessary overhead

* Update refactor

* Update php5.5 compatibility

* Change intval to cast

* Cast expression optimization
  • Loading branch information
DiogoRMax authored and dwsupplee committed Mar 29, 2018
1 parent 354fdfd commit cf58b76
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ composer.phar
composer.lock
docs/json/**/*
vendor/
.idea/

# do not delete
keys/
Expand Down
17 changes: 13 additions & 4 deletions Firestore/src/ValueMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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];
}
Expand Down
17 changes: 13 additions & 4 deletions Firestore/tests/Unit/ValueMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit cf58b76

Please sign in to comment.