Conditions | 3 |
Paths | 3 |
Total Lines | 22 |
Code Lines | 13 |
Lines | 0 |
Ratio | 0 % |
Tests | 12 |
CRAP Score | 3 |
Changes | 2 | ||
Bugs | 1 | Features | 0 |
1 | <?php |
||
22 | 4 | public function translate(array $texts): array |
|
23 | { |
||
24 | 4 | $newTexts = []; |
|
25 | |||
26 | // TODO change chunk size to real character count limit (https://social.microsoft.com/Forums/en-US/abf2a48f-d8c7-41db-a1fa-00066e7040f4/limits-in-request-to-bing-translator-api?forum=translator) |
||
27 | 4 | foreach (array_chunk($texts, $this->chunkSize) as $strings) { |
|
28 | 4 | $guzzleClient = new Client(); |
|
29 | $options = [ |
||
30 | 'form_params' => [ |
||
31 | 4 | 'text' => implode(' | ', $strings), |
|
32 | 4 | 'fromLang' => $this->from, |
|
33 | 4 | 'to' => $this->to, |
|
34 | ] |
||
35 | ]; |
||
36 | 4 | $request = $guzzleClient->request('POST', 'https://www.bing.com/ttranslatev3', $options); |
|
37 | |||
38 | 4 | $response = json_decode((string) $request->getBody(), true); |
|
39 | 4 | if ($request->getStatusCode() === 200) { |
|
40 | 4 | $newTexts = array_merge($newTexts, array_map('trim', explode('|', $response[0]['translations'][0]['text']))); |
|
41 | } |
||
42 | } |
||
43 | 4 | return array_combine($texts, $newTexts); |
|
44 | } |
||
46 |