GPBUtil.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. class GPBUtil
  35. {
  36. public function divideInt64ToInt32($value, &$high, &$low, $trim = false)
  37. {
  38. $isNeg = (bccomp($value, 0) < 0);
  39. if ($isNeg) {
  40. $value = bcsub(0, $value);
  41. }
  42. $high = (int) bcdiv(bcadd($value, 1), 4294967296);
  43. $low = (int) bcmod($value, 4294967296);
  44. if ($isNeg) {
  45. $high = ~$high;
  46. $low = ~$low;
  47. $low++;
  48. if (!$low) {
  49. $high++;
  50. }
  51. }
  52. if ($trim) {
  53. $high = 0;
  54. }
  55. }
  56. public static function checkString(&$var, $check_utf8)
  57. {
  58. if (is_array($var) || is_object($var)) {
  59. trigger_error("Expect string.", E_USER_ERROR);
  60. return;
  61. }
  62. if (!is_string($var)) {
  63. $var = strval($var);
  64. }
  65. if ($check_utf8 && !preg_match('//u', $var)) {
  66. trigger_error("Expect utf-8 encoding.", E_USER_ERROR);
  67. return;
  68. }
  69. }
  70. public static function checkEnum(&$var)
  71. {
  72. static::checkInt32($var);
  73. }
  74. public static function checkInt32(&$var)
  75. {
  76. if (is_numeric($var)) {
  77. $var = intval($var);
  78. } else {
  79. trigger_error("Expect integer.", E_USER_ERROR);
  80. }
  81. }
  82. public static function checkUint32(&$var)
  83. {
  84. if (is_numeric($var)) {
  85. if (PHP_INT_SIZE === 8) {
  86. $var = intval($var);
  87. $var |= ((-(($var >> 31) & 0x1)) & ~0xFFFFFFFF);
  88. } else {
  89. if (bccomp($var, 0x7FFFFFFF) > 0) {
  90. $var = bcsub($var, "4294967296");
  91. }
  92. $var = (int) $var;
  93. }
  94. } else {
  95. trigger_error("Expect integer.", E_USER_ERROR);
  96. }
  97. }
  98. public static function checkInt64(&$var)
  99. {
  100. if (is_numeric($var)) {
  101. if (PHP_INT_SIZE == 8) {
  102. $var = intval($var);
  103. } else {
  104. $var = bcdiv($var, 1, 0);
  105. }
  106. } else {
  107. trigger_error("Expect integer.", E_USER_ERROR);
  108. }
  109. }
  110. public static function checkUint64(&$var)
  111. {
  112. if (is_numeric($var)) {
  113. if (PHP_INT_SIZE == 8) {
  114. $var = intval($var);
  115. } else {
  116. $var = bcdiv($var, 1, 0);
  117. }
  118. } else {
  119. trigger_error("Expect integer.", E_USER_ERROR);
  120. }
  121. }
  122. public static function checkFloat(&$var)
  123. {
  124. if (is_float($var) || is_numeric($var)) {
  125. $var = floatval($var);
  126. } else {
  127. trigger_error("Expect float.", E_USER_ERROR);
  128. }
  129. }
  130. public static function checkDouble(&$var)
  131. {
  132. if (is_float($var) || is_numeric($var)) {
  133. $var = floatval($var);
  134. } else {
  135. trigger_error("Expect float.", E_USER_ERROR);
  136. }
  137. }
  138. public static function checkBool(&$var)
  139. {
  140. if (is_array($var) || is_object($var)) {
  141. trigger_error("Expect boolean.", E_USER_ERROR);
  142. return;
  143. }
  144. $var = boolval($var);
  145. }
  146. public static function checkMessage(&$var, $klass)
  147. {
  148. if (!$var instanceof $klass && !is_null($var)) {
  149. trigger_error("Expect message.", E_USER_ERROR);
  150. }
  151. }
  152. public static function checkRepeatedField(&$var, $type, $klass = null)
  153. {
  154. if (!$var instanceof RepeatedField) {
  155. trigger_error("Expect repeated field.", E_USER_ERROR);
  156. }
  157. if ($var->getType() != $type) {
  158. trigger_error(
  159. "Expect repeated field of different type.",
  160. E_USER_ERROR);
  161. }
  162. if ($var->getType() === GPBType::MESSAGE &&
  163. $var->getClass() !== $klass) {
  164. trigger_error(
  165. "Expect repeated field of different message.",
  166. E_USER_ERROR);
  167. }
  168. }
  169. public static function Int64($value)
  170. {
  171. return new Int64($value);
  172. }
  173. public static function Uint64($value)
  174. {
  175. return new Uint64($value);
  176. }
  177. }