1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BabyMarkt\DeepL; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* DeepL API client library |
9
|
|
|
*/ |
10
|
|
|
class DeepL |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* API URL: usage |
14
|
|
|
*/ |
15
|
|
|
const API_URL_RESOURCE_USAGE = 'usage'; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* API URL: languages |
19
|
|
|
*/ |
20
|
|
|
const API_URL_RESOURCE_LANGUAGES = 'languages'; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var ClientInterface |
24
|
|
|
*/ |
25
|
|
|
private $client; |
26
|
|
|
|
27
|
|
|
public function __construct($authKey, $apiVersion = 2, $host = 'api-free.deepl.com', ClientInterface $client = null) |
28
|
|
|
{ |
29
|
|
|
$this->client = $client ?? new Client($authKey, $apiVersion, $host); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Call languages-Endpoint and return Json-response as an Array |
34
|
|
|
* |
35
|
|
|
* @param string $type |
36
|
|
|
* |
37
|
|
|
* @return array |
38
|
|
|
* |
39
|
|
|
* @throws DeepLException |
40
|
|
|
*/ |
41
|
|
|
public function languages($type = null) |
42
|
|
|
{ |
43
|
|
|
$url = $this->client->buildBaseUrl(self::API_URL_RESOURCE_LANGUAGES); |
44
|
|
|
$body = $this->client->buildQuery(array('type' => $type)); |
45
|
|
|
$languages = $this->client->request($url, $body); |
46
|
|
|
|
47
|
|
|
return $languages; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Translate the text string or array from source to destination language |
52
|
|
|
* For detailed info on Parameters see README.md |
53
|
|
|
* |
54
|
|
|
* @param string|string[] $text |
55
|
|
|
* @param string $sourceLang |
56
|
|
|
* @param string $targetLang |
57
|
|
|
* @param string $tagHandling |
58
|
|
|
* @param array|null $ignoreTags |
59
|
|
|
* @param string $formality |
60
|
|
|
* @param null $splitSentences |
|
|
|
|
61
|
|
|
* @param null $preserveFormatting |
|
|
|
|
62
|
|
|
* @param array|null $nonSplittingTags |
63
|
|
|
* @param null $outlineDetection |
|
|
|
|
64
|
|
|
* @param array|null $splittingTags |
65
|
|
|
* @param string|null $glossaryId |
66
|
|
|
* |
67
|
|
|
* @return array |
68
|
|
|
* |
69
|
|
|
* @throws DeepLException |
70
|
|
|
* |
71
|
|
|
* @SuppressWarnings(PHPMD.ExcessiveParameterList) |
72
|
|
|
*/ |
73
|
|
|
public function translate( |
74
|
|
|
$text, |
75
|
|
|
$sourceLang = '', |
76
|
|
|
$targetLang = 'en', |
77
|
|
|
$tagHandling = null, |
78
|
|
|
array $ignoreTags = null, |
79
|
|
|
$formality = 'default', |
80
|
|
|
$splitSentences = null, |
81
|
|
|
$preserveFormatting = null, |
82
|
|
|
array $nonSplittingTags = null, |
83
|
|
|
$outlineDetection = null, |
84
|
|
|
array $splittingTags = null, |
85
|
31 |
|
string $glossaryId = null |
86
|
|
|
) { |
87
|
31 |
|
if (is_array($tagHandling)) { |
|
|
|
|
88
|
31 |
|
throw new InvalidArgumentException('$tagHandling must be of type String in V2 of DeepLLibrary'); |
89
|
31 |
|
} |
90
|
31 |
|
$paramsArray = array( |
91
|
|
|
'text' => $text, |
92
|
31 |
|
'source_lang' => $sourceLang, |
93
|
31 |
|
'target_lang' => $targetLang, |
94
|
|
|
'splitting_tags' => $splittingTags, |
95
|
|
|
'non_splitting_tags' => $nonSplittingTags, |
96
|
|
|
'ignore_tags' => $ignoreTags, |
97
|
|
|
'tag_handling' => $tagHandling, |
98
|
31 |
|
'formality' => $formality, |
99
|
|
|
'split_sentences' => $splitSentences, |
100
|
31 |
|
'preserve_formatting' => $preserveFormatting, |
101
|
31 |
|
'outline_detection' => $outlineDetection, |
102
|
31 |
|
'glossary_id' => $glossaryId |
103
|
31 |
|
); |
104
|
|
|
|
105
|
|
|
$paramsArray = $this->removeEmptyParams($paramsArray); |
106
|
|
|
$url = $this->client->buildBaseUrl(); |
107
|
|
|
$body = $this->client->buildQuery($paramsArray); |
108
|
|
|
|
109
|
|
|
// request the DeepL API |
110
|
|
|
$translationsArray = $this->client->request($url, $body); |
111
|
|
|
|
112
|
|
|
return $translationsArray['translations']; |
113
|
4 |
|
} |
114
|
|
|
|
115
|
4 |
|
/** |
116
|
4 |
|
* Calls the usage-Endpoint and return Json-response as an array |
117
|
4 |
|
* |
118
|
|
|
* @return array |
119
|
3 |
|
* |
120
|
|
|
* @throws DeepLException |
121
|
|
|
*/ |
122
|
|
|
public function usage() |
123
|
|
|
{ |
124
|
|
|
$url = $this->client->buildBaseUrl(self::API_URL_RESOURCE_USAGE); |
125
|
|
|
$usage = $this->client->request($url); |
126
|
|
|
|
127
|
|
|
return $usage; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @param array $paramsArray |
132
|
|
|
* |
133
|
|
|
* @return array |
134
|
|
|
*/ |
135
|
|
|
private function removeEmptyParams($paramsArray) |
136
|
|
|
{ |
137
|
|
|
foreach ($paramsArray as $key => $value) { |
138
|
|
|
if (true === empty($value)) { |
139
|
|
|
unset($paramsArray[$key]); |
140
|
|
|
} |
141
|
|
|
// Special Workaround for outline_detection which will be unset above |
142
|
|
|
// DeepL assumes outline_detection=1 if it is not send |
143
|
|
|
// in order to deactivate it, we need to send outline_detection=0 to the api |
144
|
|
|
if ('outline_detection' === $key) { |
145
|
|
|
if (1 === $value) { |
146
|
|
|
unset($paramsArray[$key]); |
147
|
|
|
} |
148
|
1 |
|
|
149
|
|
|
if (0 === $value) { |
150
|
1 |
|
$paramsArray[$key] = 0; |
151
|
1 |
|
} |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
return $paramsArray; |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|