From e0e54661f76183684dca66694967a60cbb10f04e Mon Sep 17 00:00:00 2001 From: Paul Yang Date: Thu, 15 Sep 2016 11:09:01 -0700 Subject: Check in php implementation. (#2052) This pull request includes two implementation: C extension and PHP package. Both implementations support encode/decode of singular, repeated and map fields. --- php/src/Google/Protobuf/Internal/GPBUtil.php | 161 +++++++++++++++++++++++++++ 1 file changed, 161 insertions(+) create mode 100644 php/src/Google/Protobuf/Internal/GPBUtil.php (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 new file mode 100644 index 00000000..417a9729 --- /dev/null +++ b/php/src/Google/Protobuf/Internal/GPBUtil.php @@ -0,0 +1,161 @@ +> 31) & 0x1)) & ~0xFFFFFFFF); + } + } else { + trigger_error("Expect integer.", E_USER_ERROR); + } + } + + public static function checkInt64(&$var) + { + if (is_numeric($var)) { + $var = intval($var); + } else { + trigger_error("Expect integer.", E_USER_ERROR); + } + } + + public static function checkUint64(&$var) + { + if (is_numeric($var)) { + $var = intval($var); + } else { + trigger_error("Expect integer.", E_USER_ERROR); + } + } + + public static function checkFloat(&$var) + { + if (is_float($var) || is_numeric($var)) { + $var = floatval($var); + } else { + trigger_error("Expect float.", E_USER_ERROR); + } + } + + public static function checkDouble(&$var) + { + if (is_float($var) || is_numeric($var)) { + $var = floatval($var); + } else { + trigger_error("Expect float.", E_USER_ERROR); + } + } + + public static function checkBool(&$var) + { + if (is_array($var) || is_object($var)) { + trigger_error("Expect boolean.", E_USER_ERROR); + return; + } + $var = boolval($var); + } + + public static function checkMessage(&$var, $klass) + { + if (!$var instanceof $klass && !is_null($var)) { + trigger_error("Expect message.", E_USER_ERROR); + } + } + + public static function checkRepeatedField(&$var, $type, $klass = null) + { + if (!$var instanceof RepeatedField) { + trigger_error("Expect repeated field.", E_USER_ERROR); + } + if ($var->getType() != $type) { + trigger_error( + "Expect repeated field of different type.", + E_USER_ERROR); + } + if ($var->getType() === GPBType::MESSAGE && + $var->getClass() !== $klass) { + trigger_error( + "Expect repeated field of different message.", + E_USER_ERROR); + } + } + + public static function Int64($value) + { + return new Int64($value); + } + + public static function Uint64($value) + { + return new Uint64($value); + } +} -- cgit v1.2.3