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

Commit 5b18acc

Browse files
Merge pull request #6 from Baby-Markt/update-project
MASTER: Update project
2 parents 2aa2870 + 31d7ee0 commit 5b18acc

File tree

10 files changed

+151
-70
lines changed

10 files changed

+151
-70
lines changed

.travis.yml

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,19 @@ matrix:
1818
allow_failures:
1919
- php: nightly
2020

21+
env:
22+
- COMPOSER_ALLOW_XDEBUG=0
23+
2124
cache:
2225
directories:
2326
- $HOME/.composer/cache
2427

25-
before_script:
28+
before_install:
29+
- phpenv config-rm xdebug.ini
30+
- composer validate
31+
32+
install:
2633
- composer install
2734

2835
script:
29-
- composer test
36+
- composer test:all

LICENSE

Lines changed: 0 additions & 21 deletions
This file was deleted.

LICENSE.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# The MIT License (MIT)
2+
3+
Copyright (c) 2019 babymarkt.de GmbH <entwicklung@babymarkt.de>
4+
5+
> Permission is hereby granted, free of charge, to any person obtaining a copy
6+
> of this software and associated documentation files (the "Software"), to deal
7+
> in the Software without restriction, including without limitation the rights
8+
> to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
> copies of the Software, and to permit persons to whom the Software is
10+
> furnished to do so, subject to the following conditions:
11+
>
12+
> The above copyright notice and this permission notice shall be included in
13+
> all copies or substantial portions of the Software.
14+
>
15+
> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
> IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
> FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
> LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
> OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
> THE SOFTWARE.

README.md

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
# deepl-php-lib
22

