1 | <?php |
||
35 | class MicrosoftTranslator |
||
36 | { |
||
37 | /** |
||
38 | * |
||
39 | */ |
||
40 | const AUTH_URL = 'https://datamarket.accesscontrol.windows.net/v2/OAuth2-13'; |
||
41 | |||
42 | /** |
||
43 | * |
||
44 | */ |
||
45 | const BASE_URL = 'http://api.microsofttranslator.com/V2/Http.svc/'; |
||
46 | |||
47 | /** |
||
48 | * @var \GuzzleHttp\ClientInterface |
||
49 | */ |
||
50 | private $http; |
||
51 | |||
52 | /** |
||
53 | * @var string |
||
54 | */ |
||
55 | private $scope = 'http://api.microsofttranslator.com'; |
||
56 | |||
57 | /** |
||
58 | * @var string |
||
59 | */ |
||
60 | private $grantType = 'client_credentials'; |
||
61 | |||
62 | /** |
||
63 | * @var string |
||
64 | */ |
||
65 | private $clientId; |
||
66 | |||
67 | /** |
||
68 | * @var string |
||
69 | */ |
||
70 | private $clientSecret; |
||
71 | |||
72 | /** |
||
73 | * @var string |
||
74 | */ |
||
75 | private $accessToken; |
||
76 | |||
77 | /** |
||
78 | * MicrosoftTranslator constructor. |
||
79 | */ |
||
80 | 54 | public function __construct(\GuzzleHttp\ClientInterface $httpClient = null) |
|
88 | |||
89 | /** |
||
90 | * @param $id |
||
91 | * @param $secret |
||
92 | */ |
||
93 | 39 | public function setClient($id, $secret) |
|
98 | |||
99 | /** |
||
100 | * @return mixed |
||
101 | * @throws AuthException |
||
102 | */ |
||
103 | 48 | private function getAccessToken() |
|
126 | |||
127 | /** |
||
128 | * @param ApiMethodInterface $method |
||
129 | * @return \GuzzleHttp\Message\Request|\GuzzleHttp\Message\RequestInterface |
||
130 | * @throws AuthException |
||
131 | */ |
||
132 | 45 | private function createRequest(ApiMethodInterface $method) |
|
149 | |||
150 | /** |
||
151 | * @param ApiMethodInterface $method |
||
152 | * @return mixed |
||
153 | * @throws ArgumentException |
||
154 | * @throws QuotaExceededException |
||
155 | * @throws TranslatorException |
||
156 | */ |
||
157 | 45 | private function execute(ApiMethodInterface $method) |
|
176 | |||
177 | /** |
||
178 | * @param string $message |
||
179 | * @throws QuotaExceededException |
||
180 | */ |
||
181 | 6 | private function assertNoTranslateExceptionAndZeroBalance($message) |
|
189 | |||
190 | /** |
||
191 | * @param string $message |
||
192 | * @throws ArgumentException |
||
193 | * @throws TokenExpiredException |
||
194 | */ |
||
195 | 12 | private function assertNoArgumentException($message) |
|
205 | |||
206 | /** |
||
207 | * Converts a string of text from one language to another. |
||
208 | * |
||
209 | * @param $text string |
||
210 | * @param $to string|Language |
||
211 | * @param $from string|Language |
||
212 | * @param $contentType string |
||
213 | * @return string A string representing the translated text. |
||
214 | * |
||
215 | * @throws TranslatorException |
||
216 | */ |
||
217 | 18 | public function translate($text, $to, $from = null, $contentType = Translate::CONTENT_TYPE_PLAIN) |
|
221 | |||
222 | /** |
||
223 | * @param $text |
||
224 | * @return null|Language |
||
225 | * @throws TranslatorException |
||
226 | */ |
||
227 | 3 | public function detect($text) |
|
231 | |||
232 | /** |
||
233 | * @param $text |
||
234 | * @param $language |
||
235 | * @param string $format |
||
236 | * @param string $options |
||
237 | * @return mixed |
||
238 | * @throws TranslatorException |
||
239 | */ |
||
240 | 3 | public function speak($text, $language, $format = Speak::FORMAT_MP3, $options = Speak::OPTION_MAX_QUALITY) |
|
244 | |||
245 | /** |
||
246 | * @return Language[] |
||
247 | * @throws TranslatorException |
||
248 | */ |
||
249 | 3 | public function getLanguagesForSpeak() |
|
253 | |||
254 | /** |
||
255 | * @param $languageCodes |
||
256 | * @param string $locale |
||
257 | * @return mixed |
||
258 | * @throws TranslatorException |
||
259 | */ |
||
260 | 6 | public function getLanguageNames($languageCodes, $locale = Language::ENGLISH) |
|
264 | |||
265 | /** |
||
266 | * @return Language[] |
||
267 | * @throws TranslatorException |
||
268 | */ |
||
269 | 3 | public function getLanguagesForTranslate() |
|
273 | |||
274 | /** |
||
275 | * @param $text |
||
276 | * @param $to |
||
277 | * @param $from |
||
278 | * @param null $maxTranslations |
||
279 | * @param TranslateOptions|null $options |
||
280 | * @return GetTranslationsResponse |
||
281 | * @throws TranslatorException |
||
282 | */ |
||
283 | 6 | public function getTranslations($text, $to, $from, $maxTranslations = 5, TranslateOptions $options = null) |
|
287 | |||
288 | /** |
||
289 | * @param $texts |
||
290 | * @param $to |
||
291 | * @param $from |
||
292 | * @param int $maxTranslations |
||
293 | * @param TranslateOptions|null $options |
||
294 | * @return GetTranslationsResponse[] |
||
295 | * @throws TranslatorException |
||
296 | */ |
||
297 | 3 | public function getTranslationsArray($texts, $to, $from, $maxTranslations = 5, TranslateOptions $options = null) |
|
301 | } |
||
302 |