2
2
3
3
namespace BabyMarkt \DeepL ;
4
4
5
+ use ReflectionMethod ;
6
+
5
7
/**
6
8
* DeepL API client library
7
9
*
@@ -16,11 +18,6 @@ class DeepL
16
18
*/
17
19
const API_URL_BASE = '%s://%s/v%s/%s?auth_key=%s ' ;
18
20
19
- /**
20
- * API URL: translate
21
- */
22
- const API_URL_RESOURCE_TRANSLATE = 'translate ' ;
23
-
24
21
/**
25
22
* API URL: usage
26
23
*/
@@ -177,8 +174,8 @@ public function languages()
177
174
* Translate the text string or array from source to destination language
178
175
*
179
176
* @param string|string[] $text
180
- * @param string $sourceLanguage
181
- * @param string $destinationLanguage
177
+ * @param string $sourceLang
178
+ * @param string $targetLang
182
179
* @param string $tagHandling
183
180
* @param array|null $ignoreTags
184
181
* @param string $formality
@@ -191,12 +188,12 @@ public function languages()
191
188
* @return array
192
189
* @throws DeepLException
193
190
*
194
- * @SuppressWarnings(PHPMD.ExcessiveParameterList )
191
+ * @SuppressWarnings(PHPMD.UnusedParameters )
195
192
*/
196
193
public function translate (
197
194
$ text ,
198
- $ sourceLanguage = 'de ' ,
199
- $ destinationLanguage = 'en ' ,
195
+ $ sourceLang = 'de ' ,
196
+ $ targetLang = 'en ' ,
200
197
$ tagHandling = null ,
201
198
array $ ignoreTags = null ,
202
199
$ formality = 'default ' ,
@@ -209,24 +206,17 @@ public function translate(
209
206
if (is_array ($ tagHandling )) {
210
207
throw new \InvalidArgumentException ('$tagHandling must be of type String in V2 of DeepLLibrary ' );
211
208
}
209
+ $ paramsArray = array ();
210
+ $ reflection = new ReflectionMethod ('Babymarkt\DeepL\DeepL ' , 'translate ' );
212
211
213
- $ paramsArray = array (
214
- 'text ' => $ text ,
215
- 'source_lang ' => $ sourceLanguage ,
216
- 'target_lang ' => $ destinationLanguage ,
217
- 'splitting_tags ' => $ splittingTags ,
218
- 'non_splitting_tags ' => $ nonSplittingTags ,
219
- 'ignore_tags ' => $ ignoreTags ,
220
- 'tag_handling ' => $ tagHandling ,
221
- 'formality ' => $ formality ,
222
- 'split_sentences ' => $ splitSentences ,
223
- 'preserve_formatting ' => $ preserveFormatting ,
224
- 'outline_detection ' => $ outlineDetection ,
225
- );
212
+ foreach ($ reflection ->getParameters () as $ param ) {
213
+ $ paramName = $ param ->name ;
214
+ $ paraKey = $ this ->camelToSnake ($ paramName );
215
+ $ paramsArray [$ paraKey ] = $ $ paramName ;
216
+ }
226
217
$ paramsArray = $ this ->removeEmptyParams ($ paramsArray );
227
-
228
- $ url = $ this ->buildBaseUrl ();
229
- $ body = $ this ->buildQuery ($ paramsArray );
218
+ $ url = $ this ->buildBaseUrl ();
219
+ $ body = $ this ->buildQuery ($ paramsArray );
230
220
231
221
// request the DeepL API
232
222
$ translationsArray = $ this ->request ($ url , $ body );
@@ -293,4 +283,29 @@ protected function buildQuery($paramsArray)
293
283
294
284
return $ body ;
295
285
}
286
+
287
+
288
+ /**
289
+ * @param string $subject
290
+ *
291
+ * @return string
292
+ */
293
+ private function camelToSnake ($ subject )
294
+ {
295
+ if (preg_match ('/[A-Z]/ ' , $ subject ) === 0 ) {
296
+ return $ subject ;
297
+ }
298
+ $ pattern = '/([a-z])([A-Z])/ ' ;
299
+ $ snakeString = strtolower (
300
+ preg_replace_callback (
301
+ $ pattern ,
302
+ function ($ match ) {
303
+ return $ match [1 ]."_ " .strtolower ($ match [2 ]);
304
+ },
305
+ $ subject
306
+ )
307
+ );
308
+
309
+ return $ snakeString ;
310
+ }
296
311
}
0 commit comments