3-
[![Build Status](https://travis-ci.org/Baby-Markt/deepl-php-lib.svg?branch=master)](https://travis-ci.org/Baby-Markt/deepl-php-lib)
4-
[![Latest Stable Version](https://poser.pugx.org/babymarkt/deepl-php-lib/v/stable.svg)](https://packagist.org/packages/babymarkt/deepl-php-lib)
5-
[![Total Downloads](https://poser.pugx.org/babymarkt/deepl-php-lib/downloads.png)](https://packagist.org/packages/babymarkt/deepl-php-lib)
3+
[![Latest Version on Packagist][ico-version]][link-packagist]
4+
[![Software License][ico-license]](LICENSE.md)
5+
[![Build Status][ico-travis]][link-travis]
6+
[![Coverage Status][ico-scrutinizer]][link-scrutinizer]
7+
[![Quality Score][ico-code-quality]][link-code-quality]
8+
[![Total Downloads][ico-downloads]][link-downloads]
69

710
Simple PHP Library for DeepL API. You can translate one or multiple text strings (up to 50) per request.
811

9-
## Installation
12+
## Install
1013

1114
Use composer if you want to use this library in your project.
1215

@@ -46,34 +49,52 @@ foreach ($translations as $translation) {
4649
}
4750
```
4851

49-
## Run PHPUnit Tests
52+
## Testing
5053

51-
Clone the repository.
54+
Run PHP_CodeSniffer Tests:
5255

5356
```bash
54-
git clone git@github.com:Baby-Markt/deepl-php-lib.git
57+
composer cs
5558
```
5659

57-
Create phpunit.xml from dist file.
60+
Run PHPMD Tests:
5861

5962
```bash
60-
cp phpunit.xml.dist phpunit.xml
63+
composer md
6164
```
6265

63-
Install composer dependencies.
66+
Run PHPUnit Tests:
6467

6568
```bash
66-
composer install
69+
composer test
6770
```
6871

69-
Now, run PHPUnit Tests.
72+
Run all tests:
7073

7174
```bash
72-
vendor/bin/phpunit
75+
composer test:all
7376
```
7477

75-
Or use composer.
78+
## Credits
7679

77-
```bash
78-
composer test
79-
```
80+
- [Arkadius Jonczek][link-author]
81+
- [All Contributors][link-contributors]
82+
83+
## License
84+
85+
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.
86+
87+
[ico-version]: https://img.shields.io/packagist/v/babymarkt/deepl-php-lib.svg?style=flat-square
88+
[ico-license]: https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square
89+
[ico-travis]: https://img.shields.io/travis/Baby-Markt/deepl-php-lib/master.svg?style=flat-square
90+
[ico-scrutinizer]: https://img.shields.io/scrutinizer/coverage/g/babymarkt/deepl-php-lib.svg?style=flat-square
91+
[ico-code-quality]: https://img.shields.io/scrutinizer/g/babymarkt/deepl-php-lib.svg?style=flat-square
92+
[ico-downloads]: https://img.shields.io/packagist/dt/babymarkt/deepl-php-lib.svg?style=flat-square
93+
94+
[link-packagist]: https://packagist.org/packages/babymarkt/deepl-php-lib
95+
[link-travis]: https://travis-ci.org/Baby-Markt/deepl-php-lib
96+
[link-scrutinizer]: https://scrutinizer-ci.com/g/babymarkt/deepl-php-lib/code-structure
97+
[link-code-quality]: https://scrutinizer-ci.com/g/babymarkt/deepl-php-lib
98+
[link-downloads]: https://packagist.org/packages/babymarkt/deepl-php-lib
99+
[link-author]: https://github.com/arkadiusjonczek
100+
[link-contributors]: ../../contributors

composer.json

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
{
22
"name": "babymarkt/deepl-php-lib",
3-
"description": "DeepL API Client Library supporting PHP >= 5.3 && PHP < 8.0",
43
"type": "library",
4+
"description": "DeepL API Client Library supporting PHP >= 5.3 && PHP < 8.0",
5+
"keywords": [
6+
"babymarkt",
7+
"deepl",
8+
"i18n",
9+
"translation"
10+
],
11+
"homepage": "https://github.com/Baby-Markt/deepl-php-lib",
512
"license": "MIT",
613
"authors": [
714
{
@@ -14,7 +21,9 @@
1421
},
1522
"require-dev": {
1623
"php": ">=5.3 <8.0",
17-
"phpunit/phpunit": "^4.8"
24+
"phpmd/phpmd": "2.4.*",
25+
"phpunit/phpunit": "^4.8",
26+
"squizlabs/php_codesniffer": "^2.9"
1827
},
1928
"autoload": {
2029
"psr-4": {
@@ -27,8 +36,16 @@
2736
}
2837
},
2938
"scripts": {
30-
"test": [
31-
"vendor/bin/phpunit"
39+
"cs": "vendor/bin/phpcs",
40+
"md": "vendor/bin/phpmd src,tests text phpmd.xml.dist",
41+
"test": "vendor/bin/phpunit",
42+
"test:all": [
43+
"composer cs",
44+
"composer md",
45+
"composer test"
3246
]
47+
},
48+
"config": {
49+
"sort-packages": true
3350
}
3451
}

phpcs.xml.dist

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<ruleset name="babymarkt/deepl-php-lib">
4+
<description>The coding standard of deepl-php-lib package</description>
5+
6+
<file>src</file>
7+
<file>tests</file>
8+
9+
<arg value="p" />
10+
<arg name="colors" />
11+
<arg value="s" />
12+
13+
<!-- Use the PSR2 Standard-->
14+
<rule ref="PSR2" />
15+
</ruleset>

phpmd.xml.dist

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
3+
<ruleset name="deepl-php-lib ruleset"
4+
xmlns="http://pmd.sf.net/ruleset/1.0.0"
5+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
6+
xsi:schemaLocation="http://pmd.sf.net/ruleset/1.0.0 http://pmd.sf.net/ruleset_xml_schema.xsd"
7+
xsi:noNamespaceSchemaLocation="http://pmd.sf.net/ruleset_xml_schema.xsd"
8+
>
9+
<description>PHPMD Rules for deepl-php-lib</description>
10+
11+
<rule ref="rulesets/cleancode.xml" />
12+
<rule ref="rulesets/codesize.xml" />
13+
<rule ref="rulesets/controversial.xml" />
14+
<rule ref="rulesets/design.xml" />
15+
<rule ref="rulesets/naming.xml" />
16+
<rule ref="rulesets/unusedcode.xml" />
17+
</ruleset>

src/DeepL.php

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,7 @@ public function translate(
159159
$destinationLanguage = 'en',
160160
array $tagHandling = array(),
161161
array $ignoreTags = array()
162-
)
163-
{
162+
) {
164163
// make sure we only accept supported languages
165164
$this->checkLanguages($sourceLanguage, $destinationLanguage);
166165

@@ -174,13 +173,11 @@ public function translate(
174173

175174
if ($translationsCount == 0) {
176175
throw new DeepLException('No translations found.');
177-
}
178-
else if ($translationsCount == 1) {
176+
} elseif ($translationsCount == 1) {
179177
return $translationsArray['translations'][0]['text'];
180178
}
181-
else {
182-
return $translationsArray['translations'];
183-
}
179+
180+
return $translationsArray['translations'];
184181
}
185182

186183
/**
@@ -198,13 +195,17 @@ protected function checkLanguages($sourceLanguage, $destinationLanguage)
198195
$sourceLanguage = strtoupper($sourceLanguage);
199196

200197
if (!in_array($sourceLanguage, $this->sourceLanguages)) {
201-
throw new DeepLException(sprintf('The language "%s" is not supported as source language.', $sourceLanguage));
198+
throw new DeepLException(
199+
sprintf('The language "%s" is not supported as source language.', $sourceLanguage)
200+
);
202201
}
203202

204203
$destinationLanguage = strtoupper($destinationLanguage);
205204

206205
if (!in_array($destinationLanguage, $this->destinationLanguages)) {
207-
throw new DeepLException(sprintf('The language "%s" is not supported as destination language.', $destinationLanguage));
206+
throw new DeepLException(
207+
sprintf('The language "%s" is not supported as destination language.', $destinationLanguage)
208+
);
208209
}
209210

210211
return true;
@@ -225,8 +226,7 @@ protected function buildUrl(
225226
$destinationLanguage,
226227
array $tagHandling = array(),
227228
array $ignoreTags = array()
228-
)
229-
{
229+
) {
230230
// select correct api url
231231
switch ($this->apiVersion) {
232232
case 1:
@@ -271,7 +271,6 @@ protected function buildBody($text)
271271
}
272272

273273
foreach ($text as $textElement) {
274-
275274
$body .= ($first ? '' : '&') . sprintf(DeepL::API_URL_TEXT, rawurlencode($textElement));
276275

277276
if ($first) {
@@ -299,17 +298,16 @@ protected function request($url, $body)
299298

300299
$response = curl_exec($this->curl);
301300

302-
if (!curl_errno($this->curl)) {
303-
$httpCode = curl_getinfo($this->curl, CURLINFO_HTTP_CODE);
304-
305-
if ($httpCode != 200 && array_key_exists($httpCode, $this->errorCodes)) {
306-
throw new DeepLException($this->errorCodes[$httpCode], $httpCode);
307-
}
308-
}
309-
else {
301+
if (curl_errno($this->curl)) {
310302
throw new DeepLException('There was a cURL Request Error.');
311303
}
312304

305+
$httpCode = curl_getinfo($this->curl, CURLINFO_HTTP_CODE);
306+
307+
if ($httpCode != 200 && array_key_exists($httpCode, $this->errorCodes)) {
308+
throw new DeepLException($this->errorCodes[$httpCode], $httpCode);
309+
}
310+
313311
$translationsArray = json_decode($response, true);
314312

315313
if (!$translationsArray) {

src/DeepLException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@
1010
class DeepLException extends \Exception
1111
{
1212
// to be continued ..
13-
}
13+
}

tests/DeepLTest.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22

33
namespace BabyMarkt\DeepL;
44

5+
use ReflectionClass;
6+
57
/**
68
* Class DeepLTest
79
*
810
* @package BabyMarkt\DeepL
11+
* @SuppressWarnings(PHPMD.TooManyPublicMethods)
912
*/
1013
class DeepLTest extends \PHPUnit_Framework_TestCase
1114
{
@@ -23,7 +26,9 @@ public static function setUpBeforeClass()
2326
{
2427
parent::setUpBeforeClass();
2528

26-
if (($authKey = getenv('DEEPL_AUTH_KEY')) === false) {
29+
$authKey = getenv('DEEPL_AUTH_KEY');
30+
31+
if ($authKey === false) {
2732
return;
2833
}
2934

@@ -42,7 +47,7 @@ public static function setUpBeforeClass()
4247
*/
4348
protected static function getMethod($className, $methodName)
4449
{
45-
$class = new \ReflectionClass($className);
50+
$class = new ReflectionClass($className);
4651
$method = $class->getMethod($methodName);
4752
$method->setAccessible(true);
4853

@@ -116,7 +121,8 @@ public function testBuildUrl()
116121
*/
117122
public function testBuildUrlWithTags()
118123
{
119-
$expectedString = 'https://api.deepl.com/v1/translate?auth_key=123456&source_lang=de&target_lang=en&tag_handling=xml&ignore_tags=x';
124+
$expectedString = 'https://api.deepl.com';
125+
$expectedString .= '/v1/translate?auth_key=123456&source_lang=de&target_lang=en&tag_handling=xml&ignore_tags=x';
120126

121127
$authKey = '123456';
122128
$deepl = new DeepL($authKey);
@@ -252,6 +258,6 @@ public function testTranslateException()
252258

253259
$this->setExpectedException('\BabyMarkt\DeepL\DeepLException');
254260

255-
$translatedText = $deepl->translate($germanText);
261+
$deepl->translate($germanText);
256262
}
257-
}
263+
}

0 commit comments

Comments
 (0)