GPBUtil.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. <?php
  2. // Protocol Buffers - Google's data interchange format
  3. // Copyright 2008 Google Inc. All rights reserved.
  4. // https://developers.google.com/protocol-buffers/
  5. //
  6. // Redistribution and use in source and binary forms, with or without
  7. // modification, are permitted provided that the following conditions are
  8. // met:
  9. //
  10. // * Redistributions of source code must retain the above copyright
  11. // notice, this list of conditions and the following disclaimer.
  12. // * Redistributions in binary form must reproduce the above
  13. // copyright notice, this list of conditions and the following disclaimer
  14. // in the documentation and/or other materials provided with the
  15. // distribution.
  16. // * Neither the name of Google Inc. nor the names of its
  17. // contributors may be used to endorse or promote products derived from
  18. // this software without specific prior written permission.
  19. //
  20. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. namespace Google\Protobuf\Internal;
  32. use Google\Protobuf\Internal\GPBType;
  33. use Google\Protobuf\Internal\RepeatedField;
  34. use Google\Protobuf\Internal\MapField;
  35. class GPBUtil
  36. {
  37. public function divideInt64ToInt32($value, &$high, &$low, $trim = false)
  38. {
  39. $isNeg = (bccomp($value, 0) < 0);
  40. if ($isNeg) {
  41. $value = bcsub(0, $value);
  42. }
  43. $high = (int) bcdiv(bcadd($value, 1), 4294967296);
  44. $low = bcmod($value, 4294967296);
  45. if (bccomp($low, 2147483647) > 0) {
  46. $low = (int) bcsub($low, 4294967296);
  47. } else {
  48. $low = (int) $low;
  49. }
  50. if ($isNeg) {
  51. $high = ~$high;
  52. $low = ~$low;
  53. $low++;
  54. if (!$low) {
  55. $high++;
  56. }
  57. }
  58. if ($trim) {
  59. $high = 0;
  60. }
  61. }
  62. public static function checkString(&$var, $check_utf8)
  63. {
  64. if (is_array($var) || is_object($var)) {
  65. trigger_error("Expect string.", E_USER_ERROR);
  66. return;
  67. }
  68. if (!is_string($var)) {
  69. $var = strval($var);
  70. }
  71. if ($check_utf8 && !preg_match('//u', $var)) {
  72. trigger_error("Expect utf-8 encoding.", E_USER_ERROR);
  73. return;
  74. }
  75. }
  76. public static function checkEnum(&$var)
  77. {
  78. static::checkInt32($var);
  79. }
  80. public static function checkInt32(&$var)
  81. {
  82. if (is_numeric($var)) {
  83. $var = intval($var);
  84. } else {
  85. trigger_error("Expect integer.", E_USER_ERROR);
  86. }
  87. }
  88. public static function checkUint32(&$var)
  89. {
  90. if (is_numeric($var)) {
  91. if (PHP_INT_SIZE === 8) {
  92. $var = intval($var);
  93. $var |= ((-(($var >> 31) & 0x1)) & ~0xFFFFFFFF);
  94. } else {
  95. if (bccomp($var, 0x7FFFFFFF) > 0) {
  96. $var = bcsub($var, "4294967296");
  97. }
  98. $var = (int) $var;
  99. }
  100. } else {
  101. trigger_error("Expect integer.", E_USER_ERROR);
  102. }
  103. }
  104. public static function checkInt64(&$var)
  105. {
  106. if (is_numeric($var)) {
  107. if (PHP_INT_SIZE == 8) {
  108. $var = intval($var);
  109. } else {
  110. $var = bcdiv($var, 1, 0);
  111. }
  112. } else {
  113. trigger_error("Expect integer.", E_USER_ERROR);
  114. }
  115. }
  116. public static function checkUint64(&$var)
  117. {
  118. if (is_numeric($var)) {
  119. if (PHP_INT_SIZE == 8) {
  120. $var = intval($var);
  121. } else {
  122. $var = bcdiv($var, 1, 0);
  123. }
  124. } else {
  125. trigger_error("Expect integer.", E_USER_ERROR);
  126. }
  127. }
  128. public static function checkFloat(&$var)
  129. {
  130. if (is_float($var) || is_numeric($var)) {
  131. $var = floatval($var);
  132. } else {
  133. trigger_error("Expect float.", E_USER_ERROR);
  134. }
  135. }
  136. public static function checkDouble(&$var)
  137. {
  138. if (is_float($var) || is_numeric($var)) {
  139. $var = floatval($var);
  140. } else {
  141. trigger_error("Expect float.", E_USER_ERROR);
  142. }
  143. }
  144. public static function checkBool(&$var)
  145. {
  146. if (is_array($var) || is_object($var)) {
  147. trigger_error("Expect boolean.", E_USER_ERROR);
  148. return;
  149. }
  150. $var = boolval($var);
  151. }
  152. public static function checkMessage(&$var, $klass)
  153. {
  154. if (!$var instanceof $klass && !is_null($var)) {
  155. trigger_error("Expect message.", E_USER_ERROR);
  156. }
  157. }
  158. public static function checkRepeatedField(&$var, $type, $klass = null)
  159. {
  160. if (!$var instanceof RepeatedField && !is_array($var)) {
  161. trigger_error("Expect array.", E_USER_ERROR);
  162. }
  163. if (is_array($var)) {
  164. $tmp = new RepeatedField($type, $klass);
  165. foreach ($var as $value) {
  166. $tmp[] = $value;
  167. }
  168. return $tmp;
  169. } else {
  170. if ($var->getType() != $type) {
  171. trigger_error(
  172. "Expect repeated field of different type.",
  173. E_USER_ERROR);
  174. }
  175. if ($var->getType() === GPBType::MESSAGE &&
  176. $var->getClass() !== $klass) {
  177. trigger_error(
  178. "Expect repeated field of different message.",
  179. E_USER_ERROR);
  180. }
  181. return $var;
  182. }
  183. }
  184. public static function checkMapField(&$var, $key_type, $value_type, $klass = null)
  185. {
  186. if (!$var instanceof MapField && !is_array($var)) {
  187. trigger_error("Expect dict.", E_USER_ERROR);
  188. }
  189. if (is_array($var)) {
  190. $tmp = new MapField($key_type, $value_type, $klass);
  191. foreach ($var as $key => $value) {
  192. $tmp[$key] = $value;
  193. }
  194. return $tmp;
  195. } else {
  196. if ($var->getKeyType() != $key_type) {
  197. trigger_error(
  198. "Expect map field of key type.",
  199. E_USER_ERROR);
  200. }
  201. if ($var->getValueType() != $value_type) {
  202. trigger_error(
  203. "Expect map field of value type.",
  204. E_USER_ERROR);
  205. }
  206. if ($var->getValueType() === GPBType::MESSAGE &&
  207. $var->getValueClass() !== $klass) {
  208. trigger_error(
  209. "Expect map field of different value message.",
  210. E_USER_ERROR);
  211. }
  212. return $var;
  213. }
  214. }
  215. public static function Int64($value)
  216. {
  217. return new Int64($value);
  218. }
  219. public static function Uint64($value)
  220. {
  221. return new Uint64($value);
  222. }
  223. }