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

Commit a0ce416

Browse files
committed
- add languages-function
- add Missiong DeepL-Parameters to buildUrl and translate functions - refactor code for PHP7+ - Adjust Tests
1 parent 4ca4f23 commit a0ce416

File tree

2 files changed

+184
-88
lines changed

2 files changed

+184
-88
lines changed

src/DeepL.php

Lines changed: 156 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
class DeepL
1111
{
1212
/**
13-
* API BASE URL
14-
*/
13+
* API BASE URL
14+
*/
1515
const API_URL_BASE = '%s://%s/v%s';
1616

1717
/**
@@ -27,7 +27,7 @@ class DeepL
2727
/**
2828
* API URL: languages
2929
*/
30-
const API_URL_RESOURCE_LANGUAGES = 'languages ';
30+
const API_URL_RESOURCE_LANGUAGES = 'languages';
3131

3232
/**
3333
* API URL: Parameter auth_key
@@ -64,25 +64,50 @@ class DeepL
6464
*/
6565
const API_URL_FORMALITY = 'formality=%s';
6666

67+
/**
68+
* API URL: Parameter split_sentences
69+
*/
70+
const API_URL_SPLIT_SENTENCES= 'split_sentences=%s';
71+
72+
/**
73+
* API URL: Parameter preserve_formatting
74+
*/
75+
const API_URL_PRESERVER_FORMATING = 'preserve_formatting=%s';
76+
77+
/**
78+
* API URL: Parameter non_splitting_tags
79+
*/
80+
const API_URL_NON_SPLITTING_TAGS= 'non_splitting_tags=%s';
81+
82+
/**
83+
* API URL: Parameter outline_detection
84+
*/
85+
const API_URL_OUTLINE_DETECTION= 'outline_detection=%s';
86+
87+
/**
88+
* API URL: Parameter splitting_tags
89+
*/
90+
const API_URL_SPLITTING_TAGS = 'splitting_tags=%s';
91+
6792
/**
6893
* DeepL HTTP error codes
6994
*
7095
* @var array
7196
*/
72-
protected $errorCodes = array(
97+
protected $errorCodes = [
7398
400 => 'Wrong request, please check error message and your parameters.',
7499
403 => 'Authorization failed. Please supply a valid auth_key parameter.',
75100
413 => 'Request Entity Too Large. The request size exceeds the current limit.',
76101
429 => 'Too many requests. Please wait and send your request once again.',
77-
456 => 'Quota exceeded. The character limit has been reached.'
78-
);
102+
456 => 'Quota exceeded. The character limit has been reached.',
103+
];
79104

80105
/**
81106
* Supported translation source languages
82107
*
83108
* @var array
84109
*/
85-
protected $sourceLanguages = array(
110+
protected $sourceLanguages = [
86111
'EN',
87112
'DE',
88113
'FR',
@@ -93,15 +118,15 @@ class DeepL
93118
'PL',
94119
'RU',
95120
'JA',
96-
'ZH'
97-
);
121+
'ZH',
122+
];
98123

99124
/**
100125
* Supported translation destination languages
101126
*
102127
* @var array
103128
*/
104-
protected $destinationLanguages = array(
129+
protected $destinationLanguages = [
105130
'EN',
106131
'DE',
107132
'FR',
@@ -114,8 +139,8 @@ class DeepL
114139
'PL',
115140
'RU',
116141
'JA',
117-
'ZH'
118-
);
142+
'ZH',
143+
];
119144

120145
/**
121146
* @var integer
@@ -177,18 +202,31 @@ public function __destruct()
177202
* @throws DeepLException
178203
*/
179204
public function translate(
180-
$text,
181-
$sourceLanguage = 'de',
182-
$destinationLanguage = 'en',
183-
array $tagHandling = array(),
184-
array $ignoreTags = array(),
185-
$formality = "default"
205+
string $text,
206+
string $sourceLanguage = 'de',
207+
string $destinationLanguage = 'en',
208+
string $tagHandling = null,
209+
array $ignoreTags = null,
210+
string $formality = "default",
211+
string $resource = 'translate',
212+
string $splitSentences = null,
213+
bool $preserveFormatting = null,
214+
array $nonSplittingTags = null,
215+
bool $outlineDetection = null,
216+
array $splittingTags = null
186217
) {
187218
// make sure we only accept supported languages
188219
$this->checkLanguages($sourceLanguage, $destinationLanguage);
189220

190221
// build the DeepL API request url
191-
$url = $this->buildUrl($sourceLanguage, $destinationLanguage, $tagHandling, $ignoreTags, $formality);
222+
$url = $this->buildUrl(
223+
$sourceLanguage,
224+
$destinationLanguage,
225+
$tagHandling,
226+
$ignoreTags,
227+
$formality,
228+
self::API_URL_RESOURCE_TRANSLATE
229+
);
192230
$body = $this->buildBody($text);
193231

194232
// request the DeepL API
@@ -204,16 +242,6 @@ public function translate(
204242
return $translationsArray['translations'];
205243
}
206244

207-
public function usage()
208-
{
209-
$result = [];
210-
$body = '';
211-
$url = $this->buildUrl(null, null, [], [], '', 'usage');
212-
$result = $this->request($url, $body);
213-
214-
return $result;
215-
}
216-
217245
/**
218246
* Check if the given languages are supported
219247
*
@@ -224,19 +252,19 @@ public function usage()
224252
*
225253
* @throws DeepLException
226254
*/
227-
protected function checkLanguages($sourceLanguage, $destinationLanguage)
255+
protected function checkLanguages(string $sourceLanguage, string $destinationLanguage)
228256
{
229257
$sourceLanguage = strtoupper($sourceLanguage);
230258

231-
if (!in_array($sourceLanguage, $this->sourceLanguages)) {
259+
if (false === in_array($sourceLanguage, $this->sourceLanguages)) {
232260
throw new DeepLException(
233261
sprintf('The language "%s" is not supported as source language.', $sourceLanguage)
234262
);
235263
}
236264

237265
$destinationLanguage = strtoupper($destinationLanguage);
238266

239-
if (!in_array($destinationLanguage, $this->destinationLanguages)) {
267+
if (false === in_array($destinationLanguage, $this->destinationLanguages)) {
240268
throw new DeepLException(
241269
sprintf('The language "%s" is not supported as destination language.', $destinationLanguage)
242270
);
@@ -258,66 +286,63 @@ protected function checkLanguages($sourceLanguage, $destinationLanguage)
258286
* @return string
259287
*/
260288
protected function buildUrl(
261-
$sourceLanguage = null,
262-
$destinationLanguage = null,
263-
$tagHandling = null,
264-
$ignoreTags = null,
265-
$formality="default",
266-
$resource='translate'
267-
)
268-
{
269-
$url = sprintf(DeepL::API_URL_BASE, 'https', $this->host, $this->apiVersion);
289+
string $sourceLanguage = null,
290+
string $destinationLanguage = null,
291+
string $tagHandling = null,
292+
array $ignoreTags = null,
293+
string $formality = 'default',
294+
string $resource = 'translate',
295+
string $splitSentences = null,
296+
bool $preserveFormatting = null,
297+
array $nonSplittingTags = null,
298+
bool $outlineDetection = null,
299+
array $splittingTags = null
300+
) {
301+
$url = sprintf(self::API_URL_BASE, 'https', $this->host, $this->apiVersion);
270302
$url .= sprintf('/%s', $resource);
271-
$url .= '?' . sprintf(DeepL::API_URL_AUTH_KEY, $this->authKey);
303+
$url .= '?'.sprintf(self::API_URL_AUTH_KEY, $this->authKey);
272304

273-
if (!empty($sourceLanguage)) {
274-
$url .= '&'.sprintf(DeepL::API_URL_SOURCE_LANG, strtolower($sourceLanguage));
305+
if (false === empty($sourceLanguage)) {
306+
$url .= '&'.sprintf(self::API_URL_SOURCE_LANG, strtolower($sourceLanguage));
275307
}
276308

277-
if (!empty($destinationLanguage)) {
278-
$url .= '&'.sprintf(DeepL::API_URL_DESTINATION_LANG, strtolower($destinationLanguage));
309+
if (false === empty($destinationLanguage)) {
310+
$url .= '&'.sprintf(self::API_URL_DESTINATION_LANG, strtolower($destinationLanguage));
279311
}
280312

281-
if (!empty($tagHandling)) {
282-
$url .= '&' . sprintf(DeepL::API_URL_TAG_HANDLING, implode(',', $tagHandling));
313+
if (false === empty($tagHandling)) {
314+
$url .= '&'.sprintf(self::API_URL_TAG_HANDLING, $tagHandling);
283315
}
284316

285-
if (!empty($ignoreTags)) {
286-
$url .= '&' . sprintf(DeepL::API_URL_IGNORE_TAGS, implode(',', $ignoreTags));
317+
if (false === empty($ignoreTags)) {
318+
$url .= '&'.sprintf(self::API_URL_IGNORE_TAGS, implode(',', $ignoreTags));
287319
}
288320

289-
if (!empty($formality)) {
290-
$url .= '&' . sprintf(DeepL::API_URL_FORMALITY, $formality);
321+
if (false === empty($formality)) {
322+
$url .= '&'.sprintf(self::API_URL_FORMALITY, $formality);
291323
}
292324

293-
return $url;
294-
}
325+
if (false === empty($splitSentences)) {
326+
$url .= '&'.sprintf(self::API_URL_SPLIT_SENTENCES, $splitSentences);
327+
}
295328

296-
/**
297-
* Build the body for the DeepL API request
298-
*
299-
* @param string|string[] $text
300-
*
301-
* @return string
302-
*/
303-
protected function buildBody($text)
304-
{
305-
$body = '';
306-
$first = true;
329+
if (false === empty($preserveFormatting)) {
330+
$url .= '&'.sprintf(self::API_URL_PRESERVER_FORMATING, $preserveFormatting);
331+
}
307332

308-
if (!is_array($text)) {
309-
$text = (array)$text;
333+
if (false === empty($nonSplittingTags)) {
334+
$url .= '&'.sprintf(self::API_URL_NON_SPLITTING_TAGS, implode(',', $nonSplittingTags));
310335
}
311336

312-
foreach ($text as $textElement) {
313-
$body .= ($first ? '' : '&') . sprintf(DeepL::API_URL_TEXT, rawurlencode($textElement));
337+
if (false === empty($outlineDetection)) {
338+
$url .= '&'.sprintf(self::API_URL_OUTLINE_DETECTION, $outlineDetection);
339+
}
314340

315-
if ($first) {
316-
$first = false;
317-
}
341+
if (false === empty($splittingTags)) {
342+
$url .= '&'.sprintf(self::API_URL_SPLITTING_TAGS, implode(',', $splittingTags));
318343
}
319344

320-
return $body;
345+
return $url;
321346
}
322347

323348
/**
@@ -331,9 +356,10 @@ protected function buildBody($text)
331356
*/
332357
protected function request($url, $body)
333358
{
359+
curl_setopt($this->curl, CURLOPT_POST, true);
334360
curl_setopt($this->curl, CURLOPT_URL, $url);
335361
curl_setopt($this->curl, CURLOPT_POSTFIELDS, $body);
336-
curl_setopt($this->curl, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
362+
curl_setopt($this->curl, CURLOPT_HTTPHEADER, ['Content-Type: application/x-www-form-urlencoded']);
337363

338364
$response = curl_exec($this->curl);
339365

@@ -355,4 +381,60 @@ protected function request($url, $body)
355381

356382
return $translationsArray;
357383
}
384+
385+
/**
386+
* Build the body for the DeepL API request
387+
*
388+
* @param string|string[] $text
389+
*
390+
* @return string
391+
*/
392+
protected function buildBody($text)
393+
{
394+
$body = '';
395+
$first = true;
396+
397+
if (!is_array($text)) {
398+
$text = (array)$text;
399+
}
400+
401+
foreach ($text as $textElement) {
402+
$body .= ($first ? '' : '&').sprintf(self::API_URL_TEXT, rawurlencode($textElement));
403+
404+
if ($first) {
405+
$first = false;
406+
}
407+
}
408+
409+
return $body;
410+
}
411+
412+
/**
413+
* @return array
414+
* @throws DeepLException
415+
*/
416+
public function usage()
417+
{
418+
$result = [];
419+
$body = '';
420+
$url = $this->buildUrl(null, null, null, null, '', self::API_URL_RESOURCE_USAGE);
421+
$result = $this->request($url, $body);
422+
423+
return $result;
424+
}
425+
426+
427+
/**
428+
* @return array
429+
* @throws DeepLException
430+
*/
431+
public function languages()
432+
{
433+
$result = [];
434+
$body = '';
435+
$url = $this->buildUrl(null, null, [], [], '', self::API_URL_RESOURCE_LANGUAGES);
436+
$result = $this->request($url, $body);
437+
438+
return $result;
439+
}
358440
}

0 commit comments

Comments
 (0)