CommUtil.php 7.5 KB

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