Skip to content

Commit 9a3e37e

Browse files
1. Common.php新增isWin、getNonceByURandom、extendArrayData通用方法
1 parent 7f53d12 commit 9a3e37e

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

src/Common.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,19 @@ public static function httpRequest($url, $data = null)
3030
return $output;
3131
}
3232

33+
/**
34+
* 是否为windows系统
35+
* @return bool
36+
*/
37+
public static function isWin()
38+
{
39+
if (strtoupper(substr(PHP_OS,0,3)) === 'WIN') {
40+
return true;
41+
} else {
42+
return false;
43+
}
44+
}
45+
3346
/**
3447
* 获取字符串
3548
* @param int $type
@@ -58,6 +71,50 @@ public static function getNonce($type = 1, $length = 16)
5871
return $nonce;
5972
}
6073

74+
/**
75+
* 根据URandom获取字符串
76+
* @param int $length
77+
* @return bool|string
78+
*/
79+
public static function getNonceByURandom($length = 16)
80+
{
81+
$nonce = '';
82+
$binary = '';
83+
$fp = @fopen('/dev/urandom', 'rb');
84+
if ($fp !== false) {
85+
$binary .= @fread($fp, $length);
86+
@fclose($fp);
87+
} else {
88+
trigger_error('Can not open /dev/urandom.');
89+
}
90+
$string = base64_encode($binary);
91+
$nonce = substr($string, 0, $length);
92+
return $nonce;
93+
}
94+
95+
/**
96+
* 扩展原数组的数据(仅更新原数组已有的键)
97+
* @param $rawData
98+
* @param $newData
99+
* @return array
100+
*/
101+
public static function extendArrayData($rawData, $newData)
102+
{
103+
if (!is_array($rawData) || count($rawData) <= 0) {
104+
return array();
105+
}
106+
107+
if (!is_array($newData)) {
108+
return $rawData;
109+
}
110+
111+
foreach ($rawData as $key=>$value) {
112+
isset($newData[$key]) && $rawData[$key] = $newData[$key];
113+
}
114+
115+
return $rawData;
116+
}
117+
61118
/**
62119
* 根据错误码获取错误信息
63120
* @param $errorCode

0 commit comments

Comments
 (0)