|
@@ -0,0 +1,256 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+/**
|
|
|
+ * Description of CommUtil
|
|
|
+ * 通用辅助单元
|
|
|
+ * 常用的辅助方法都写在这里
|
|
|
+ * @author jgao,gwang
|
|
|
+ */
|
|
|
+class CommUtil {
|
|
|
+ //put your code here
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 编码 * data => gzdeflate => base64_encode
|
|
|
+ * // RFC 1950 - zlib
|
|
|
+ * $compressedData = zlib_encode($uncompressedData, 15);
|
|
|
+ * // RFC 1951 - raw deflate
|
|
|
+ * $compressedData = zlib_encode($uncompressedData, -15);
|
|
|
+ * // RFC 1952 - gzip
|
|
|
+ * $compressedData = zlib_encode($uncompressedData, 31);
|
|
|
+ * @link http://php.net/manual/en/function.zlib-encode.php 参考
|
|
|
+ * @param string $data
|
|
|
+ * @return string
|
|
|
+ */
|
|
|
+ public static function zb64encode($data) {
|
|
|
+ return base64_encode(gzdeflate($data));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 解码 * data => base64_decode => gzinflate
|
|
|
+ * // RFC 1950 - zlib
|
|
|
+ * $compressedData = zlib_encode($uncompressedData, 15);
|
|
|
+ * // RFC 1951 - raw deflate
|
|
|
+ * $compressedData = zlib_encode($uncompressedData, -15);
|
|
|
+ * // RFC 1952 - gzip
|
|
|
+ * $compressedData = zlib_encode($uncompressedData, 31);
|
|
|
+ * @link http://php.net/manual/en/function.zlib-encode.php 参考
|
|
|
+ * @param string $data
|
|
|
+ * @return string
|
|
|
+ */
|
|
|
+ public static function zb64decode($data) {
|
|
|
+ return gzinflate(base64_decode($data));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 创建唯一的订单编号
|
|
|
+ * @return type
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ public static function CreateOrderId() {
|
|
|
+ if (true) { # use redis
|
|
|
+ $key = MemKey_GameRun::Game_UserRecordByZone($zoneid, $day);
|
|
|
+ $id = gMem()->increment($key);
|
|
|
+ if (false === $id) {
|
|
|
+ throw new \Exception('create order id failed!', '101');
|
|
|
+ }
|
|
|
+ return $id;
|
|
|
+ } else { # use mysql
|
|
|
+ daoInst()->query("show table status like ''");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ *
|
|
|
+ * @param int $min
|
|
|
+ * @param int $max
|
|
|
+ * @return type
|
|
|
+ */
|
|
|
+ public static function random($min, $max) {
|
|
|
+ return rand($min, $max);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 在1到1万之间取一个随机值
|
|
|
+ * @return type
|
|
|
+ */
|
|
|
+ public static function random10K() {
|
|
|
+ return self::random(1, 10000);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 直接计算百分比是否落在区间内,相当于骰一次100面骰子,且结果正好小于参数指定的值.
|
|
|
+ * @param float $percent 百分之x(精度±0.01%)
|
|
|
+ * @return boolean true 本地投筛子成功, false 失败
|
|
|
+ */
|
|
|
+ public static function randomPercent($percent) {
|
|
|
+ return (self::random(1, 10000) / 100) <= $percent;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 对象装箱
|
|
|
+ * @param type $surObj 原始obj
|
|
|
+ * @param type $desObj 具体obj
|
|
|
+ */
|
|
|
+ public static function loadObject($surObj, &$desObj) {
|
|
|
+ if ($surObj == null || $desObj == null) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ foreach ($desObj as $key => $value) {
|
|
|
+ if (property_exists($surObj, $key)) {
|
|
|
+ $desObj->$key = $surObj->$key;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 方法调用
|
|
|
+ * @param type $func
|
|
|
+ * @param type $paras
|
|
|
+ * @param type $class
|
|
|
+ */
|
|
|
+ public static function callFunction($func, $paras, $class = "") {
|
|
|
+ if ($class == "" || $class == null) {
|
|
|
+ call_user_func_array($func, $paras);
|
|
|
+ } else {
|
|
|
+ call_user_func_array(array($class, $func), $paras);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 浮点数转整形
|
|
|
+ * @param type $value
|
|
|
+ * @return type
|
|
|
+ */
|
|
|
+ public static function floatToInt($value) {
|
|
|
+ return intval(round($value - 0.49999));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 浮点数转上整形
|
|
|
+ * @param type $value
|
|
|
+ * @return type
|
|
|
+ */
|
|
|
+ public static function floatToCeil($value) {
|
|
|
+ return round($value + 0.49999);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 解析异常描述信息
|
|
|
+ * @param type $e
|
|
|
+ * @return type
|
|
|
+ */
|
|
|
+ public static function getExceptMsg($e) {
|
|
|
+ return 'Msg:' . $e->getMessage() . ' Code:' . $e->getCode() . ' File:' . $e->getFile() . ' Line:' . $e->getLine();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 字符串分割
|
|
|
+ * @param type $str
|
|
|
+ * @param type $char
|
|
|
+ * @return type
|
|
|
+ */
|
|
|
+ public static function split($str, $char) {
|
|
|
+ $strArr = explode($char, $str);
|
|
|
+ return $strArr;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 属性是否存在
|
|
|
+ * @param type $obj
|
|
|
+ * @param type $property
|
|
|
+ * @return type
|
|
|
+ */
|
|
|
+ public static function isPropertyExists($obj, $property) {
|
|
|
+// DebugHelper::var_dump($property);
|
|
|
+ return property_exists($obj, $property);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 元素是否位于某数组中
|
|
|
+ * @param type $array
|
|
|
+ * @param type $item
|
|
|
+ * @return type
|
|
|
+ */
|
|
|
+ public static function isInArray($array, $item) {
|
|
|
+ return in_array($item, $array);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 统计数组元素数或者对象属性个数
|
|
|
+ * @param type $var
|
|
|
+ * @return type
|
|
|
+ */
|
|
|
+ public static function count($var) {
|
|
|
+ return count((array) $var);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 字符串包含判定
|
|
|
+ * @param type $string 完整字符串
|
|
|
+ * @param type $sub 子串
|
|
|
+ * @return type
|
|
|
+ */
|
|
|
+ public static function isInString($string, $sub) {
|
|
|
+ return strpos($string, $sub) >= 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 对数组/关联数组进行key=>value模式的遍历
|
|
|
+ * @param array $array 原始数组
|
|
|
+ * @param callable $callback ($key,$val)
|
|
|
+ * @return array 返回callback返回true的item的集合
|
|
|
+ */
|
|
|
+ public static function arrayKVfilter(array $array, $callback = null) {
|
|
|
+ if ($callback == null) {
|
|
|
+ $callback = function ($key, $val) {
|
|
|
+ return (bool) $val;
|
|
|
+ };
|
|
|
+ }
|
|
|
+ $return = array();
|
|
|
+ foreach ($array as $key => $val) {
|
|
|
+ if ($callback($key, $val)) {
|
|
|
+ $return[$key] = $val;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return $return;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * PHP stdClass Object转array
|
|
|
+ *
|
|
|
+ */
|
|
|
+ public static function object_array($array) {
|
|
|
+ if (is_object($array)) {
|
|
|
+ $array = (array) $array;
|
|
|
+ }
|
|
|
+ if (is_array($array)) {
|
|
|
+ foreach ($array as $key => $value) {
|
|
|
+ $array[$key] = self::object_array($value);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return $array;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将转义符"\"增加为"\\",方便入库
|
|
|
+ * * */
|
|
|
+ public static function parseBackslash($str) {
|
|
|
+ return str_replace("\\\\", "\\\\\\\\", $str);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将任意编码格式的字符串转换为utf-8编码
|
|
|
+ * @param 原始字符串 $str
|
|
|
+ * @return 转换后的字符串
|
|
|
+ */
|
|
|
+ public static function str2UTF8($str) {
|
|
|
+ if (false !== mb_detect_encoding($str, 'UTF-8', true)) {
|
|
|
+ return $str;
|
|
|
+ }
|
|
|
+ if (false !== mb_detect_encoding($str, 'gbk', true)) {
|
|
|
+ return iconv('gbk', 'UTF-8', $str);
|
|
|
+ }
|
|
|
+ return iconv(mb_detect_encoding($str), 'UTF-8//IGNORE', $str);
|
|
|
+ }
|
|
|
+
|
|
|
+}
|