1 | <?php |
||
31 | class MicrosoftTranslator |
||
32 | { |
||
33 | /** |
||
34 | * |
||
35 | */ |
||
36 | const AUTH_URL = 'https://datamarket.accesscontrol.windows.net/v2/OAuth2-13'; |
||
37 | |||
38 | /** |
||
39 | * |
||
40 | */ |
||
41 | const BASE_URL = 'http://api.microsofttranslator.com/V2/Http.svc/'; |
||
42 | |||
43 | /** |
||
44 | * @var \GuzzleHttp\ClientInterface |
||
45 | */ |
||
46 | private $http; |
||
47 | |||
48 | /** |
||
49 | * @var string |
||
50 | */ |
||
51 | private $scope = 'http://api.microsofttranslator.com'; |
||
52 | |||
53 | /** |
||
54 | * @var string |
||
55 | */ |
||
56 | private $grantType = 'client_credentials'; |
||
57 | |||
58 | /** |
||
59 | * @var string |
||
60 | */ |
||
61 | private $clientId; |
||
62 | |||
63 | /** |
||
64 | * @var string |
||
65 | */ |
||
66 | private $clientSecret; |
||
67 | |||
68 | /** |
||
69 | * @var string |
||
70 | */ |
||
71 | private $accessToken; |
||
72 | |||
73 | /** |
||
74 | * MicrosoftTranslator constructor. |
||
75 | */ |
||
76 | 39 | public function __construct(\GuzzleHttp\ClientInterface $httpClient = null) |
|
77 | { |
||
78 | 39 | if (is_null($httpClient)) { |
|
79 | 3 | $httpClient = new \GuzzleHttp\Client(); |
|
80 | 3 | } |
|
81 | |||
82 | 39 | $this->http = $httpClient; |
|
83 | 39 | } |
|
84 | |||
85 | /** |
||
86 | * @param $id |
||
87 | * @param $secret |
||
88 | */ |
||
89 | 24 | public function setClient($id, $secret) |
|
90 | { |
||
91 | 24 | $this->clientId = $id; |
|
92 | 24 | $this->clientSecret = $secret; |
|
93 | 24 | } |
|
94 | |||
95 | /** |
||
96 | * @return mixed |
||
97 | * @throws AuthException |
||
98 | */ |
||
99 | 33 | private function getAccessToken() |
|
122 | |||
123 | /** |
||
124 | * @param ApiMethodInterface $method |
||
125 | * @return \GuzzleHttp\Message\Request|\GuzzleHttp\Message\RequestInterface |
||
126 | * @throws AuthException |
||
127 | */ |
||
128 | 30 | private function createRequest(ApiMethodInterface $method) |
|
145 | |||
146 | /** |
||
147 | * @param ApiMethodInterface $method |
||
148 | * @return mixed |
||
149 | * @throws ArgumentException |
||
150 | * @throws QuotaExceededException |
||
151 | * @throws TranslatorException |
||
152 | */ |
||
153 | 30 | private function execute(ApiMethodInterface $method) |
|
154 | { |
||
155 | 30 | $response = $this->http->send($this->createRequest($method)); |
|
156 | |||
157 | 30 | if ($response->getStatusCode() != 200) { |
|
158 | try { |
||
159 | 12 | $message = strip_tags($response->getBody()); |
|
160 | 12 | $this->assertNoArgumentException($message); |
|
161 | 6 | $this->assertNoTranslateExceptionAndZeroBalance($message); |
|
162 | 12 | } catch (TokenExpiredException $e) { |
|
163 | 3 | $this->accessToken = null; |
|
164 | 3 | return $this->execute($method); |
|
165 | } |
||
166 | |||
167 | 3 | throw new TranslatorException($response->getBody()); |
|
168 | } |
||
169 | |||
170 | 21 | return $method->processResponse($response); |
|
171 | } |
||
172 | |||
173 | /** |
||
174 | * @param string $message |
||
175 | * @throws QuotaExceededException |
||
176 | */ |
||
177 | 6 | private function assertNoTranslateExceptionAndZeroBalance($message) |
|
178 | { |
||
179 | 6 | if (strpos($message, 'TranslateApiException') === 0 |
|
180 | 6 | && strpos($message, 'credentials has zero balance.') |
|
181 | 6 | ) { |
|
182 | 3 | throw new QuotaExceededException($message); |
|
183 | } |
||
184 | 3 | } |
|
185 | |||
186 | /** |
||
187 | * @param string $message |
||
188 | * @throws ArgumentException |
||
189 | * @throws TokenExpiredException |
||
190 | */ |
||
191 | 12 | private function assertNoArgumentException($message) |
|
192 | { |
||
193 | 12 | if (strpos($message, 'Argument Exception') === 0) { |
|
194 | 6 | if (strpos($message, 'The incoming token has expired.')) { |
|
195 | 3 | throw new TokenExpiredException($message); |
|
196 | } |
||
197 | |||
198 | 3 | throw new ArgumentException($message); |
|
199 | } |
||
200 | 6 | } |
|
201 | |||
202 | /** |
||
203 | * @param $text |
||
204 | * @param $to |
||
205 | * @param $from |
||
206 | * @return null|string |
||
207 | */ |
||
208 | 15 | public function translate($text, $to, $from = null) |
|
209 | { |
||
210 | 15 | return $this->execute(new Translate($text, $to, $from)); |
|
211 | } |
||
212 | |||
213 | /** |
||
214 | * @param $text |
||
215 | * @return null|Language |
||
216 | * @throws TranslatorException |
||
217 | */ |
||
218 | 3 | public function detect($text) |
|
222 | |||
223 | /** |
||
224 | * @param $text |
||
225 | * @param $language |
||
226 | * @param string $format |
||
227 | * @param string $options |
||
228 | * @return mixed |
||
229 | * @throws TranslatorException |
||
230 | */ |
||
231 | 3 | public function speak($text, $language, $format = Speak::FORMAT_MP3, $options = Speak::OPTION_MAX_QUALITY) |
|
232 | { |
||
233 | 3 | return $this->execute(new Speak($text, $language, $format, $options)); |
|
234 | } |
||
235 | |||
236 | /** |
||
237 | * @return Language[] |
||
238 | * @throws TranslatorException |
||
239 | */ |
||
240 | 3 | public function getLanguagesForSpeak() |
|
241 | { |
||
242 | 3 | return $this->execute(new GetLanguagesForSpeak()); |
|
243 | } |
||
244 | |||
245 | /** |
||
246 | * @param $languageCodes |
||
247 | * @param string $locale |
||
248 | * @return mixed |
||
249 | * @throws TranslatorException |
||
250 | */ |
||
251 | 6 | public function getLanguageNames($languageCodes, $locale = Language::ENGLISH) |
|
255 | } |