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 | 51 | public function __construct(\GuzzleHttp\ClientInterface $httpClient = null) |
|
88 | |||
89 | /** |
||
90 | * @param $id |
||
91 | * @param $secret |
||
92 | */ |
||
93 | 36 | public function setClient($id, $secret) |
|
98 | |||
99 | /** |
||
100 | * @return mixed |
||
101 | * @throws AuthException |
||
102 | */ |
||
103 | 45 | private function getAccessToken() |
|
126 | |||
127 | /** |
||
128 | * @param ApiMethodInterface $method |
||
129 | * @return \GuzzleHttp\Message\Request|\GuzzleHttp\Message\RequestInterface |
||
130 | * @throws AuthException |
||
131 | */ |
||
132 | 42 | 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 | 42 | 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 | * @param $text |
||
208 | * @param $to |
||
209 | * @param $from |
||
210 | * @return null|string |
||
211 | */ |
||
212 | 15 | public function translate($text, $to, $from = null) |
|
216 | |||
217 | /** |
||
218 | * @param $text |
||
219 | * @return null|Language |
||
220 | * @throws TranslatorException |
||
221 | */ |
||
222 | 3 | public function detect($text) |
|
226 | |||
227 | /** |
||
228 | * @param $text |
||
229 | * @param $language |
||
230 | * @param string $format |
||
231 | * @param string $options |
||
232 | * @return mixed |
||
233 | * @throws TranslatorException |
||
234 | */ |
||
235 | 3 | public function speak($text, $language, $format = Speak::FORMAT_MP3, $options = Speak::OPTION_MAX_QUALITY) |
|
239 | |||
240 | /** |
||
241 | * @return Language[] |
||
242 | * @throws TranslatorException |
||
243 | */ |
||
244 | 3 | public function getLanguagesForSpeak() |
|
248 | |||
249 | /** |
||
250 | * @param $languageCodes |
||
251 | * @param string $locale |
||
252 | * @return mixed |
||
253 | * @throws TranslatorException |
||
254 | */ |
||
255 | 6 | public function getLanguageNames($languageCodes, $locale = Language::ENGLISH) |
|
259 | |||
260 | /** |
||
261 | * @return Language[] |
||
262 | * @throws TranslatorException |
||
263 | */ |
||
264 | 3 | public function getLanguagesForTranslate() |
|
268 | |||
269 | /** |
||
270 | * @param $text |
||
271 | * @param $to |
||
272 | * @param $from |
||
273 | * @param null $maxTranslations |
||
274 | * @param TranslateOptions|null $options |
||
275 | * @return GetTranslationsResponse |
||
276 | * @throws TranslatorException |
||
277 | */ |
||
278 | 6 | public function getTranslations($text, $to, $from, $maxTranslations = 5, TranslateOptions $options = null) |
|
282 | |||
283 | /** |
||
284 | * @param $texts |
||
285 | * @param $to |
||
286 | * @param $from |
||
287 | * @param int $maxTranslations |
||
288 | * @param TranslateOptions|null $options |
||
289 | * @return GetTranslationsResponse[] |
||
290 | * @throws TranslatorException |
||
291 | */ |
||
292 | 3 | public function getTranslationsArray($texts, $to, $from, $maxTranslations = 5, TranslateOptions $options = null) |
|
296 | } |