Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 3e3ba23

Browse files
committed
- use ReflectionMethod insted of array
1 parent 049b850 commit 3e3ba23

File tree

1 file changed

+41
-26
lines changed

1 file changed

+41
-26
lines changed

src/DeepL.php

Lines changed: 41 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace BabyMarkt\DeepL;
44

5+
use ReflectionMethod;
6+
57
/**
68
* DeepL API client library
79
*
@@ -16,11 +18,6 @@ class DeepL
1618
*/
1719
const API_URL_BASE = '%s://%s/v%s/%s?auth_key=%s';
1820

19-
/**
20-
* API URL: translate
21-
*/
22-
const API_URL_RESOURCE_TRANSLATE = 'translate';
23-
2421
/**
2522
* API URL: usage
2623
*/
@@ -177,8 +174,8 @@ public function languages()
177174
* Translate the text string or array from source to destination language
178175
*
179176
* @param string|string[] $text
180-
* @param string $sourceLanguage
181-
* @param string $destinationLanguage
177+
* @param string $sourceLang
178+
* @param string $targetLang
182179
* @param string $tagHandling
183180
* @param array|null $ignoreTags
184181
* @param string $formality
@@ -191,12 +188,12 @@ public function languages()
191188
* @return array
192189
* @throws DeepLException
193190
*
194-
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
191+
* @SuppressWarnings(PHPMD.UnusedParameters)
195192
*/
196193
public function translate(
197194
$text,
198-
$sourceLanguage = 'de',
199-
$destinationLanguage = 'en',
195+
$sourceLang = 'de',
196+
$targetLang = 'en',
200197
$tagHandling = null,
201198
array $ignoreTags = null,
202199
$formality = 'default',
@@ -209,24 +206,17 @@ public function translate(
209206
if (is_array($tagHandling)) {
210207
throw new \InvalidArgumentException('$tagHandling must be of type String in V2 of DeepLLibrary');
211208
}
209+
$paramsArray = array();
210+
$reflection = new ReflectionMethod('Babymarkt\DeepL\DeepL', 'translate');
212211

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+
}
226217
$paramsArray = $this->removeEmptyParams($paramsArray);
227-
228-
$url = $this->buildBaseUrl();
229-
$body = $this->buildQuery($paramsArray);
218+
$url = $this->buildBaseUrl();
219+
$body = $this->buildQuery($paramsArray);
230220

231221
// request the DeepL API
232222
$translationsArray = $this->request($url, $body);
@@ -293,4 +283,29 @@ protected function buildQuery($paramsArray)
293283

294284
return $body;
295285
}
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+
}
296311
}

0 commit comments

Comments
 (0)