From 78c4ad9cc44c7147c518f1aec3c8b92bae622591 Mon Sep 17 00:00:00 2001 From: Michal Haltuf Date: Wed, 12 May 2021 11:22:10 +0200 Subject: [PATCH] update versions for lower php readme, minor changes in methods add GA badge --- README.md | 31 +++++++++++++++++++++++++++++++ composer.json | 8 ++++++-- src/Response.php | 11 +++++++++++ tests/unit/Api.phpt | 8 ++++---- tests/unit/Response.phpt | 4 +++- 5 files changed, 55 insertions(+), 7 deletions(-) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..8bbce05 --- /dev/null +++ b/README.md @@ -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` + diff --git a/composer.json b/composer.json index 108e204..19b97e0 100644 --- a/composer.json +++ b/composer.json @@ -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": { diff --git a/src/Response.php b/src/Response.php index c2ba597..dd939f4 100644 --- a/src/Response.php +++ b/src/Response.php @@ -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); + } } \ No newline at end of file diff --git a/tests/unit/Api.phpt b/tests/unit/Api.phpt index be4c1e2..6c25560 100644 --- a/tests/unit/Api.phpt +++ b/tests/unit/Api.phpt @@ -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); @@ -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 diff --git a/tests/unit/Response.phpt b/tests/unit/Response.phpt index 5edee60..802a928 100644 --- a/tests/unit/Response.phpt +++ b/tests/unit/Response.phpt @@ -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()); \ No newline at end of file +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()); \ No newline at end of file