From ecca6ea95d56a6f70ff7b223ec3f904758acc8b1 Mon Sep 17 00:00:00 2001 From: Paul Yang Date: Fri, 30 Jun 2017 12:14:09 -0700 Subject: Add json encode/decode for php. (#3226) * Add json encode/decode for php. * Fix php conformance test on 32-bit machines. * Fix conformance test for c extension. * Fix comments --- php/src/Google/Protobuf/Internal/GPBUtil.php | 70 ++++++++++++++-------------- 1 file changed, 35 insertions(+), 35 deletions(-) (limited to 'php/src/Google/Protobuf/Internal/GPBUtil.php') diff --git a/php/src/Google/Protobuf/Internal/GPBUtil.php b/php/src/Google/Protobuf/Internal/GPBUtil.php index 8c97e9fa..22ad27f9 100644 --- a/php/src/Google/Protobuf/Internal/GPBUtil.php +++ b/php/src/Google/Protobuf/Internal/GPBUtil.php @@ -45,8 +45,13 @@ class GPBUtil $value = bcsub(0, $value); } - $high = (int) bcdiv(bcadd($value, 1), 4294967296); + $high = bcdiv($value, 4294967296); $low = bcmod($value, 4294967296); + if (bccomp($high, 2147483647) > 0) { + $high = (int) bcsub($high, 4294967296); + } else { + $high = (int) $high; + } if (bccomp($low, 2147483647) > 0) { $low = (int) bcsub($low, 4294967296); } else { @@ -58,7 +63,7 @@ class GPBUtil $low = ~$low; $low++; if (!$low) { - $high++; + $high = (int)($high + 1); } } @@ -70,15 +75,13 @@ class GPBUtil public static function checkString(&$var, $check_utf8) { if (is_array($var) || is_object($var)) { - trigger_error("Expect string.", E_USER_ERROR); - return; + throw new \InvalidArgumentException("Expect string."); } if (!is_string($var)) { $var = strval($var); } if ($check_utf8 && !preg_match('//u', $var)) { - trigger_error("Expect utf-8 encoding.", E_USER_ERROR); - return; + throw new \Exception("Expect utf-8 encoding."); } } @@ -92,7 +95,7 @@ class GPBUtil if (is_numeric($var)) { $var = intval($var); } else { - trigger_error("Expect integer.", E_USER_ERROR); + throw new \Exception("Expect integer."); } } @@ -109,7 +112,7 @@ class GPBUtil $var = (int) $var; } } else { - trigger_error("Expect integer.", E_USER_ERROR); + throw new \Exception("Expect integer."); } } @@ -119,10 +122,15 @@ class GPBUtil if (PHP_INT_SIZE == 8) { $var = intval($var); } else { - $var = bcdiv($var, 1, 0); + if (is_float($var) || + is_integer($var) || + (is_string($var) && + bccomp($var, "9223372036854774784") < 0)) { + $var = number_format($var, 0, ".", ""); + } } } else { - trigger_error("Expect integer.", E_USER_ERROR); + throw new \Exception("Expect integer."); } } @@ -132,10 +140,10 @@ class GPBUtil if (PHP_INT_SIZE == 8) { $var = intval($var); } else { - $var = bcdiv($var, 1, 0); + $var = number_format($var, 0, ".", ""); } } else { - trigger_error("Expect integer.", E_USER_ERROR); + throw new \Exception("Expect integer."); } } @@ -144,7 +152,7 @@ class GPBUtil if (is_float($var) || is_numeric($var)) { $var = floatval($var); } else { - trigger_error("Expect float.", E_USER_ERROR); + throw new \Exception("Expect float."); } } @@ -153,15 +161,14 @@ class GPBUtil if (is_float($var) || is_numeric($var)) { $var = floatval($var); } else { - trigger_error("Expect float.", E_USER_ERROR); + throw new \Exception("Expect float."); } } public static function checkBool(&$var) { if (is_array($var) || is_object($var)) { - trigger_error("Expect boolean.", E_USER_ERROR); - return; + throw new \Exception("Expect boolean."); } $var = boolval($var); } @@ -169,14 +176,14 @@ class GPBUtil public static function checkMessage(&$var, $klass) { if (!$var instanceof $klass && !is_null($var)) { - trigger_error("Expect message.", E_USER_ERROR); + throw new \Exception("Expect message."); } } public static function checkRepeatedField(&$var, $type, $klass = null) { if (!$var instanceof RepeatedField && !is_array($var)) { - trigger_error("Expect array.", E_USER_ERROR); + throw new \Exception("Expect array."); } if (is_array($var)) { $tmp = new RepeatedField($type, $klass); @@ -186,15 +193,13 @@ class GPBUtil return $tmp; } else { if ($var->getType() != $type) { - trigger_error( - "Expect repeated field of different type.", - E_USER_ERROR); + throw new \Exception( + "Expect repeated field of different type."); } if ($var->getType() === GPBType::MESSAGE && $var->getClass() !== $klass) { - trigger_error( - "Expect repeated field of different message.", - E_USER_ERROR); + throw new \Exception( + "Expect repeated field of different message."); } return $var; } @@ -203,7 +208,7 @@ class GPBUtil public static function checkMapField(&$var, $key_type, $value_type, $klass = null) { if (!$var instanceof MapField && !is_array($var)) { - trigger_error("Expect dict.", E_USER_ERROR); + throw new \Exception("Expect dict."); } if (is_array($var)) { $tmp = new MapField($key_type, $value_type, $klass); @@ -213,20 +218,15 @@ class GPBUtil return $tmp; } else { if ($var->getKeyType() != $key_type) { - trigger_error( - "Expect map field of key type.", - E_USER_ERROR); + throw new \Exception("Expect map field of key type."); } if ($var->getValueType() != $value_type) { - trigger_error( - "Expect map field of value type.", - E_USER_ERROR); + throw new \Exception("Expect map field of value type."); } if ($var->getValueType() === GPBType::MESSAGE && $var->getValueClass() !== $klass) { - trigger_error( - "Expect map field of different value message.", - E_USER_ERROR); + throw new \Exception( + "Expect map field of different value message."); } return $var; } @@ -328,7 +328,7 @@ class GPBUtil $low = ~$low; $low++; if (!$low) { - $high++; + $high = (int) ($high + 1); } } $result = bcadd(bcmul($high, 4294967296), $low); -- cgit v1.2.3