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

Commit 4ca4f23

Browse files
committed
- add host to constructor
- add all 3 resources form DeepL api - refactor buildURL-function - add usage-fcuntion
1 parent bd1fb4a commit 4ca4f23

File tree

1 file changed

+44
-24
lines changed

1 file changed

+44
-24
lines changed

src/DeepL.php

Lines changed: 44 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,24 @@
1010
class DeepL
1111
{
1212
/**
13-
* API v1 URL
13+
* API BASE URL
14+
*/
15+
const API_URL_BASE = '%s://%s/v%s';
16+
17+
/**
18+
* API URL: translate
19+
*/
20+
const API_URL_RESOURCE_TRANSLATE = 'translate';
21+
22+
/**
23+
* API URL: usage
1424
*/
15-
const API_URL_V1 = 'https://api.deepl.com/v1/translate';
25+
const API_URL_RESOURCE_USAGE = 'usage';
1626

1727
/**
18-
* API v2 URL
28+
* API URL: languages
1929
*/
20-
const API_URL_V2 = 'https://api.deepl.com/v2/translate';
30+
const API_URL_RESOURCE_LANGUAGES = 'languages ';
2131

2232
/**
2333
* API URL: Parameter auth_key
@@ -132,10 +142,11 @@ class DeepL
132142
* @param string $authKey
133143
* @param integer $apiVersion
134144
*/
135-
public function __construct($authKey, $apiVersion = 2)
145+
public function __construct($authKey, $apiVersion = 2, $host = 'api.deepl.com')
136146
{
137147
$this->authKey = $authKey;
138148
$this->apiVersion = $apiVersion;
149+
$this->host = $host;
139150
$this->curl = curl_init();
140151

141152
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, 1);
@@ -193,6 +204,16 @@ public function translate(
193204
return $translationsArray['translations'];
194205
}
195206

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+
196217
/**
197218
* Check if the given languages are supported
198219
*
@@ -232,31 +253,30 @@ protected function checkLanguages($sourceLanguage, $destinationLanguage)
232253
* @param array $tagHandling
233254
* @param array $ignoreTags
234255
* @param string $formality
256+
* @param string $resource
235257
*
236258
* @return string
237259
*/
238260
protected function buildUrl(
239-
$sourceLanguage,
240-
$destinationLanguage,
241-
array $tagHandling = array(),
242-
array $ignoreTags = array(),
243-
$formality = "default"
244-
) {
245-
// select correct api url
246-
switch ($this->apiVersion) {
247-
case 1:
248-
$url = DeepL::API_URL_V1;
249-
break;
250-
case 2:
251-
$url = DeepL::API_URL_V2;
252-
break;
253-
default:
254-
$url = DeepL::API_URL_V2;
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);
270+
$url .= sprintf('/%s', $resource);
271+
$url .= '?' . sprintf(DeepL::API_URL_AUTH_KEY, $this->authKey);
272+
273+
if (!empty($sourceLanguage)) {
274+
$url .= '&'.sprintf(DeepL::API_URL_SOURCE_LANG, strtolower($sourceLanguage));
255275
}
256276

257-
$url .= '?' . sprintf(DeepL::API_URL_AUTH_KEY, $this->authKey);
258-
$url .= '&' . sprintf(DeepL::API_URL_SOURCE_LANG, strtolower($sourceLanguage));
259-
$url .= '&' . sprintf(DeepL::API_URL_DESTINATION_LANG, strtolower($destinationLanguage));
277+
if (!empty($destinationLanguage)) {
278+
$url .= '&'.sprintf(DeepL::API_URL_DESTINATION_LANG, strtolower($destinationLanguage));
279+
}
260280

261281
if (!empty($tagHandling)) {
262282
$url .= '&' . sprintf(DeepL::API_URL_TAG_HANDLING, implode(',', $tagHandling));

0 commit comments

Comments
 (0)