CommUtil.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. <?php
  2. /**
  3. * Description of CommUtil
  4. * 通用辅助单元
  5. * 常用的辅助方法都写在这里
  6. * @author jgao,gwang
  7. */
  8. class CommUtil {
  9. //put your code here
  10. /**
  11. * 编码 * data => gzdeflate => base64_encode
  12. * // RFC 1950 - zlib
  13. * $compressedData = zlib_encode($uncompressedData, 15);
  14. * // RFC 1951 - raw deflate
  15. * $compressedData = zlib_encode($uncompressedData, -15);
  16. * // RFC 1952 - gzip
  17. * $compressedData = zlib_encode($uncompressedData, 31);
  18. * @link http://php.net/manual/en/function.zlib-encode.php 参考
  19. * @param string $data
  20. * @return string
  21. */
  22. public static function zb64encode($data) {
  23. return base64_encode(gzdeflate($data));
  24. }
  25. /**
  26. * 解码 * data => base64_decode => gzinflate
  27. * // RFC 1950 - zlib
  28. * $compressedData = zlib_encode($uncompressedData, 15);
  29. * // RFC 1951 - raw deflate
  30. * $compressedData = zlib_encode($uncompressedData, -15);
  31. * // RFC 1952 - gzip
  32. * $compressedData = zlib_encode($uncompressedData, 31);
  33. * @link http://php.net/manual/en/function.zlib-encode.php 参考
  34. * @param string $data
  35. * @return string
  36. */
  37. public static function zb64decode($data) {
  38. return gzinflate(base64_decode($data));
  39. }
  40. /**
  41. * 创建唯一的订单编号
  42. * @return type
  43. * @throws Exception
  44. */
  45. public static function CreateOrderId() {
  46. if (true) { # use redis
  47. $key = MemKey_GameRun::Game_UserRecordByZone($zoneid, $day);
  48. $id = gMem()->increment($key);
  49. if (false === $id) {
  50. throw new \Exception('create order id failed!', '101');
  51. }
  52. return $id;
  53. } else { # use mysql
  54. daoInst()->query("show table status like ''");
  55. }
  56. }
  57. /**
  58. *
  59. * @param int $min
  60. * @param int $max
  61. * @return type
  62. */
  63. public static function random($min, $max) {
  64. return rand($min, $max);
  65. }
  66. /**
  67. * 在1到1万之间取一个随机值
  68. * @return type
  69. */
  70. public static function random10K() {
  71. return self::random(1, 10000);
  72. }
  73. /**
  74. * 直接计算百分比是否落在区间内,相当于骰一次100面骰子,且结果正好小于参数指定的值.
  75. * @param float $percent 百分之x(精度±0.01%)
  76. * @return boolean true 本地投筛子成功, false 失败
  77. */
  78. public static function randomPercent($percent) {
  79. return (self::random(1, 10000) / 100) <= $percent;
  80. }
  81. /**
  82. * 对象装箱
  83. * @param type $surObj 原始obj
  84. * @param type $desObj 具体obj
  85. */
  86. public static function loadObject($surObj, &$desObj) {
  87. if ($surObj == null || $desObj == null) {
  88. return;
  89. }
  90. foreach ($desObj as $key => $value) {
  91. if (property_exists($surObj, $key)) {
  92. $desObj->$key = $surObj->$key;
  93. }
  94. }
  95. }
  96. /**
  97. * 方法调用
  98. * @param type $func
  99. * @param type $paras
  100. * @param type $class
  101. */
  102. public static function callFunction($func, $paras, $class = "") {
  103. if ($class == "" || $class == null) {
  104. call_user_func_array($func, $paras);
  105. } else {
  106. call_user_func_array(array($class, $func), $paras);
  107. }
  108. }
  109. /**
  110. * 浮点数转整形
  111. * @param type $value
  112. * @return type
  113. */
  114. public static function floatToInt($value) {
  115. return intval(round($value - 0.49999));
  116. }
  117. /**
  118. * 浮点数转上整形
  119. * @param type $value
  120. * @return type
  121. */
  122. public static function floatToCeil($value) {
  123. return round($value + 0.49999);
  124. }
  125. /**
  126. * 解析异常描述信息
  127. * @param type $e
  128. * @return type
  129. */
  130. public static function getExceptMsg($e) {
  131. return 'Msg:' . $e->getMessage() . ' Code:' . $e->getCode() . ' File:' . $e->getFile() . ' Line:' . $e->getLine();
  132. }
  133. /**
  134. * 字符串分割
  135. * @param type $str
  136. * @param type $char
  137. * @return type
  138. */
  139. public static function split($str, $char) {
  140. $strArr = explode($char, $str);
  141. return $strArr;
  142. }
  143. /**
  144. * 属性是否存在
  145. * @param type $obj
  146. * @param type $property
  147. * @return type
  148. */
  149. public static function isPropertyExists($obj, $property) {
  150. // DebugHelper::var_dump($property);
  151. return property_exists($obj, $property);
  152. }
  153. /**
  154. * 元素是否位于某数组中
  155. * @param type $array
  156. * @param type $item
  157. * @return type
  158. */
  159. public static function isInArray($array, $item) {
  160. return in_array($item, $array);
  161. }
  162. /**
  163. * 统计数组元素数或者对象属性个数
  164. * @param type $var
  165. * @return type
  166. */
  167. public static function count($var) {
  168. return count((array) $var);
  169. }
  170. /**
  171. * 字符串包含判定
  172. * @param type $string 完整字符串
  173. * @param type $sub 子串
  174. * @return type
  175. */
  176. public static function isInString($string, $sub) {
  177. return strpos($string, $sub) >= 0;
  178. }
  179. /**
  180. * 对数组/关联数组进行key=>value模式的遍历
  181. * @param array $array 原始数组
  182. * @param callable $callback ($key,$val)
  183. * @return array 返回callback返回true的item的集合
  184. */
  185. public static function arrayKVfilter(array $array, $callback = null) {
  186. if ($callback == null) {
  187. $callback = function ($key, $val) {
  188. return (bool) $val;
  189. };
  190. }
  191. $return = array();
  192. foreach ($array as $key => $val) {
  193. if ($callback($key, $val)) {
  194. $return[$key] = $val;
  195. }
  196. }
  197. return $return;
  198. }
  199. /**
  200. * PHP stdClass Object转array
  201. *
  202. */
  203. public static function object_array($array) {
  204. if (is_object($array)) {
  205. $array = (array) $array;
  206. }
  207. if (is_array($array)) {
  208. foreach ($array as $key => $value) {
  209. $array[$key] = self::object_array($value);
  210. }
  211. }
  212. return $array;
  213. }
  214. /**
  215. * 将转义符"\"增加为"\\",方便入库
  216. * * */
  217. public static function parseBackslash($str) {
  218. return str_replace("\\\\", "\\\\\\\\", $str);
  219. }
  220. /**
  221. * 将任意编码格式的字符串转换为utf-8编码
  222. * @param 原始字符串 $str
  223. * @return 转换后的字符串
  224. */
  225. public static function str2UTF8($str) {
  226. if (false !== mb_detect_encoding($str, 'UTF-8', true)) {
  227. return $str;
  228. }
  229. if (false !== mb_detect_encoding($str, 'gbk', true)) {
  230. return iconv('gbk', 'UTF-8', $str);
  231. }
  232. return iconv(mb_detect_encoding($str), 'UTF-8//IGNORE', $str);
  233. }
  234. }