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

Commit 4b63a36

Browse files
update deepl library and tests
1 parent 3e2a491 commit 4b63a36

File tree

2 files changed

+84
-20
lines changed

2 files changed

+84
-20
lines changed

src/DeepL.php

Lines changed: 48 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,35 @@
22

33
namespace BabyMarkt\DeepL;
44

5+
/**
6+
* DeepL API client library
7+
*
8+
* @package BabyMarkt\DeepL
9+
*/
510
class DeepL
611
{
712
/**
8-
* API URL
13+
* API v1 URL
914
*/
1015
const API_URL = 'https://api.deepl.com/v1/translate';
1116

1217
/**
13-
* API URL: Parameter auth_key
18+
* API v1 URL: Parameter auth_key
1419
*/
1520
const API_URL_AUTH_KEY = 'auth_key=%s';
1621

1722
/**
18-
* API URL: Parameter text
23+
* API v1 URL: Parameter text
1924
*/
2025
const API_URL_TEXT = 'text=%s';
2126

2227
/**
23-
* API URL: Parameter source_lang
28+
* API v1 URL: Parameter source_lang
2429
*/
2530
const API_URL_SOURCE_LANG = 'source_lang=%s';
2631

2732
/**
28-
* API URL: Parameter target_lang
33+
* API v1 URL: Parameter target_lang
2934
*/
3035
const API_URL_DESTINATION_LANG = 'target_lang=%s';
3136

@@ -66,6 +71,13 @@ class DeepL
6671
*/
6772
protected $authKey;
6873

74+
/**
75+
* cURL resource
76+
*
77+
* @var resource
78+
*/
79+
protected $curl;
80+
6981
/**
7082
* DeepL constructor
7183
*
@@ -74,18 +86,33 @@ class DeepL
7486
public function __construct($authKey)
7587
{
7688
$this->authKey = $authKey;
89+
$this->curl = curl_init();
90+
91+
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, 1);
92+
}
93+
94+
/**
95+
* DeepL Destructor
96+
*/
97+
public function __destruct()
98+
{
99+
if ($this->curl && is_resource($this->curl)) {
100+
curl_close($this->curl);
101+
}
77102
}
78103

79104
/**
80105
* Translate the text string or array from source to destination language
81106
*
82-
* @param $text
83-
* @param $sourceLanguage
84-
* @param $destinationLanguage
107+
* @param $text string|string[]
108+
* @param $sourceLanguage string
109+
* @param $destinationLanguage string
85110
*
86111
* @return array
112+
*
113+
* @throws DeepLException
87114
*/
88-
public function translate($text, $sourceLanguage, $destinationLanguage)
115+
public function translate($text, $sourceLanguage = 'de', $destinationLanguage = 'en')
89116
{
90117
// make sure we only accept supported languages
91118
$this->checkLanguages($sourceLanguage, $destinationLanguage);
@@ -154,29 +181,30 @@ protected function buildUrl($text, $sourceLanguage, $destinationLanguage)
154181
*
155182
* @param $url
156183
*
157-
* @return mixed
184+
* @return array
158185
*
159186
* @throws DeepLException
160187
*/
161188
protected function request($url)
162189
{
163-
$ch = curl_init();
164-
curl_setopt($ch, CURLOPT_URL, $url);
165-
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
166-
$output = curl_exec($ch);
167-
curl_close($ch);
190+
curl_setopt($this->curl, CURLOPT_URL, $url);
191+
$response = curl_exec($this->curl);
168192

169193
// if there was a curl error
170-
if ($output == false) {
194+
if ($response == false) {
195+
196+
$errorString = curl_error($this->curl);
197+
$errorNumber = curl_errno($this->curl);
171198

172-
$errorString = curl_error($ch);
173-
$errorNumber = curl_errno($ch);
199+
if (!$errorString) {
200+
$errorString = 'There was a cURL Response Error - please check your DeepL Auth Key.';
201+
}
174202

175203
throw new DeepLException($errorString, $errorNumber);
176204
}
177205

178-
$json = json_decode($output);
206+
$translationsArray = json_decode($response, true);
179207

180-
return $json;
208+
return $translationsArray;
181209
}
182210
}

tests/DeepLTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,40 @@ public function testBuildUrl()
8282

8383
$this->assertEquals($expectedString, $return);
8484
}
85+
86+
/**
87+
* Test translate()
88+
*
89+
* TEST REQUIRES VALID DEEPL AUTH KEY!!
90+
*/
91+
public function testTranslate()
92+
{
93+
return;
94+
95+
$authKey = 'INSERT YOUR AUTH KEY HERE';
96+
$germanText = 'Hallo Welt';
97+
98+
$deepl = new DeepL($authKey);
99+
100+
$response = $deepl->translate($germanText);
101+
102+
$this->assertEquals('Hello World', $response['translations'][0]['text']);
103+
}
104+
105+
/**
106+
* Test translate()
107+
*
108+
* TEST REQUIRES VALID DEEPL AUTH KEY!!
109+
*/
110+
public function testTranslateException()
111+
{
112+
$authKey = '123456';
113+
$germanText = 'Hallo Welt';
114+
115+
$deepl = new DeepL($authKey);
116+
117+
$this->setExpectedException('\BabyMarkt\DeepL\DeepLException');
118+
119+
$response = $deepl->translate($germanText);
120+
}
85121
}

0 commit comments

Comments
 (0)