CommUtil.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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. * @param int $min
  44. * @param int $max
  45. * @return type
  46. */
  47. public static function random($min, $max) {
  48. return rand($min, $max);
  49. }
  50. /**
  51. * 在1到1万之间取一个随机值
  52. * @return type
  53. */
  54. public static function random10K() {
  55. return self::random(1, 10000);
  56. }
  57. /**
  58. * 直接计算百分比是否落在区间内,相当于骰一次100面骰子,且结果正好小于参数指定的值.
  59. * @param float $percent 百分之x(精度±0.01%)
  60. * @return boolean true 本地投筛子成功, false 失败
  61. */
  62. public static function randomPercent($percent) {
  63. return (self::random(1, 10000) / 100) <= $percent;
  64. }
  65. /**
  66. * 对象装箱
  67. * @param type $surObj 原始obj
  68. * @param type $desObj 具体obj
  69. */
  70. public static function loadObject($surObj, &$desObj) {
  71. if ($surObj == null || $desObj == null) {
  72. return;
  73. }
  74. foreach ($desObj as $key => $value) {
  75. if (property_exists($surObj, $key)) {
  76. $desObj->$key = $surObj->$key;
  77. }
  78. }
  79. }
  80. /**
  81. * 方法调用
  82. * @param type $func
  83. * @param type $paras
  84. * @param type $class
  85. */
  86. public static function callFunction($func, $paras, $class = "") {
  87. if ($class == "" || $class == null) {
  88. call_user_func_array($func, $paras);
  89. } else {
  90. call_user_func_array(array($class, $func), $paras);
  91. }
  92. }
  93. /**
  94. * 浮点数转整形
  95. * @param type $value
  96. * @return type
  97. */
  98. public static function floatToInt($value) {
  99. return intval(round($value - 0.49999));
  100. }
  101. /**
  102. * 浮点数转上整形
  103. * @param type $value
  104. * @return type
  105. */
  106. public static function floatToCeil($value) {
  107. return round($value + 0.49999);
  108. }
  109. /**
  110. * 解析异常描述信息
  111. * @param type $e
  112. * @return type
  113. */
  114. public static function getExceptMsg($e) {
  115. return 'Msg:' . $e->getMessage() . ' Code:' . $e->getCode() . ' File:' . $e->getFile() . ' Line:' . $e->getLine();
  116. }
  117. /**
  118. * 字符串分割
  119. * @param type $str
  120. * @param type $char
  121. * @return type
  122. */
  123. public static function split($str, $char) {
  124. $strArr = explode($char, $str);
  125. return $strArr;
  126. }
  127. /**
  128. * 属性是否存在
  129. * @param type $obj
  130. * @param type $property
  131. * @return type
  132. */
  133. public static function isPropertyExists($obj, $property) {
  134. // DebugHelper::var_dump($property);
  135. return property_exists($obj, $property);
  136. }
  137. /**
  138. * 元素是否位于某数组中
  139. * @param type $array
  140. * @param type $item
  141. * @return type
  142. */
  143. public static function isInArray($array, $item) {
  144. return in_array($item, $array);
  145. }
  146. /**
  147. * 统计数组元素数或者对象属性个数
  148. * @param type $var
  149. * @return type
  150. */
  151. public static function count($var) {
  152. return count((array) $var);
  153. }
  154. /**
  155. * 字符串包含判定
  156. * @param type $string 完整字符串
  157. * @param type $sub 子串
  158. * @return type
  159. */
  160. public static function isInString($string, $sub) {
  161. return strpos($string, $sub) >= 0;
  162. }
  163. /**
  164. * 对数组/关联数组进行key=>value模式的遍历
  165. * @param array $array 原始数组
  166. * @param callable $callback ($key,$val)
  167. * @return array 返回callback返回true的item的集合
  168. */
  169. public static function arrayKVfilter(array $array, $callback = null) {
  170. if ($callback == null) {
  171. $callback = function ($key, $val) {
  172. return (bool) $val;
  173. };
  174. }
  175. $return = array();
  176. foreach ($array as $key => $val) {
  177. if ($callback($key, $val)) {
  178. $return[$key] = $val;
  179. }
  180. }
  181. return $return;
  182. }
  183. /**
  184. * PHP stdClass Object转array
  185. *
  186. */
  187. public static function object_array($array) {
  188. if (is_object($array)) {
  189. $array = (array) $array;
  190. }
  191. if (is_array($array)) {
  192. foreach ($array as $key => $value) {
  193. $array[$key] = self::object_array($value);
  194. }
  195. }
  196. return $array;
  197. }
  198. /**
  199. * 数组转对象
  200. * @param type $arr
  201. * @return \stdClass
  202. */
  203. public static function array2obj($arr) {
  204. $obj = new \stdClass();
  205. if (!is_null($arr)) {
  206. $vars = is_array($arr) ? $arr : (array) $arr; # 关联数组
  207. foreach ($vars as $name => $value) {
  208. $obj->$name = $value; # 取参数中的或者默认值
  209. }
  210. }
  211. return $obj;
  212. }
  213. /**
  214. * 将转义符"\"增加为"\\",方便入库
  215. * * */
  216. public static function parseBackslash($str) {
  217. return str_replace("\\\\", "\\\\\\\\", $str);
  218. }
  219. /**
  220. * Zip压缩启用
  221. */
  222. public static function output_compress() {
  223. ini_set("zlib.output_compression", "On");
  224. }
  225. /**
  226. * 将任意编码格式的字符串转换为utf-8编码
  227. * @param 原始字符串 $str
  228. * @return 转换后的字符串
  229. */
  230. public static function str2UTF8($str) {
  231. if (false !== mb_detect_encoding($str, 'UTF-8', true)) {
  232. return $str;
  233. }
  234. if (false !== mb_detect_encoding($str, 'gbk', true)) {
  235. return iconv('gbk', 'UTF-8', $str);
  236. }
  237. return iconv(mb_detect_encoding($str), 'UTF-8//IGNORE', $str);
  238. }
  239. }
  240. function arr2obj($arr) {
  241. return CommUtil::array2obj($arr);
  242. }
  243. function obj2arr($obj) {
  244. return CommUtil::object_array($obj);
  245. }
  246. /**
  247. * 设定脚本内使用的date函数的时区为中华人民共和国
  248. */
  249. function default_timezone() {
  250. date_default_timezone_set("PRC");
  251. }
  252. /**
  253. * 如果变量为null则取默认值
  254. * @param type $obj
  255. * @param type $defaultValue
  256. */
  257. function my_null_default($obj, $defaultValue) {
  258. return ($obj === null) ? $defaultValue : $obj; # 注意这里必须用===判断
  259. }
  260. /**
  261. * 确保指定变量不为空(若为空则用stdclass初始化)
  262. * @param \stdClass $obj
  263. */
  264. function my_default_Obj(&$obj) {
  265. if (null == $obj) {
  266. $obj = new \stdClass();
  267. }
  268. }
  269. /**
  270. * 确保指定变量不为空(若为空则用array()初始化)
  271. * @param \array $arr
  272. */
  273. function my_default_Arr(&$arr) {
  274. if (null == $arr) {
  275. $arr = array();
  276. }
  277. }
  278. /**
  279. * 对象初始化
  280. * @return type
  281. */
  282. function ObjectInit() {
  283. return new \stdClass();
  284. }
  285. /**
  286. * 数组初始化
  287. * @return type
  288. */
  289. function ArrayInit() {
  290. return array();
  291. }
  292. /**
  293. * 是否处于编辑器中,辅助智能感知代码的 gwang(⊙ω⊙)
  294. * @return bool 是否处于编辑器中
  295. */
  296. function isEditor() {
  297. return false; # 永久性的false就好
  298. }