1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BabyMarkt\DeepL; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* DeepL API client library |
7
|
|
|
* |
8
|
|
|
* @package BabyMarkt\DeepL |
9
|
|
|
*/ |
10
|
|
|
class DeepL |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* API v1 URL |
14
|
|
|
*/ |
15
|
|
|
const API_URL_V1 = 'https://api.deepl.com/v1/translate'; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* API v2 URL |
19
|
|
|
*/ |
20
|
|
|
const API_URL_V2 = 'https://api.deepl.com/v2/translate'; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* API URL: Parameter auth_key |
24
|
|
|
*/ |
25
|
|
|
const API_URL_AUTH_KEY = 'auth_key=%s'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* API URL: Parameter text |
29
|
|
|
*/ |
30
|
|
|
const API_URL_TEXT = 'text=%s'; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* API URL: Parameter source_lang |
34
|
|
|
*/ |
35
|
|
|
const API_URL_SOURCE_LANG = 'source_lang=%s'; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* API URL: Parameter target_lang |
39
|
|
|
*/ |
40
|
|
|
const API_URL_DESTINATION_LANG = 'target_lang=%s'; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* API URL: Parameter tag_handling |
44
|
|
|
*/ |
45
|
|
|
const API_URL_TAG_HANDLING = 'tag_handling=%s'; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* API URL: Parameter ignore_tags |
49
|
|
|
*/ |
50
|
|
|
const API_URL_IGNORE_TAGS = 'ignore_tags=%s'; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* API URL: Parameter formality |
54
|
|
|
*/ |
55
|
|
|
const API_URL_FORMALITY = 'formality=%s'; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* DeepL HTTP error codes |
59
|
|
|
* |
60
|
|
|
* @var array |
61
|
|
|
*/ |
62
|
|
|
protected $errorCodes = array( |
63
|
|
|
400 => 'Wrong request, please check error message and your parameters.', |
64
|
|
|
403 => 'Authorization failed. Please supply a valid auth_key parameter.', |
65
|
|
|
413 => 'Request Entity Too Large. The request size exceeds the current limit.', |
66
|
|
|
429 => 'Too many requests. Please wait and send your request once again.', |
67
|
|
|
456 => 'Quota exceeded. The character limit has been reached.' |
68
|
|
|
); |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Supported translation source languages |
72
|
|
|
* |
73
|
|
|
* @var array |
74
|
|
|
*/ |
75
|
|
|
protected $sourceLanguages = array( |
76
|
|
|
'EN', |
77
|
|
|
'DE', |
78
|
|
|
'FR', |
79
|
|
|
'ES', |
80
|
|
|
'PT', |
81
|
|
|
'IT', |
82
|
|
|
'NL', |
83
|
|
|
'PL', |
84
|
|
|
'RU', |
85
|
|
|
'JA', |
86
|
|
|
'ZH' |
87
|
|
|
); |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Supported translation destination languages |
91
|
|
|
* |
92
|
|
|
* @var array |
93
|
|
|
*/ |
94
|
|
|
protected $destinationLanguages = array( |
95
|
|
|
'EN', |
96
|
|
|
'DE', |
97
|
|
|
'FR', |
98
|
|
|
'ES', |
99
|
|
|
'PT', |
100
|
|
|
'PT-PT', |
101
|
|
|
'PT-BR', |
102
|
|
|
'IT', |
103
|
|
|
'NL', |
104
|
|
|
'PL', |
105
|
|
|
'RU', |
106
|
|
|
'JA', |
107
|
|
|
'ZH' |
108
|
|
|
); |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @var integer |
112
|
|
|
*/ |
113
|
|
|
protected $apiVersion; |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* DeepL API Auth Key (DeepL Pro access required) |
117
|
|
|
* |
118
|
|
|
* @var string |
119
|
|
|
*/ |
120
|
|
|
protected $authKey; |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* cURL resource |
124
|
|
|
* |
125
|
|
|
* @var resource |
126
|
|
|
*/ |
127
|
|
|
protected $curl; |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* DeepL constructor |
131
|
|
|
* |
132
|
|
|
* @param string $authKey |
133
|
|
|
* @param integer $apiVersion |
134
|
|
|
*/ |
135
|
9 |
|
public function __construct($authKey, $apiVersion = 2) |
136
|
|
|
{ |
137
|
9 |
|
$this->authKey = $authKey; |
138
|
9 |
|
$this->apiVersion = $apiVersion; |
139
|
9 |
|
$this->curl = curl_init(); |
|
|
|
|
140
|
|
|
|
141
|
9 |
|
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, 1); |
|
|
|
|
142
|
9 |
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* DeepL destructor |
146
|
|
|
*/ |
147
|
9 |
|
public function __destruct() |
148
|
|
|
{ |
149
|
9 |
|
if ($this->curl && is_resource($this->curl)) { |
150
|
9 |
|
curl_close($this->curl); |
151
|
|
|
} |
152
|
9 |
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Translate the text string or array from source to destination language |
156
|
|
|
* |
157
|
|
|
* @param string|string[] $text |
158
|
|
|
* @param string $sourceLanguage |
159
|
|
|
* @param string $destinationLanguage |
160
|
|
|
* @param array $tagHandling |
161
|
|
|
* @param array $ignoreTags |
162
|
|
|
* @param string $formality |
163
|
|
|
* |
164
|
|
|
* @return string|string[] |
165
|
|
|
* |
166
|
|
|
* @throws DeepLException |
167
|
|
|
*/ |
168
|
1 |
|
public function translate( |
169
|
|
|
$text, |
170
|
|
|
$sourceLanguage = 'de', |
171
|
|
|
$destinationLanguage = 'en', |
172
|
|
|
array $tagHandling = array(), |
173
|
|
|
array $ignoreTags = array(), |
174
|
|
|
$formality = "default" |
175
|
|
|
) { |
176
|
|
|
// make sure we only accept supported languages |
177
|
1 |
|
$this->checkLanguages($sourceLanguage, $destinationLanguage); |
178
|
|
|
|
179
|
|
|
// build the DeepL API request url |
180
|
1 |
|
$url = $this->buildUrl($sourceLanguage, $destinationLanguage, $tagHandling, $ignoreTags, $formality); |
181
|
1 |
|
$body = $this->buildBody($text); |
182
|
|
|
|
183
|
|
|
// request the DeepL API |
184
|
1 |
|
$translationsArray = $this->request($url, $body); |
185
|
|
|
$translationsCount = count($translationsArray['translations']); |
186
|
|
|
|
187
|
|
|
if ($translationsCount == 0) { |
188
|
|
|
throw new DeepLException('No translations found.'); |
189
|
|
|
} elseif ($translationsCount == 1) { |
190
|
|
|
return $translationsArray['translations'][0]['text']; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
return $translationsArray['translations']; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Check if the given languages are supported |
198
|
|
|
* |
199
|
|
|
* @param string $sourceLanguage |
200
|
|
|
* @param string $destinationLanguage |
201
|
|
|
* |
202
|
|
|
* @return boolean |
203
|
|
|
* |
204
|
|
|
* @throws DeepLException |
205
|
|
|
*/ |
206
|
4 |
|
protected function checkLanguages($sourceLanguage, $destinationLanguage) |
207
|
|
|
{ |
208
|
4 |
|
$sourceLanguage = strtoupper($sourceLanguage); |
209
|
|
|
|
210
|
4 |
|
if (!in_array($sourceLanguage, $this->sourceLanguages)) { |
211
|
1 |
|
throw new DeepLException( |
212
|
1 |
|
sprintf('The language "%s" is not supported as source language.', $sourceLanguage) |
213
|
|
|
); |
214
|
|
|
} |
215
|
|
|
|
216
|
3 |
|
$destinationLanguage = strtoupper($destinationLanguage); |
217
|
|
|
|
218
|
3 |
|
if (!in_array($destinationLanguage, $this->destinationLanguages)) { |
219
|
1 |
|
throw new DeepLException( |
220
|
1 |
|
sprintf('The language "%s" is not supported as destination language.', $destinationLanguage) |
221
|
|
|
); |
222
|
|
|
} |
223
|
|
|
|
224
|
2 |
|
return true; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* Build the URL for the DeepL API request |
229
|
|
|
* |
230
|
|
|
* @param string $sourceLanguage |
231
|
|
|
* @param string $destinationLanguage |
232
|
|
|
* @param array $tagHandling |
233
|
|
|
* @param array $ignoreTags |
234
|
|
|
* @param string $formality |
235
|
|
|
* |
236
|
|
|
* @return string |
237
|
|
|
*/ |
238
|
5 |
|
protected function buildUrl( |
239
|
|
|
$sourceLanguage, |
240
|
|
|
$destinationLanguage, |
241
|
|
|
array $tagHandling = array(), |
242
|
|
|
array $ignoreTags = array(), |
243
|
|
|
$formality = "default" |
244
|
|
|
) { |
245
|
|
|
// select correct api url |
246
|
5 |
|
switch ($this->apiVersion) { |
247
|
5 |
|
case 1: |
248
|
2 |
|
$url = DeepL::API_URL_V1; |
249
|
2 |
|
break; |
250
|
3 |
|
case 2: |
251
|
3 |
|
$url = DeepL::API_URL_V2; |
252
|
3 |
|
break; |
253
|
|
|
default: |
254
|
|
|
$url = DeepL::API_URL_V2; |
255
|
|
|
} |
256
|
|
|
|
257
|
5 |
|
$url .= '?' . sprintf(DeepL::API_URL_AUTH_KEY, $this->authKey); |
258
|
5 |
|
$url .= '&' . sprintf(DeepL::API_URL_SOURCE_LANG, strtolower($sourceLanguage)); |
259
|
5 |
|
$url .= '&' . sprintf(DeepL::API_URL_DESTINATION_LANG, strtolower($destinationLanguage)); |
260
|
|
|
|
261
|
5 |
|
if (!empty($tagHandling)) { |
262
|
2 |
|
$url .= '&' . sprintf(DeepL::API_URL_TAG_HANDLING, implode(',', $tagHandling)); |
263
|
|
|
} |
264
|
|
|
|
265
|
5 |
|
if (!empty($ignoreTags)) { |
266
|
2 |
|
$url .= '&' . sprintf(DeepL::API_URL_IGNORE_TAGS, implode(',', $ignoreTags)); |
267
|
|
|
} |
268
|
|
|
|
269
|
5 |
|
if (!empty($formality)) { |
270
|
5 |
|
$url .= '&' . sprintf(DeepL::API_URL_FORMALITY, $formality); |
271
|
|
|
} |
272
|
|
|
|
273
|
5 |
|
return $url; |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
/** |
277
|
|
|
* Build the body for the DeepL API request |
278
|
|
|
* |
279
|
|
|
* @param string|string[] $text |
280
|
|
|
* |
281
|
|
|
* @return string |
282
|
|
|
*/ |
283
|
2 |
|
protected function buildBody($text) |
284
|
|
|
{ |
285
|
2 |
|
$body = ''; |
286
|
2 |
|
$first = true; |
287
|
|
|
|
288
|
2 |
|
if (!is_array($text)) { |
289
|
2 |
|
$text = (array)$text; |
290
|
|
|
} |
291
|
|
|
|
292
|
2 |
|
foreach ($text as $textElement) { |
293
|
2 |
|
$body .= ($first ? '' : '&') . sprintf(DeepL::API_URL_TEXT, rawurlencode($textElement)); |
294
|
|
|
|
295
|
2 |
|
if ($first) { |
296
|
2 |
|
$first = false; |
297
|
|
|
} |
298
|
|
|
} |
299
|
|
|
|
300
|
2 |
|
return $body; |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
/** |
304
|
|
|
* Make a request to the given URL |
305
|
|
|
* |
306
|
|
|
* @param string $url |
307
|
|
|
* |
308
|
|
|
* @return array |
309
|
|
|
* |
310
|
|
|
* @throws DeepLException |
311
|
|
|
*/ |
312
|
1 |
|
protected function request($url, $body) |
313
|
|
|
{ |
314
|
1 |
|
curl_setopt($this->curl, CURLOPT_URL, $url); |
315
|
1 |
|
curl_setopt($this->curl, CURLOPT_POSTFIELDS, $body); |
316
|
1 |
|
curl_setopt($this->curl, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded')); |
317
|
|
|
|
318
|
1 |
|
$response = curl_exec($this->curl); |
319
|
|
|
|
320
|
1 |
|
if (curl_errno($this->curl)) { |
321
|
|
|
throw new DeepLException('There was a cURL Request Error.'); |
322
|
|
|
} |
323
|
|
|
|
324
|
1 |
|
$httpCode = curl_getinfo($this->curl, CURLINFO_HTTP_CODE); |
325
|
|
|
|
326
|
1 |
|
if ($httpCode != 200 && array_key_exists($httpCode, $this->errorCodes)) { |
327
|
1 |
|
throw new DeepLException($this->errorCodes[$httpCode], $httpCode); |
328
|
|
|
} |
329
|
|
|
|
330
|
|
|
$translationsArray = json_decode($response, true); |
331
|
|
|
|
332
|
|
|
if (!$translationsArray) { |
333
|
|
|
throw new DeepLException('The Response seems to not be valid JSON.'); |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
return $translationsArray; |
337
|
|
|
} |
338
|
|
|
} |
339
|
|
|
|
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.