1 | <?php |
||
32 | class MicrosoftTranslator |
||
33 | { |
||
34 | /** |
||
35 | * |
||
36 | */ |
||
37 | const AUTH_URL = 'https://datamarket.accesscontrol.windows.net/v2/OAuth2-13'; |
||
38 | |||
39 | /** |
||
40 | * |
||
41 | */ |
||
42 | const BASE_URL = 'http://api.microsofttranslator.com/V2/Http.svc/'; |
||
43 | |||
44 | /** |
||
45 | * @var \GuzzleHttp\ClientInterface |
||
46 | */ |
||
47 | private $http; |
||
48 | |||
49 | /** |
||
50 | * @var string |
||
51 | */ |
||
52 | private $scope = 'http://api.microsofttranslator.com'; |
||
53 | |||
54 | /** |
||
55 | * @var string |
||
56 | */ |
||
57 | private $grantType = 'client_credentials'; |
||
58 | |||
59 | /** |
||
60 | * @var string |
||
61 | */ |
||
62 | private $clientId; |
||
63 | |||
64 | /** |
||
65 | * @var string |
||
66 | */ |
||
67 | private $clientSecret; |
||
68 | |||
69 | /** |
||
70 | * @var string |
||
71 | */ |
||
72 | private $accessToken; |
||
73 | |||
74 | /** |
||
75 | * MicrosoftTranslator constructor. |
||
76 | */ |
||
77 | 42 | public function __construct(\GuzzleHttp\ClientInterface $httpClient = null) |
|
85 | |||
86 | /** |
||
87 | * @param $id |
||
88 | * @param $secret |
||
89 | */ |
||
90 | 27 | public function setClient($id, $secret) |
|
95 | |||
96 | /** |
||
97 | * @return mixed |
||
98 | * @throws AuthException |
||
99 | */ |
||
100 | 36 | private function getAccessToken() |
|
123 | |||
124 | /** |
||
125 | * @param ApiMethodInterface $method |
||
126 | * @return \GuzzleHttp\Message\Request|\GuzzleHttp\Message\RequestInterface |
||
127 | * @throws AuthException |
||
128 | */ |
||
129 | 33 | private function createRequest(ApiMethodInterface $method) |
|
146 | |||
147 | /** |
||
148 | * @param ApiMethodInterface $method |
||
149 | * @return mixed |
||
150 | * @throws ArgumentException |
||
151 | * @throws QuotaExceededException |
||
152 | * @throws TranslatorException |
||
153 | */ |
||
154 | 33 | private function execute(ApiMethodInterface $method) |
|
173 | |||
174 | /** |
||
175 | * @param string $message |
||
176 | * @throws QuotaExceededException |
||
177 | */ |
||
178 | 6 | private function assertNoTranslateExceptionAndZeroBalance($message) |
|
186 | |||
187 | /** |
||
188 | * @param string $message |
||
189 | * @throws ArgumentException |
||
190 | * @throws TokenExpiredException |
||
191 | */ |
||
192 | 12 | private function assertNoArgumentException($message) |
|
202 | |||
203 | /** |
||
204 | * @param $text |
||
205 | * @param $to |
||
206 | * @param $from |
||
207 | * @return null|string |
||
208 | */ |
||
209 | 15 | public function translate($text, $to, $from = null) |
|
213 | |||
214 | /** |
||
215 | * @param $text |
||
216 | * @return null|Language |
||
217 | * @throws TranslatorException |
||
218 | */ |
||
219 | 3 | public function detect($text) |
|
223 | |||
224 | /** |
||
225 | * @param $text |
||
226 | * @param $language |
||
227 | * @param string $format |
||
228 | * @param string $options |
||
229 | * @return mixed |
||
230 | * @throws TranslatorException |
||
231 | */ |
||
232 | 3 | public function speak($text, $language, $format = Speak::FORMAT_MP3, $options = Speak::OPTION_MAX_QUALITY) |
|
236 | |||
237 | /** |
||
238 | * @return Language[] |
||
239 | * @throws TranslatorException |
||
240 | */ |
||
241 | 3 | public function getLanguagesForSpeak() |
|
245 | |||
246 | /** |
||
247 | * @param $languageCodes |
||
248 | * @param string $locale |
||
249 | * @return mixed |
||
250 | * @throws TranslatorException |
||
251 | */ |
||
252 | 6 | public function getLanguageNames($languageCodes, $locale = Language::ENGLISH) |
|
256 | |||
257 | /** |
||
258 | * @return Language[] |
||
259 | * @throws TranslatorException |
||
260 | */ |
||
261 | 3 | public function getLanguagesForTranslate() |
|
265 | } |