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) |
|
155 | |||
156 | /** |
||
157 | * @param string $message |
||
158 | * @throws QuotaExceededException |
||
159 | */ |
||
160 | 6 | function assertNoTranslateExceptionAndZeroBalance($message) |
|
168 | |||
169 | /** |
||
170 | * @param string $message |
||
171 | * @throws ArgumentException |
||
172 | * @throws TokenExpiredException |
||
173 | */ |
||
174 | 12 | function assertNoArgumentException($message) |
|
184 | |||
185 | /** |
||
186 | * @param $text |
||
187 | * @param $to |
||
188 | * @param $from |
||
189 | * @return null|string |
||
190 | */ |
||
191 | 15 | public function translate($text, $to, $from = null) |
|
195 | |||
196 | /** |
||
197 | * @param $text |
||
198 | * @return null|string |
||
199 | * @throws TranslatorException |
||
200 | */ |
||
201 | 3 | public function detect($text) |
|
205 | |||
206 | /** |
||
207 | * @param $text |
||
208 | * @param $language |
||
209 | * @param string $format |
||
210 | * @param string $options |
||
211 | * @return mixed |
||
212 | * @throws TranslatorException |
||
213 | */ |
||
214 | 3 | public function speak($text, $language, $format = Speak::FORMAT_MP3, $options = Speak::OPTION_MAX_QUALITY) |
|
218 | } |
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.