aboutsummaryrefslogtreecommitdiff
path: root/php
diff options
context:
space:
mode:
authorLeonard Hecker <leonard@hecker.io>2019-06-18 19:31:50 +0200
committerPaul Yang <TeBoring@users.noreply.github.com>2019-06-18 10:31:50 -0700
commit43307d4da09e1a27b132cd3a2c29824efaafef25 (patch)
tree5a567941439489a3724fd6d6675b078b5751f68a /php
parentf2a919f58f12574ac04ca79e6b84577dec6f2b43 (diff)
downloadprotobuf-43307d4da09e1a27b132cd3a2c29824efaafef25.tar.gz
php: Fix formatting of Duration (#6155)
* php: Fixed php notices for unknown enum indices * php: Fixed formatting of Duration This fixes: * Missing nanoseconds. The nanoseconds where divided as a float and implicitly converted to a string before being passed to bcadd. This can result in a float formatted in scientific/exponential notation, which bcmath doesn't understand. * Durations are supposed to be formatted without trailing zeroes.
Diffstat (limited to 'php')
-rw-r--r--php/src/Google/Protobuf/Internal/EnumDescriptor.php15
-rw-r--r--php/src/Google/Protobuf/Internal/GPBUtil.php26
2 files changed, 31 insertions, 10 deletions
diff --git a/php/src/Google/Protobuf/Internal/EnumDescriptor.php b/php/src/Google/Protobuf/Internal/EnumDescriptor.php
index 82a427670..7af4f8401 100644
--- a/php/src/Google/Protobuf/Internal/EnumDescriptor.php
+++ b/php/src/Google/Protobuf/Internal/EnumDescriptor.php
@@ -39,17 +39,26 @@ class EnumDescriptor
public function getValueByNumber($number)
{
- return $this->value[$number];
+ if (isset($this->value[$number])) {
+ return $this->value[$number];
+ }
+ return null;
}
public function getValueByName($name)
{
- return $this->name_to_value[$name];
+ if (isset($this->name_to_value[$name])) {
+ return $this->name_to_value[$name];
+ }
+ return null;
}
public function getValueDescriptorByIndex($index)
{
- return $this->value_descriptor[$index];
+ if (isset($this->value_descriptor[$index])) {
+ return $this->value_descriptor[$index];
+ }
+ return null;
}
public function getValueCount()
diff --git a/php/src/Google/Protobuf/Internal/GPBUtil.php b/php/src/Google/Protobuf/Internal/GPBUtil.php
index 0beedba33..7ec3ca229 100644
--- a/php/src/Google/Protobuf/Internal/GPBUtil.php
+++ b/php/src/Google/Protobuf/Internal/GPBUtil.php
@@ -504,17 +504,29 @@ class GPBUtil
public static function formatDuration($value)
{
- if (bccomp($value->getSeconds(), "315576000001") != -1) {
- throw new GPBDecodeException("Duration number too large.");
+ if (bccomp($value->getSeconds(), '315576000001') != -1) {
+ throw new GPBDecodeException('Duration number too large.');
}
- if (bccomp($value->getSeconds(), "-315576000001") != 1) {
- throw new GPBDecodeException("Duration number too small.");
+ if (bccomp($value->getSeconds(), '-315576000001') != 1) {
+ throw new GPBDecodeException('Duration number too small.');
+ }
+
+ $nanos = $value->getNanos();
+ if ($nanos === 0) {
+ return (string) $value->getSeconds();
}
- return strval(bcadd($value->getSeconds(),
- $value->getNanos() / 1000000000.0, 9));
- }
+ if ($nanos % 1000000 === 0) {
+ $digits = 3;
+ } elseif ($nanos % 1000 === 0) {
+ $digits = 6;
+ } else {
+ $digits = 9;
+ }
+ $nanos = bcdiv($nanos, '1000000000', $digits);
+ return bcadd($value->getSeconds(), $nanos, $digits);
+ }
public static function parseFieldMask($paths_string)
{