@@ -89,85 +89,12 @@ public function __destruct()
89
89
*/
90
90
public function usage ()
91
91
{
92
- $ body = '' ;
93
92
$ url = $ this ->buildBaseUrl (self ::API_URL_RESOURCE_USAGE );
94
- $ usage = $ this ->request ($ url, $ body );
93
+ $ usage = $ this ->request ($ url );
95
94
96
95
return $ usage ;
97
96
}
98
97
99
- /**
100
- * Creates the Base-Url which all of the 3 API-resources have in common.
101
- *
102
- * @param string $resource
103
- *
104
- * @return string
105
- */
106
- protected function buildBaseUrl ($ resource = 'translate ' )
107
- {
108
- $ url = sprintf (
109
- self ::API_URL_BASE ,
110
- self ::API_URL_SCHEMA ,
111
- $ this ->host ,
112
- $ this ->apiVersion ,
113
- $ resource ,
114
- $ this ->authKey
115
- );
116
-
117
- return $ url ;
118
- }
119
-
120
- /**
121
- * Make a request to the given URL
122
- *
123
- * @param string $url
124
- * @param string $body
125
- *
126
- * @return array
127
- *
128
- * @throws DeepLException
129
- */
130
- protected function request ($ url , $ body )
131
- {
132
- curl_setopt ($ this ->curl , CURLOPT_POST , true );
133
- curl_setopt ($ this ->curl , CURLOPT_URL , $ url );
134
- curl_setopt ($ this ->curl , CURLOPT_POSTFIELDS , $ body );
135
- curl_setopt ($ this ->curl , CURLOPT_HTTPHEADER , array ('Content-Type: application/x-www-form-urlencoded ' ));
136
-
137
- $ response = curl_exec ($ this ->curl );
138
-
139
- if (curl_errno ($ this ->curl )) {
140
- throw new DeepLException ('There was a cURL Request Error. ' );
141
- }
142
- $ httpCode = curl_getinfo ($ this ->curl , CURLINFO_HTTP_CODE );
143
- $ responseArray = json_decode ($ response , true );
144
-
145
- if ($ httpCode != 200 && is_array ($ responseArray ) && array_key_exists ('message ' , $ responseArray )) {
146
- throw new DeepLException ($ responseArray ['message ' ], $ httpCode );
147
- }
148
-
149
- if (false === is_array ($ responseArray )) {
150
- throw new DeepLException ('The Response seems to not be valid JSON. ' , $ httpCode );
151
- }
152
-
153
- return $ responseArray ;
154
- }
155
-
156
- /**
157
- * Call languages-Endpoint and return Json-response as an Array
158
- *
159
- * @return array
160
- * @throws DeepLException
161
- */
162
- public function languages ()
163
- {
164
- $ body = '' ;
165
- $ url = $ this ->buildBaseUrl (self ::API_URL_RESOURCE_LANGUAGES );
166
- $ languages = $ this ->request ($ url , $ body );
167
-
168
- return $ languages ;
169
- }
170
-
171
98
/**
172
99
* Translate the text string or array from source to destination language
173
100
*
@@ -229,32 +156,38 @@ public function translate(
229
156
}
230
157
231
158
/**
232
- * @param array $paramsArray
159
+ * Call languages-Endpoint and return Json-response as an Array
233
160
*
234
161
* @return array
162
+ * @throws DeepLException
235
163
*/
236
- private function removeEmptyParams ( $ paramsArray )
164
+ public function languages ( )
237
165
{
166
+ $ url = $ this ->buildBaseUrl (self ::API_URL_RESOURCE_LANGUAGES );
167
+ $ languages = $ this ->request ($ url );
238
168
239
- foreach ($ paramsArray as $ key => $ value ) {
240
- if (true === empty ($ value )) {
241
- unset($ paramsArray [$ key ]);
242
- }
243
- // Special Workaround for outline_detection which will be unset above
244
- // DeepL assumes outline_detection=1 if it is not send
245
- // in order to deactivate it, we need to send outline_detection=0 to the api
246
- if ('outline_detection ' === $ key ) {
247
- if (1 === $ value ) {
248
- unset($ paramsArray [$ key ]);
249
- }
169
+ return $ languages ;
170
+ }
250
171
251
- if (0 === $ value ) {
252
- $ paramsArray [$ key ] = 0 ;
253
- }
254
- }
255
- }
172
+ /**
173
+ * Creates the Base-Url which all of the 3 API-resources have in common.
174
+ *
175
+ * @param string $resource
176
+ *
177
+ * @return string
178
+ */
179
+ protected function buildBaseUrl ($ resource = 'translate ' )
180
+ {
181
+ $ url = sprintf (
182
+ self ::API_URL_BASE ,
183
+ self ::API_URL_SCHEMA ,
184
+ $ this ->host ,
185
+ $ this ->apiVersion ,
186
+ $ resource ,
187
+ $ this ->authKey
188
+ );
256
189
257
- return $ paramsArray ;
190
+ return $ url ;
258
191
}
259
192
260
193
/**
@@ -287,4 +220,72 @@ protected function buildQuery($paramsArray)
287
220
288
221
return $ body ;
289
222
}
223
+
224
+
225
+
226
+
227
+ /**
228
+ * Make a request to the given URL
229
+ *
230
+ * @param string $url
231
+ * @param string $body
232
+ *
233
+ * @return array
234
+ *
235
+ * @throws DeepLException
236
+ */
237
+ protected function request ($ url , $ body = '' )
238
+ {
239
+ curl_setopt ($ this ->curl , CURLOPT_POST , true );
240
+ curl_setopt ($ this ->curl , CURLOPT_URL , $ url );
241
+ curl_setopt ($ this ->curl , CURLOPT_POSTFIELDS , $ body );
242
+ curl_setopt ($ this ->curl , CURLOPT_HTTPHEADER , array ('Content-Type: application/x-www-form-urlencoded ' ));
243
+
244
+ $ response = curl_exec ($ this ->curl );
245
+
246
+ if (curl_errno ($ this ->curl )) {
247
+ throw new DeepLException ('There was a cURL Request Error. ' );
248
+ }
249
+ $ httpCode = curl_getinfo ($ this ->curl , CURLINFO_HTTP_CODE );
250
+ $ responseArray = json_decode ($ response , true );
251
+
252
+ if ($ httpCode != 200 && is_array ($ responseArray ) && array_key_exists ('message ' , $ responseArray )) {
253
+ throw new DeepLException ($ responseArray ['message ' ], $ httpCode );
254
+ }
255
+
256
+ if (false === is_array ($ responseArray )) {
257
+ throw new DeepLException ('The Response seems to not be valid JSON. ' , $ httpCode );
258
+ }
259
+
260
+ return $ responseArray ;
261
+ }
262
+
263
+ /**
264
+ * @param array $paramsArray
265
+ *
266
+ * @return array
267
+ */
268
+ private function removeEmptyParams ($ paramsArray )
269
+ {
270
+
271
+ foreach ($ paramsArray as $ key => $ value ) {
272
+ if (true === empty ($ value )) {
273
+ unset($ paramsArray [$ key ]);
274
+ }
275
+ // Special Workaround for outline_detection which will be unset above
276
+ // DeepL assumes outline_detection=1 if it is not send
277
+ // in order to deactivate it, we need to send outline_detection=0 to the api
278
+ if ('outline_detection ' === $ key ) {
279
+ if (1 === $ value ) {
280
+ unset($ paramsArray [$ key ]);
281
+ }
282
+
283
+ if (0 === $ value ) {
284
+ $ paramsArray [$ key ] = 0 ;
285
+ }
286
+ }
287
+ }
288
+
289
+ return $ paramsArray ;
290
+ }
290
291
}
0 commit comments