Created
June 29, 2018 00:33
-
-
Save ikonst/681706b2a37de23b2ee3f547b364b2b5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use DateTime; | |
use DrSlump\Protobuf; | |
use google\protobuf\Timestamp; | |
class PhpArrayModernCodec extends \DrSlump\Protobuf\Codec\PhpArray { | |
protected function encodeMessage(Protobuf\Message $message) | |
{ | |
if ($message instanceof Timestamp) { | |
$dt = ProtoHelper::PbTimeStampToDateTime($message); | |
if ($message->getNanos()) { | |
$nanosStr = str_pad((string)$message->getNanos(), 9, '0', STR_PAD_LEFT); | |
return $dt->format('Y-m-d\TH:i:s') . ".{$nanosStr}Z"; | |
} else { | |
return $dt->format('Y-m-d\TH:i:s') . 'Z'; | |
} | |
} | |
return parent::encodeMessage($message); | |
} | |
protected function decodeMessage(Protobuf\Message $message, $data) | |
{ | |
if ($message instanceof Timestamp) { | |
/** @var string $data */ | |
if (preg_match('/^(.+)\.(\d+)Z$/', $data, $matches)) { | |
$dt = DateTime::createFromFormat('Y-m-d\TH:i:s', $matches[1]); | |
if ($dt) { | |
$message->setSeconds($dt->format('U')); | |
$message->setNanos((int)($matches[2])); | |
} else { | |
$message->setSeconds(0); | |
$message->setNanos(0); | |
} | |
return $message; | |
} elseif (preg_match('/^(.*)Z$/', $data, $matches)) { | |
$dt = DateTime::createFromFormat('Y-m-d\TH:i:s', $matches[1]); | |
$message->setSeconds($dt->format('U')); | |
return $message; | |
} else { | |
return $message; | |
} | |
} | |
return parent::decodeMessage($message, $data); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use google\protobuf\Timestamp; | |
class PhpArrayModernCodecTest extends PHPUnit_Framework_TestCase | |
{ | |
public function test_encodeEmptyTimestamp() | |
{ | |
$codec = new PhpArrayModernCodec(); | |
$ts = new Timestamp(); | |
$s = $ts->serialize($codec); | |
$this->assertEquals('1970-01-01T00:00:00Z', $s); | |
} | |
public function test_encodeTimestampWithoutNano() | |
{ | |
$codec = new PhpArrayModernCodec(); | |
$ts = new Timestamp(); | |
$ts->setSeconds(1514764800); | |
$s = $ts->serialize($codec); | |
$this->assertEquals('2018-01-01T00:00:00Z', $s); | |
} | |
public function test_encodeTimestampWithNano() | |
{ | |
$codec = new PhpArrayModernCodec(); | |
$ts = new Timestamp(); | |
$ts->setSeconds(1514764800); | |
$ts->setNanos(123); | |
$s = $ts->serialize($codec); | |
$this->assertEquals('2018-01-01T00:00:00.000000123Z', $s); | |
} | |
public function test_decodeTimestampWithoutNano() | |
{ | |
$codec = new PhpArrayModernCodec(); | |
$ts = Timestamp::deserialize('2018-01-01T00:00:00Z', $codec); | |
$this->assertEquals(1514764800, $ts->getSeconds()); | |
$this->assertEquals(0, $ts->getNanos()); | |
} | |
public function test_decodeTimestampWithNano() | |
{ | |
$codec = new PhpArrayModernCodec(); | |
$ts = Timestamp::deserialize('2018-01-01T00:00:00.000000123Z', $codec); | |
$this->assertEquals(1514764800, $ts->getSeconds()); | |
$this->assertEquals(123, $ts->getNanos()); | |
} | |
public function test_decodeTimestampError() | |
{ | |
$codec = new PhpArrayModernCodec(); | |
$ts = Timestamp::deserialize('', $codec); | |
$this->assertEquals(0, $ts->getSeconds()); | |
$this->assertEquals(0, $ts->getNanos()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment