1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace icy2003\php\iapis\baidu; |
4
|
|
|
|
5
|
|
|
use icy2003\php\I; |
6
|
|
|
use icy2003\php\iapis\Api; |
7
|
|
|
use icy2003\php\ihelpers\Arrays; |
8
|
|
|
use icy2003\php\ihelpers\Http; |
9
|
|
|
use icy2003\php\ihelpers\Json; |
10
|
|
|
use icy2003\php\ihelpers\Strings; |
11
|
|
|
|
12
|
|
|
class Translation extends Api |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* 错误码列表 |
16
|
|
|
* |
17
|
|
|
* @var array |
18
|
|
|
*/ |
19
|
|
|
protected $_errorMap = [ |
20
|
|
|
'52000' => '成功', |
21
|
|
|
'52001' => '请求超时', |
22
|
|
|
'52002' => '系统错误', |
23
|
|
|
'52003' => '未授权用户', |
24
|
|
|
'54000' => '必填参数为空', |
25
|
|
|
'54001' => '签名错误', |
26
|
|
|
'54003' => '访问频率受限', |
27
|
|
|
'54004' => '账户余额不足', |
28
|
|
|
'54005' => '长 query 频繁请求', |
29
|
|
|
'58000' => '客户端 IP 非法', |
30
|
|
|
'58001' => '译文语言方向不支持', |
31
|
|
|
'58002' => '服务当前关闭', |
32
|
|
|
'90107' => '认证未通过或未生效', |
33
|
|
|
]; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* APP ID |
37
|
|
|
* |
38
|
|
|
* @link https://api.fanyi.baidu.com/api/trans/product/desktop?req=developer |
39
|
|
|
* |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
protected $_appid; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* 密钥 |
46
|
|
|
* |
47
|
|
|
* @link https://api.fanyi.baidu.com/api/trans/product/desktop?req=developer |
48
|
|
|
* |
49
|
|
|
* @var string |
50
|
|
|
*/ |
51
|
|
|
protected $_secretKey; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* 初始化 |
55
|
|
|
* |
56
|
|
|
* @param string $appid |
57
|
|
|
* @param string $secretKey |
58
|
|
|
*/ |
59
|
|
|
public function __construct($appid, $secretKey) |
60
|
|
|
{ |
61
|
|
|
$this->_appid = $appid; |
62
|
|
|
$this->_secretKey = $secretKey; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* 通用翻译 API |
67
|
|
|
* |
68
|
|
|
* @param string $text |
69
|
|
|
* @param string $to |
70
|
|
|
* |
71
|
|
|
* @return static |
72
|
|
|
*/ |
73
|
|
|
public function general($text, $to) |
74
|
|
|
{ |
75
|
|
|
$salt = Strings::random(); |
76
|
|
|
$this->_result = Json::decode(Http::get('http://api.fanyi.baidu.com/api/trans/vip/translate', [ |
|
|
|
|
77
|
|
|
'q' => $text, |
78
|
|
|
'from' => 'auto', |
79
|
|
|
'to' => $to, |
80
|
|
|
'appid' => $this->_appid, |
81
|
|
|
'salt' => $salt, |
82
|
|
|
'sign' => md5($this->_appid . $text . $salt . $this->_secretKey), |
83
|
|
|
])); |
84
|
|
|
$this->_toArrayCall = function ($array) { |
85
|
|
|
$trans = I::get($array, 'trans_result', []); |
86
|
|
|
return Arrays::column($trans, 'dst'); |
|
|
|
|
87
|
|
|
}; |
88
|
|
|
|
89
|
|
|
return $this; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @ignore |
94
|
|
|
* |
95
|
|
|
* @return boolean |
96
|
|
|
*/ |
97
|
|
|
public function isSuccess() |
98
|
|
|
{ |
99
|
|
|
if ($this->_result['error_code'] == 52000) { |
100
|
|
|
return true; |
101
|
|
|
} |
102
|
|
|
return false; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @ignore |
107
|
|
|
* |
108
|
|
|
* @return string |
109
|
|
|
*/ |
110
|
|
|
public function getError() |
111
|
|
|
{ |
112
|
|
|
return I::get($this->_errorMap, $this->_result['error_code'], $this->_result['error_msg']); |
|
|
|
|
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.