PageRenderTime 95ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/wind/utility/WindUtility.php

http://github.com/phpwind/windframework
PHP | 245 lines | 139 code | 9 blank | 97 comment | 34 complexity | 4f37c106bec771889d81e4150f37f39e MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * 通用工具库
  4. *
  5. * @author Qiong Wu <papa0924@gmail.com>
  6. * @copyright ©2003-2103 phpwind.com
  7. * @license http://www.windframework.com
  8. * @version $Id$
  9. * @package utility
  10. */
  11. class WindUtility {
  12. /**
  13. * 解析表达式
  14. *
  15. * 表达式格式: namespace:arg1.arg2.arg3.arg4.arg5==value
  16. * 返回: array($namespace, $param1, $operator, $param1)
  17. *
  18. * @param string $expression 待解析的表达式
  19. * @return array 返回解析后的表达式,由表达式的各项组成的数组:
  20. * <ul>
  21. * <li>第一个元素: 命名空间</li>
  22. * <li>第二个元素: 表达式的左边操作数</li>
  23. * <li>第三个元素: 表达式的操作符</li>
  24. * <li>第四个元素: 表达式的右边操作数</li>
  25. * </ul>
  26. */
  27. public static function resolveExpression($expression) {
  28. $operators = array('==', '!=', '<', '>', '<=', '>=');
  29. $operatorsReplace = array('#==#', '#!=#', '#<#', '#>#', '#<=#', '#>=#');
  30. list($p, $o, $v2) = explode('#', str_replace($operators, $operatorsReplace, $expression));
  31. if (strpos($p, ":") !== false)
  32. list($_namespace, $p) = explode(':', trim($p, ':'));
  33. else
  34. $_namespace = '';
  35. return array($_namespace, $p, $o, $v2);
  36. }
  37. /**
  38. * 执行简单的条件表达式
  39. *
  40. * 只能执行==、!=、<、>、<=、>=简单的比较
  41. *
  42. * @param string $v1 左边的操作数
  43. * @param string $v2 右边的操作数
  44. * @param string $operator 操作符号
  45. * @return boolean
  46. */
  47. public static function evalExpression($v1, $v2, $operator) {
  48. switch ($operator) {
  49. case '==':
  50. return $v1 == $v2;
  51. case '!=':
  52. return $v1 != $v2;
  53. case '<':
  54. return $v1 < $v2;
  55. case '>':
  56. return $v1 > $v2;
  57. case '<=':
  58. return $v1 <= $v2;
  59. case '>=':
  60. return $v1 >= $v2;
  61. default:
  62. return false;
  63. }
  64. return false;
  65. }
  66. /**
  67. * 递归合并两个数组
  68. *
  69. * @param array $array1 数组1
  70. * @param array $array2 数组2
  71. * @return array 合并后的数组
  72. */
  73. public static function mergeArray($array1, $array2) {
  74. foreach ($array2 as $key => $value) {
  75. if (empty($value)) {
  76. $array1[$key] = $value;
  77. } else if (!isset($array1[$key])) {
  78. $array1[$key] = $value;
  79. } elseif (is_array($array1[$key]) && is_array($value)) {
  80. $array1[$key] = self::mergeArray($array1[$key], $array2[$key]);
  81. } elseif (is_numeric($key) && $array1[$key] !== $array2[$key]) {
  82. $array1[] = $value;
  83. } else
  84. $array1[$key] = $value;
  85. }
  86. return $array1;
  87. }
  88. /**
  89. * 将字符串首字母小写
  90. *
  91. * @param string $str 待处理的字符串
  92. * @return string 返回处理后的字符串
  93. */
  94. public static function lcfirst($str) {
  95. $str[0] = strtolower($str[0]);
  96. return $str;
  97. }
  98. /**
  99. * 获得随机数字符串
  100. *
  101. * @param int $length 随机数的长度
  102. * @return string 随机获得的字串
  103. */
  104. public static function generateRandStr($length) {
  105. $randstr = "";
  106. for ($i = 0; $i < (int) $length; $i++) {
  107. $randnum = mt_rand(0, 61);
  108. if ($randnum < 10) {
  109. $randstr .= chr($randnum + 48);
  110. } else if ($randnum < 36) {
  111. $randstr .= chr($randnum + 55);
  112. } else {
  113. $randstr .= chr($randnum + 61);
  114. }
  115. }
  116. return $randstr;
  117. }
  118. /**
  119. * 通用组装测试验证规则
  120. *
  121. * @param string $field 验证字段名称
  122. * @param string $validator 验证方法
  123. * @param array $args 验证方法中传递的其他参数,需在待验证字段值的后面,默认为空数组
  124. * @param string $default 验证失败是设置的默认值,默认为null
  125. * @param string $message 验证失败是返回的错误信息,默认为空字串
  126. * @return array 返回验证规则
  127. * <ul>
  128. * <li>field: 验证字段名称</li>
  129. * <li>validator: 验证方法</li>
  130. * <li>args: 验证方法中传递的其他参数,需在待验证字段值的后面,缺省为空数组</li>
  131. * <li>default: 验证失败是设置的默认值,缺省为null</li>
  132. * <li>message: 验证失败是返回的错误信息,默认为'提示:XX验证失败'</li>
  133. * </ul>
  134. */
  135. public static function buildValidateRule($field, $validator, $args = array(), $default = null, $message = '') {
  136. return array(
  137. 'field' => $field,
  138. 'validator' => $validator,
  139. 'args' => (array) $args,
  140. 'default' => $default,
  141. 'message' => ($message ? $message : '提示:\'' . $field . '\'验证失败'));
  142. }
  143. /**
  144. * 对字符串中的参数进行替换
  145. *
  146. * 该函优化了php strtr()实现, 在进行数组方式的字符替换时支持了两种模式的字符替换:
  147. * @example<pre>
  148. * 1. echo WindUtility::strtr("I Love {you}",array('{you}' => 'lili'));
  149. * 结果: I Love lili
  150. * 2. echo WindUtility::strtr("I Love #0,#1",array('lili','qiong'));
  151. * 结果: I Love lili,qiong
  152. * <pre>
  153. * @see WindLangResource::getMessage()
  154. * @param string $str
  155. * @param string $from
  156. * @param string $to 可选参数,默认值为''
  157. * @return string
  158. */
  159. public static function strtr($str, $from, $to = '') {
  160. if (is_string($from)) return strtr($str, $from, $to);
  161. if (isset($from[0])) {
  162. foreach ($from as $key => $value) {
  163. $from['#' . $key] = $value;
  164. unset($from[$key]);
  165. }
  166. }
  167. return !empty($from) ? strtr($str, $from) : $str;
  168. }
  169. /**
  170. * 错误信息处理方法
  171. *
  172. * @param string $file
  173. * @param string $line
  174. * @param array $trace
  175. */
  176. public static function crash($file, $line, $trace) {
  177. $msg = '';
  178. $count = count($trace);
  179. $padLen = strlen($count);
  180. foreach ($trace as $key => $call) {
  181. if (!isset($call['file']) || $call['file'] == '') {
  182. $call['file'] = '~Internal Location~';
  183. $call['line'] = 'N/A';
  184. }
  185. $traceLine = '#' . str_pad(($count - $key), $padLen, "0", STR_PAD_LEFT) . ' ' . self::getCallLine(
  186. $call);
  187. $trace[$key] = $traceLine;
  188. }
  189. $fileLines = array();
  190. if (is_file($file)) {
  191. $currentLine = $line - 1;
  192. $fileLines = explode("\n", file_get_contents($file, null, null, 0, 10000000));
  193. $topLine = $currentLine - 5;
  194. $fileLines = array_slice($fileLines, $topLine > 0 ? $topLine : 0, 10, true);
  195. if (($count = count($fileLines)) > 0) {
  196. $padLen = strlen($count);
  197. foreach ($fileLines as $line => &$fileLine)
  198. $fileLine = " " . htmlspecialchars(
  199. str_pad($line + 1, $padLen, "0", STR_PAD_LEFT) . ": " . str_replace("\t",
  200. " ", rtrim($fileLine)), null, "UTF-8");
  201. }
  202. }
  203. return array($fileLines, $trace);
  204. }
  205. /**
  206. * @param array $call
  207. * @return string
  208. */
  209. private static function getCallLine($call) {
  210. $call_signature = "";
  211. if (isset($call['file'])) $call_signature .= $call['file'] . " ";
  212. if (isset($call['line'])) $call_signature .= "(" . $call['line'] . ") ";
  213. if (isset($call['function'])) {
  214. $call_signature .= $call['function'] . "(";
  215. if (isset($call['args'])) {
  216. foreach ($call['args'] as $arg) {
  217. if (is_string($arg))
  218. $arg = '"' . (strlen($arg) <= 64 ? $arg : substr($arg, 0, 64) . "…") . '"';
  219. else if (is_object($arg))
  220. $arg = "[Instance of '" . get_class($arg) . "']";
  221. else if ($arg === true)
  222. $arg = "true";
  223. else if ($arg === false)
  224. $arg = "false";
  225. else if ($arg === null)
  226. $arg = "null";
  227. else
  228. $arg = strval($arg);
  229. $call_signature .= $arg . ',';
  230. }
  231. }
  232. $call_signature = trim($call_signature, ',') . ")";
  233. }
  234. return $call_signature;
  235. }
  236. }