/wind/utility/WindUtility.php
PHP | 245 lines | 139 code | 9 blank | 97 comment | 34 complexity | 4f37c106bec771889d81e4150f37f39e MD5 | raw file
Possible License(s): BSD-3-Clause
- <?php
- /**
- * 通用工具库
- *
- * @author Qiong Wu <papa0924@gmail.com>
- * @copyright ©2003-2103 phpwind.com
- * @license http://www.windframework.com
- * @version $Id$
- * @package utility
- */
- class WindUtility {
-
- /**
- * 解析表达式
- *
- * 表达式格式: namespace:arg1.arg2.arg3.arg4.arg5==value
- * 返回: array($namespace, $param1, $operator, $param1)
- *
- * @param string $expression 待解析的表达式
- * @return array 返回解析后的表达式,由表达式的各项组成的数组:
- * <ul>
- * <li>第一个元素: 命名空间</li>
- * <li>第二个元素: 表达式的左边操作数</li>
- * <li>第三个元素: 表达式的操作符</li>
- * <li>第四个元素: 表达式的右边操作数</li>
- * </ul>
- */
- public static function resolveExpression($expression) {
- $operators = array('==', '!=', '<', '>', '<=', '>=');
- $operatorsReplace = array('#==#', '#!=#', '#<#', '#>#', '#<=#', '#>=#');
- list($p, $o, $v2) = explode('#', str_replace($operators, $operatorsReplace, $expression));
- if (strpos($p, ":") !== false)
- list($_namespace, $p) = explode(':', trim($p, ':'));
- else
- $_namespace = '';
- return array($_namespace, $p, $o, $v2);
- }
-
- /**
- * 执行简单的条件表达式
- *
- * 只能执行==、!=、<、>、<=、>=简单的比较
- *
- * @param string $v1 左边的操作数
- * @param string $v2 右边的操作数
- * @param string $operator 操作符号
- * @return boolean
- */
- public static function evalExpression($v1, $v2, $operator) {
- switch ($operator) {
- case '==':
- return $v1 == $v2;
- case '!=':
- return $v1 != $v2;
- case '<':
- return $v1 < $v2;
- case '>':
- return $v1 > $v2;
- case '<=':
- return $v1 <= $v2;
- case '>=':
- return $v1 >= $v2;
- default:
- return false;
- }
- return false;
- }
-
- /**
- * 递归合并两个数组
- *
- * @param array $array1 数组1
- * @param array $array2 数组2
- * @return array 合并后的数组
- */
- public static function mergeArray($array1, $array2) {
- foreach ($array2 as $key => $value) {
- if (empty($value)) {
- $array1[$key] = $value;
- } else if (!isset($array1[$key])) {
- $array1[$key] = $value;
- } elseif (is_array($array1[$key]) && is_array($value)) {
- $array1[$key] = self::mergeArray($array1[$key], $array2[$key]);
- } elseif (is_numeric($key) && $array1[$key] !== $array2[$key]) {
- $array1[] = $value;
- } else
- $array1[$key] = $value;
- }
- return $array1;
- }
-
- /**
- * 将字符串首字母小写
- *
- * @param string $str 待处理的字符串
- * @return string 返回处理后的字符串
- */
- public static function lcfirst($str) {
- $str[0] = strtolower($str[0]);
- return $str;
- }
-
- /**
- * 获得随机数字符串
- *
- * @param int $length 随机数的长度
- * @return string 随机获得的字串
- */
- public static function generateRandStr($length) {
- $randstr = "";
- for ($i = 0; $i < (int) $length; $i++) {
- $randnum = mt_rand(0, 61);
- if ($randnum < 10) {
- $randstr .= chr($randnum + 48);
- } else if ($randnum < 36) {
- $randstr .= chr($randnum + 55);
- } else {
- $randstr .= chr($randnum + 61);
- }
- }
- return $randstr;
- }
-
- /**
- * 通用组装测试验证规则
- *
- * @param string $field 验证字段名称
- * @param string $validator 验证方法
- * @param array $args 验证方法中传递的其他参数,需在待验证字段值的后面,默认为空数组
- * @param string $default 验证失败是设置的默认值,默认为null
- * @param string $message 验证失败是返回的错误信息,默认为空字串
- * @return array 返回验证规则
- * <ul>
- * <li>field: 验证字段名称</li>
- * <li>validator: 验证方法</li>
- * <li>args: 验证方法中传递的其他参数,需在待验证字段值的后面,缺省为空数组</li>
- * <li>default: 验证失败是设置的默认值,缺省为null</li>
- * <li>message: 验证失败是返回的错误信息,默认为'提示:XX验证失败'</li>
- * </ul>
- */
- public static function buildValidateRule($field, $validator, $args = array(), $default = null, $message = '') {
- return array(
- 'field' => $field,
- 'validator' => $validator,
- 'args' => (array) $args,
- 'default' => $default,
- 'message' => ($message ? $message : '提示:\'' . $field . '\'验证失败'));
- }
-
- /**
- * 对字符串中的参数进行替换
- *
- * 该函优化了php strtr()实现, 在进行数组方式的字符替换时支持了两种模式的字符替换:
- * @example<pre>
- * 1. echo WindUtility::strtr("I Love {you}",array('{you}' => 'lili'));
- * 结果: I Love lili
- * 2. echo WindUtility::strtr("I Love #0,#1",array('lili','qiong'));
- * 结果: I Love lili,qiong
- * <pre>
- * @see WindLangResource::getMessage()
- * @param string $str
- * @param string $from
- * @param string $to 可选参数,默认值为''
- * @return string
- */
- public static function strtr($str, $from, $to = '') {
- if (is_string($from)) return strtr($str, $from, $to);
- if (isset($from[0])) {
- foreach ($from as $key => $value) {
- $from['#' . $key] = $value;
- unset($from[$key]);
- }
- }
- return !empty($from) ? strtr($str, $from) : $str;
- }
-
- /**
- * 错误信息处理方法
- *
- * @param string $file
- * @param string $line
- * @param array $trace
- */
- public static function crash($file, $line, $trace) {
- $msg = '';
- $count = count($trace);
- $padLen = strlen($count);
- foreach ($trace as $key => $call) {
- if (!isset($call['file']) || $call['file'] == '') {
- $call['file'] = '~Internal Location~';
- $call['line'] = 'N/A';
- }
- $traceLine = '#' . str_pad(($count - $key), $padLen, "0", STR_PAD_LEFT) . ' ' . self::getCallLine(
- $call);
- $trace[$key] = $traceLine;
- }
- $fileLines = array();
- if (is_file($file)) {
- $currentLine = $line - 1;
- $fileLines = explode("\n", file_get_contents($file, null, null, 0, 10000000));
- $topLine = $currentLine - 5;
- $fileLines = array_slice($fileLines, $topLine > 0 ? $topLine : 0, 10, true);
- if (($count = count($fileLines)) > 0) {
- $padLen = strlen($count);
- foreach ($fileLines as $line => &$fileLine)
- $fileLine = " " . htmlspecialchars(
- str_pad($line + 1, $padLen, "0", STR_PAD_LEFT) . ": " . str_replace("\t",
- " ", rtrim($fileLine)), null, "UTF-8");
- }
- }
- return array($fileLines, $trace);
- }
-
- /**
- * @param array $call
- * @return string
- */
- private static function getCallLine($call) {
- $call_signature = "";
- if (isset($call['file'])) $call_signature .= $call['file'] . " ";
- if (isset($call['line'])) $call_signature .= "(" . $call['line'] . ") ";
- if (isset($call['function'])) {
- $call_signature .= $call['function'] . "(";
- if (isset($call['args'])) {
- foreach ($call['args'] as $arg) {
- if (is_string($arg))
- $arg = '"' . (strlen($arg) <= 64 ? $arg : substr($arg, 0, 64) . "…") . '"';
- else if (is_object($arg))
- $arg = "[Instance of '" . get_class($arg) . "']";
- else if ($arg === true)
- $arg = "true";
- else if ($arg === false)
- $arg = "false";
- else if ($arg === null)
- $arg = "null";
- else
- $arg = strval($arg);
- $call_signature .= $arg . ',';
- }
- }
- $call_signature = trim($call_signature, ',') . ")";
- }
- return $call_signature;
- }
- }