1 | <?php |
||
19 | class MicrosoftTranslator |
||
20 | { |
||
21 | /** |
||
22 | * |
||
23 | */ |
||
24 | const AUTH_URL = 'https://datamarket.accesscontrol.windows.net/v2/OAuth2-13'; |
||
25 | |||
26 | /** |
||
27 | * |
||
28 | */ |
||
29 | const BASE_URL = 'http://api.microsofttranslator.com/V2/Http.svc/'; |
||
30 | |||
31 | /** |
||
32 | * @var \GuzzleHttp\ClientInterface |
||
33 | */ |
||
34 | private $http; |
||
35 | |||
36 | /** |
||
37 | * @var string |
||
38 | */ |
||
39 | private $scope = 'http://api.microsofttranslator.com'; |
||
40 | |||
41 | /** |
||
42 | * @var string |
||
43 | */ |
||
44 | private $grantType = 'client_credentials'; |
||
45 | |||
46 | /** |
||
47 | * @var string |
||
48 | */ |
||
49 | private $clientId; |
||
50 | |||
51 | /** |
||
52 | * @var string |
||
53 | */ |
||
54 | private $clientSecret; |
||
55 | |||
56 | /** |
||
57 | * @var string |
||
58 | */ |
||
59 | private $accessToken; |
||
60 | |||
61 | /** |
||
62 | * MicrosoftTranslator constructor. |
||
63 | */ |
||
64 | 30 | public function __construct(\GuzzleHttp\ClientInterface $httpClient = null) |
|
72 | |||
73 | /** |
||
74 | * @param $id |
||
75 | * @param $secret |
||
76 | */ |
||
77 | 15 | public function setClient($id, $secret) |
|
82 | |||
83 | /** |
||
84 | * @return mixed |
||
85 | * @throws AuthException |
||
86 | */ |
||
87 | 24 | private function getAccessToken() |
|
110 | |||
111 | 21 | private function createRequest(ApiMethodInterface $method) |
|
128 | |||
129 | /** |
||
130 | * @param ApiMethodInterface $method |
||
131 | * @return mixed |
||
132 | * @throws ArgumentException |
||
133 | * @throws QuotaExceededException |
||
134 | * @throws TranslatorException |
||
135 | */ |
||
136 | 21 | private function execute(ApiMethodInterface $method) |
|
153 | |||
154 | /** |
||
155 | * @param ResponseInterface $response |
||
156 | * @throws ArgumentException |
||
157 | * @throws QuotaExceededException |
||
158 | * @throws TokenExpiredException |
||
159 | */ |
||
160 | 12 | private function processError(ResponseInterface $response) |
|
176 | |||
177 | /** |
||
178 | * @param $text |
||
179 | * @param $to |
||
180 | * @param $from |
||
181 | * @return null|string |
||
182 | */ |
||
183 | 15 | public function translate($text, $to, $from = null) |
|
187 | |||
188 | /** |
||
189 | * @param $text |
||
190 | * @return null|string |
||
191 | * @throws TranslatorException |
||
192 | */ |
||
193 | 3 | public function detect($text) |
|
197 | |||
198 | /** |
||
199 | * @param $text |
||
200 | * @param $language |
||
201 | * @param string $format |
||
202 | * @param string $options |
||
203 | * @return mixed |
||
204 | * @throws TranslatorException |
||
205 | */ |
||
206 | 3 | public function speak($text, $language, $format = Speak::FORMAT_MP3, $options = Speak::OPTION_MAX_QUALITY) |
|
210 | } |