1 | <?php |
||
34 | class MicrosoftTranslator |
||
35 | { |
||
36 | /** |
||
37 | * |
||
38 | */ |
||
39 | const AUTH_URL = 'https://datamarket.accesscontrol.windows.net/v2/OAuth2-13'; |
||
40 | |||
41 | /** |
||
42 | * |
||
43 | */ |
||
44 | const BASE_URL = 'http://api.microsofttranslator.com/V2/Http.svc/'; |
||
45 | |||
46 | /** |
||
47 | * @var \GuzzleHttp\ClientInterface |
||
48 | */ |
||
49 | private $http; |
||
50 | |||
51 | /** |
||
52 | * @var string |
||
53 | */ |
||
54 | private $scope = 'http://api.microsofttranslator.com'; |
||
55 | |||
56 | /** |
||
57 | * @var string |
||
58 | */ |
||
59 | private $grantType = 'client_credentials'; |
||
60 | |||
61 | /** |
||
62 | * @var string |
||
63 | */ |
||
64 | private $clientId; |
||
65 | |||
66 | /** |
||
67 | * @var string |
||
68 | */ |
||
69 | private $clientSecret; |
||
70 | |||
71 | /** |
||
72 | * @var string |
||
73 | */ |
||
74 | private $accessToken; |
||
75 | |||
76 | /** |
||
77 | * MicrosoftTranslator constructor. |
||
78 | */ |
||
79 | 48 | public function __construct(\GuzzleHttp\ClientInterface $httpClient = null) |
|
87 | |||
88 | /** |
||
89 | * @param $id |
||
90 | * @param $secret |
||
91 | */ |
||
92 | 33 | public function setClient($id, $secret) |
|
97 | |||
98 | /** |
||
99 | * @return mixed |
||
100 | * @throws AuthException |
||
101 | */ |
||
102 | 42 | private function getAccessToken() |
|
125 | |||
126 | /** |
||
127 | * @param ApiMethodInterface $method |
||
128 | * @return \GuzzleHttp\Message\Request|\GuzzleHttp\Message\RequestInterface |
||
129 | * @throws AuthException |
||
130 | */ |
||
131 | 39 | private function createRequest(ApiMethodInterface $method) |
|
148 | |||
149 | /** |
||
150 | * @param ApiMethodInterface $method |
||
151 | * @return mixed |
||
152 | * @throws ArgumentException |
||
153 | * @throws QuotaExceededException |
||
154 | * @throws TranslatorException |
||
155 | */ |
||
156 | 39 | private function execute(ApiMethodInterface $method) |
|
175 | |||
176 | /** |
||
177 | * @param string $message |
||
178 | * @throws QuotaExceededException |
||
179 | */ |
||
180 | 6 | private function assertNoTranslateExceptionAndZeroBalance($message) |
|
188 | |||
189 | /** |
||
190 | * @param string $message |
||
191 | * @throws ArgumentException |
||
192 | * @throws TokenExpiredException |
||
193 | */ |
||
194 | 12 | private function assertNoArgumentException($message) |
|
204 | |||
205 | /** |
||
206 | * @param $text |
||
207 | * @param $to |
||
208 | * @param $from |
||
209 | * @return null|string |
||
210 | */ |
||
211 | 15 | public function translate($text, $to, $from = null) |
|
215 | |||
216 | /** |
||
217 | * @param $text |
||
218 | * @return null|Language |
||
219 | * @throws TranslatorException |
||
220 | */ |
||
221 | 3 | public function detect($text) |
|
225 | |||
226 | /** |
||
227 | * @param $text |
||
228 | * @param $language |
||
229 | * @param string $format |
||
230 | * @param string $options |
||
231 | * @return mixed |
||
232 | * @throws TranslatorException |
||
233 | */ |
||
234 | 3 | public function speak($text, $language, $format = Speak::FORMAT_MP3, $options = Speak::OPTION_MAX_QUALITY) |
|
238 | |||
239 | /** |
||
240 | * @return Language[] |
||
241 | * @throws TranslatorException |
||
242 | */ |
||
243 | 3 | public function getLanguagesForSpeak() |
|
247 | |||
248 | /** |
||
249 | * @param $languageCodes |
||
250 | * @param string $locale |
||
251 | * @return mixed |
||
252 | * @throws TranslatorException |
||
253 | */ |
||
254 | 6 | public function getLanguageNames($languageCodes, $locale = Language::ENGLISH) |
|
258 | |||
259 | /** |
||
260 | * @return Language[] |
||
261 | * @throws TranslatorException |
||
262 | */ |
||
263 | 3 | public function getLanguagesForTranslate() |
|
267 | |||
268 | /** |
||
269 | * @param $text |
||
270 | * @param $to |
||
271 | * @param $from |
||
272 | * @param null $maxTranslations |
||
273 | * @param TranslateOptions|null $options |
||
274 | * @return mixed |
||
275 | * @throws TranslatorException |
||
276 | */ |
||
277 | 6 | public function getTranslations($text, $to, $from, $maxTranslations = 5, TranslateOptions $options = null) |
|
281 | } |