Skip to content

Commit

Permalink
update versions for lower php
Browse files Browse the repository at this point in the history
readme, minor changes in methods

add GA badge
  • Loading branch information
haltuf committed May 12, 2021
1 parent 6542d28 commit 78c4ad9
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 7 deletions.
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# DeepL API wrapper for PHP

![example workflow](https://github.com/haltuf/deepl-php-api/actions/workflows/ci.yml/badge.svg)

Unofficial, no-dependency library for calling DeepL API.
Requires curl extension. Supports PHP >= 7.1, works for PHP 8.0, too.

Currently Work-In_progress. Works for basic use cases, no support for xml parameters yet.

## Installation

```
composer require haltuf/deepl-php-api
```

## Usage

Create `Api` object and call `translate` method.

```php
use Haltuf\DeepL\Api;
use Haltuf\DeepL\Language;

$api = new Api('my_key');
$translation = $api->translate('Text to be translated', Language::CZECH, Language::ENGLISH);

echo $translation->getText();
```

Outputs: `Text, který má být přeložen`

8 changes: 6 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@
"name": "haltuf/deepl-php-api",
"description": "Unofficial DeepL API wrapper for PHP",
"license": ["BSD-3-Clause"],
"require": {
"ext-curl": "*",
"php": ">=7.1"
},
"require-dev": {
"nette/tester": "^2.4",
"mockery/mockery": "^1.4"
"nette/tester": "^2.3",
"mockery/mockery": "^1.3"
},
"autoload": {
"psr-4": {
Expand Down
11 changes: 11 additions & 0 deletions src/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,15 @@ public function getTranslations(): array
{
return $this->translations;
}

public function getText(): string
{
$output = [];
foreach ($this->getTranslations() as $translation)
{
$output[] = $translation->getText();
}

return implode("\n", $output);
}
}
8 changes: 4 additions & 4 deletions tests/unit/Api.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,9 @@ $api = new Api(DEEPL_API_KEY, true, $wrapper);
Assert::same('https://api-free.deepl.com/v2/translate', $api->getEndpoint());

// --- TEST Api::translate()
$response = $api->translate('Text to be translated', Language::CZECH);
Assert::type(Response::class, $response);
Assert::same('Text, který má být přeložen', $response->getTranslations()[0]->getText());
Assert::same(Language::ENGLISH, $response->getTranslations()[0]->getDetectedSourceLanguage());
$translation = $api->translate('Text to be translated', Language::CZECH);
Assert::type(Response::class, $translation);
Assert::same('Text, který má být přeložen', $translation->getText());

$request = new Request('Text to be translated', Language::CZECH, Language::ENGLISH);
$request->formality(Formality::LESS_FORMAL);
Expand All @@ -61,6 +60,7 @@ $request->splitSentences(SplitSentences::NO_NEW_LINES);
$response = $api->send($request);
Assert::type(Response::class, $response);
Assert::same('Text, který má být přeložen', $response->getTranslations()[0]->getText());
Assert::same(Language::ENGLISH, $response->getTranslations()[0]->getDetectedSourceLanguage());


// --- clean up
Expand Down
4 changes: 3 additions & 1 deletion tests/unit/Response.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,6 @@ $jsonResponse = '{
$response = Response::fromJson($jsonResponse);
Assert::type(Response::class, $response);
Assert::count(3, $response->getTranslations());
Assert::same('Dies ist der dritte Satz.', $response->getTranslations()[2]->getText());
Assert::same('Dies ist der dritte Satz.', $response->getTranslations()[2]->getText());

Assert::same("Das ist der erste Satz.\nDas ist der zweite Satz.\nDies ist der dritte Satz.", $response->getText());

0 comments on commit 78c4ad9

Please sign in to comment.