From dbe273dd3a48c57fcdd91c4ba7c81f67de151fed Mon Sep 17 00:00:00 2001 From: Vinicius Gava Date: Sat, 7 Oct 2017 18:45:09 -0300 Subject: [PATCH 1/9] - New architecture - Add support to composer - Partial Refactory - translate method - Add unit tests - minimum php required 5.6 - Add phpcs --- .gitignore | 2 + composer.json | 29 + composer.lock | 1616 +++++++++++++++++ phpcs.xml | 17 + phpunit.xml | 26 + src/Client.php | 185 ++ src/Exception/InvalidAccessKeyException.php | 15 + src/Exception/InvalidLanguageException.php | 15 + .../InvalidSourceLanguageException.php | 15 + .../InvalidTargetLanguageException.php | 15 + src/Exception/InvalidTextException.php | 15 + src/Exception/TranslationErrorException.php | 15 + src/TranslateInterface.php | 25 + tests/ClientTest.php | 192 ++ 14 files changed, 2182 insertions(+) create mode 100644 .gitignore create mode 100644 composer.json create mode 100644 composer.lock create mode 100644 phpcs.xml create mode 100644 phpunit.xml create mode 100644 src/Client.php create mode 100644 src/Exception/InvalidAccessKeyException.php create mode 100644 src/Exception/InvalidLanguageException.php create mode 100644 src/Exception/InvalidSourceLanguageException.php create mode 100644 src/Exception/InvalidTargetLanguageException.php create mode 100644 src/Exception/InvalidTextException.php create mode 100644 src/Exception/TranslationErrorException.php create mode 100644 src/TranslateInterface.php create mode 100644 tests/ClientTest.php diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0dca145 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/vendor/ +.idea diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..8a911d7 --- /dev/null +++ b/composer.json @@ -0,0 +1,29 @@ +{ + "name": "viniciusgava/google-translate-api", + "description": "Google translate API V2 client for PHP", + "license": "http://www.gnu.org/copyleft/gpl.html", + "authors": [ + { + "name": "Vinicius Gava", + "email": "gava.vinicius@gmail.com" + } + ], + "autoload": { + "psr-4": { + "GoogleTranslate\\": "src/" + } + }, + "autoload-dev": { + "psr-4": { + "GoogleTranslate\\Tests\\": "tests/" + } + }, + "require": { + "php": ">5.6", + "guzzlehttp/guzzle": "^6.3" + }, + "require-dev": { + "squizlabs/php_codesniffer": "*", + "phpunit/phpunit": "^5.7" + } +} diff --git a/composer.lock b/composer.lock new file mode 100644 index 0000000..19bf7a9 --- /dev/null +++ b/composer.lock @@ -0,0 +1,1616 @@ +{ + "_readme": [ + "This file locks the dependencies of your project to a known state", + "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", + "This file is @generated automatically" + ], + "content-hash": "ae6dfdcce2f8860b0d87246490275b0c", + "packages": [ + { + "name": "guzzlehttp/guzzle", + "version": "6.3.0", + "source": { + "type": "git", + "url": "https://github.com/guzzle/guzzle.git", + "reference": "f4db5a78a5ea468d4831de7f0bf9d9415e348699" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/guzzle/guzzle/zipball/f4db5a78a5ea468d4831de7f0bf9d9415e348699", + "reference": "f4db5a78a5ea468d4831de7f0bf9d9415e348699", + "shasum": "" + }, + "require": { + "guzzlehttp/promises": "^1.0", + "guzzlehttp/psr7": "^1.4", + "php": ">=5.5" + }, + "require-dev": { + "ext-curl": "*", + "phpunit/phpunit": "^4.0 || ^5.0", + "psr/log": "^1.0" + }, + "suggest": { + "psr/log": "Required for using the Log middleware" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "6.2-dev" + } + }, + "autoload": { + "files": [ + "src/functions_include.php" + ], + "psr-4": { + "GuzzleHttp\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Michael Dowling", + "email": "mtdowling@gmail.com", + "homepage": "https://github.com/mtdowling" + } + ], + "description": "Guzzle is a PHP HTTP client library", + "homepage": "http://guzzlephp.org/", + "keywords": [ + "client", + "curl", + "framework", + "http", + "http client", + "rest", + "web service" + ], + "time": "2017-06-22T18:50:49+00:00" + }, + { + "name": "guzzlehttp/promises", + "version": "v1.3.1", + "source": { + "type": "git", + "url": "https://github.com/guzzle/promises.git", + "reference": "a59da6cf61d80060647ff4d3eb2c03a2bc694646" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/guzzle/promises/zipball/a59da6cf61d80060647ff4d3eb2c03a2bc694646", + "reference": "a59da6cf61d80060647ff4d3eb2c03a2bc694646", + "shasum": "" + }, + "require": { + "php": ">=5.5.0" + }, + "require-dev": { + "phpunit/phpunit": "^4.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.4-dev" + } + }, + "autoload": { + "psr-4": { + "GuzzleHttp\\Promise\\": "src/" + }, + "files": [ + "src/functions_include.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Michael Dowling", + "email": "mtdowling@gmail.com", + "homepage": "https://github.com/mtdowling" + } + ], + "description": "Guzzle promises library", + "keywords": [ + "promise" + ], + "time": "2016-12-20T10:07:11+00:00" + }, + { + "name": "guzzlehttp/psr7", + "version": "1.4.2", + "source": { + "type": "git", + "url": "https://github.com/guzzle/psr7.git", + "reference": "f5b8a8512e2b58b0071a7280e39f14f72e05d87c" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/guzzle/psr7/zipball/f5b8a8512e2b58b0071a7280e39f14f72e05d87c", + "reference": "f5b8a8512e2b58b0071a7280e39f14f72e05d87c", + "shasum": "" + }, + "require": { + "php": ">=5.4.0", + "psr/http-message": "~1.0" + }, + "provide": { + "psr/http-message-implementation": "1.0" + }, + "require-dev": { + "phpunit/phpunit": "~4.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.4-dev" + } + }, + "autoload": { + "psr-4": { + "GuzzleHttp\\Psr7\\": "src/" + }, + "files": [ + "src/functions_include.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Michael Dowling", + "email": "mtdowling@gmail.com", + "homepage": "https://github.com/mtdowling" + }, + { + "name": "Tobias Schultze", + "homepage": "https://github.com/Tobion" + } + ], + "description": "PSR-7 message implementation that also provides common utility methods", + "keywords": [ + "http", + "message", + "request", + "response", + "stream", + "uri", + "url" + ], + "time": "2017-03-20T17:10:46+00:00" + }, + { + "name": "psr/http-message", + "version": "1.0.1", + "source": { + "type": "git", + "url": "https://github.com/php-fig/http-message.git", + "reference": "f6561bf28d520154e4b0ec72be95418abe6d9363" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/php-fig/http-message/zipball/f6561bf28d520154e4b0ec72be95418abe6d9363", + "reference": "f6561bf28d520154e4b0ec72be95418abe6d9363", + "shasum": "" + }, + "require": { + "php": ">=5.3.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0.x-dev" + } + }, + "autoload": { + "psr-4": { + "Psr\\Http\\Message\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "PHP-FIG", + "homepage": "http://www.php-fig.org/" + } + ], + "description": "Common interface for HTTP messages", + "homepage": "https://github.com/php-fig/http-message", + "keywords": [ + "http", + "http-message", + "psr", + "psr-7", + "request", + "response" + ], + "time": "2016-08-06T14:39:51+00:00" + } + ], + "packages-dev": [ + { + "name": "doctrine/instantiator", + "version": "1.0.5", + "source": { + "type": "git", + "url": "https://github.com/doctrine/instantiator.git", + "reference": "8e884e78f9f0eb1329e445619e04456e64d8051d" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/doctrine/instantiator/zipball/8e884e78f9f0eb1329e445619e04456e64d8051d", + "reference": "8e884e78f9f0eb1329e445619e04456e64d8051d", + "shasum": "" + }, + "require": { + "php": ">=5.3,<8.0-DEV" + }, + "require-dev": { + "athletic/athletic": "~0.1.8", + "ext-pdo": "*", + "ext-phar": "*", + "phpunit/phpunit": "~4.0", + "squizlabs/php_codesniffer": "~2.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0.x-dev" + } + }, + "autoload": { + "psr-4": { + "Doctrine\\Instantiator\\": "src/Doctrine/Instantiator/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Marco Pivetta", + "email": "ocramius@gmail.com", + "homepage": "http://ocramius.github.com/" + } + ], + "description": "A small, lightweight utility to instantiate objects in PHP without invoking their constructors", + "homepage": "https://github.com/doctrine/instantiator", + "keywords": [ + "constructor", + "instantiate" + ], + "time": "2015-06-14T21:17:01+00:00" + }, + { + "name": "myclabs/deep-copy", + "version": "1.6.1", + "source": { + "type": "git", + "url": "https://github.com/myclabs/DeepCopy.git", + "reference": "8e6e04167378abf1ddb4d3522d8755c5fd90d102" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/myclabs/DeepCopy/zipball/8e6e04167378abf1ddb4d3522d8755c5fd90d102", + "reference": "8e6e04167378abf1ddb4d3522d8755c5fd90d102", + "shasum": "" + }, + "require": { + "php": ">=5.4.0" + }, + "require-dev": { + "doctrine/collections": "1.*", + "phpunit/phpunit": "~4.1" + }, + "type": "library", + "autoload": { + "psr-4": { + "DeepCopy\\": "src/DeepCopy/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "description": "Create deep copies (clones) of your objects", + "homepage": "https://github.com/myclabs/DeepCopy", + "keywords": [ + "clone", + "copy", + "duplicate", + "object", + "object graph" + ], + "time": "2017-04-12T18:52:22+00:00" + }, + { + "name": "phpdocumentor/reflection-common", + "version": "1.0.1", + "source": { + "type": "git", + "url": "https://github.com/phpDocumentor/ReflectionCommon.git", + "reference": "21bdeb5f65d7ebf9f43b1b25d404f87deab5bfb6" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/phpDocumentor/ReflectionCommon/zipball/21bdeb5f65d7ebf9f43b1b25d404f87deab5bfb6", + "reference": "21bdeb5f65d7ebf9f43b1b25d404f87deab5bfb6", + "shasum": "" + }, + "require": { + "php": ">=5.5" + }, + "require-dev": { + "phpunit/phpunit": "^4.6" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0.x-dev" + } + }, + "autoload": { + "psr-4": { + "phpDocumentor\\Reflection\\": [ + "src" + ] + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Jaap van Otterdijk", + "email": "opensource@ijaap.nl" + } + ], + "description": "Common reflection classes used by phpdocumentor to reflect the code structure", + "homepage": "http://www.phpdoc.org", + "keywords": [ + "FQSEN", + "phpDocumentor", + "phpdoc", + "reflection", + "static analysis" + ], + "time": "2017-09-11T18:02:19+00:00" + }, + { + "name": "phpdocumentor/reflection-docblock", + "version": "4.1.1", + "source": { + "type": "git", + "url": "https://github.com/phpDocumentor/ReflectionDocBlock.git", + "reference": "2d3d238c433cf69caeb4842e97a3223a116f94b2" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/phpDocumentor/ReflectionDocBlock/zipball/2d3d238c433cf69caeb4842e97a3223a116f94b2", + "reference": "2d3d238c433cf69caeb4842e97a3223a116f94b2", + "shasum": "" + }, + "require": { + "php": "^7.0", + "phpdocumentor/reflection-common": "^1.0@dev", + "phpdocumentor/type-resolver": "^0.4.0", + "webmozart/assert": "^1.0" + }, + "require-dev": { + "mockery/mockery": "^0.9.4", + "phpunit/phpunit": "^4.4" + }, + "type": "library", + "autoload": { + "psr-4": { + "phpDocumentor\\Reflection\\": [ + "src/" + ] + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Mike van Riel", + "email": "me@mikevanriel.com" + } + ], + "description": "With this component, a library can provide support for annotations via DocBlocks or otherwise retrieve information that is embedded in a DocBlock.", + "time": "2017-08-30T18:51:59+00:00" + }, + { + "name": "phpdocumentor/type-resolver", + "version": "0.4.0", + "source": { + "type": "git", + "url": "https://github.com/phpDocumentor/TypeResolver.git", + "reference": "9c977708995954784726e25d0cd1dddf4e65b0f7" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/phpDocumentor/TypeResolver/zipball/9c977708995954784726e25d0cd1dddf4e65b0f7", + "reference": "9c977708995954784726e25d0cd1dddf4e65b0f7", + "shasum": "" + }, + "require": { + "php": "^5.5 || ^7.0", + "phpdocumentor/reflection-common": "^1.0" + }, + "require-dev": { + "mockery/mockery": "^0.9.4", + "phpunit/phpunit": "^5.2||^4.8.24" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0.x-dev" + } + }, + "autoload": { + "psr-4": { + "phpDocumentor\\Reflection\\": [ + "src/" + ] + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Mike van Riel", + "email": "me@mikevanriel.com" + } + ], + "time": "2017-07-14T14:27:02+00:00" + }, + { + "name": "phpspec/prophecy", + "version": "v1.7.2", + "source": { + "type": "git", + "url": "https://github.com/phpspec/prophecy.git", + "reference": "c9b8c6088acd19d769d4cc0ffa60a9fe34344bd6" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/phpspec/prophecy/zipball/c9b8c6088acd19d769d4cc0ffa60a9fe34344bd6", + "reference": "c9b8c6088acd19d769d4cc0ffa60a9fe34344bd6", + "shasum": "" + }, + "require": { + "doctrine/instantiator": "^1.0.2", + "php": "^5.3|^7.0", + "phpdocumentor/reflection-docblock": "^2.0|^3.0.2|^4.0", + "sebastian/comparator": "^1.1|^2.0", + "sebastian/recursion-context": "^1.0|^2.0|^3.0" + }, + "require-dev": { + "phpspec/phpspec": "^2.5|^3.2", + "phpunit/phpunit": "^4.8 || ^5.6.5" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.7.x-dev" + } + }, + "autoload": { + "psr-0": { + "Prophecy\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Konstantin Kudryashov", + "email": "ever.zet@gmail.com", + "homepage": "http://everzet.com" + }, + { + "name": "Marcello Duarte", + "email": "marcello.duarte@gmail.com" + } + ], + "description": "Highly opinionated mocking framework for PHP 5.3+", + "homepage": "https://github.com/phpspec/prophecy", + "keywords": [ + "Double", + "Dummy", + "fake", + "mock", + "spy", + "stub" + ], + "time": "2017-09-04T11:05:03+00:00" + }, + { + "name": "phpunit/php-code-coverage", + "version": "4.0.8", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/php-code-coverage.git", + "reference": "ef7b2f56815df854e66ceaee8ebe9393ae36a40d" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/ef7b2f56815df854e66ceaee8ebe9393ae36a40d", + "reference": "ef7b2f56815df854e66ceaee8ebe9393ae36a40d", + "shasum": "" + }, + "require": { + "ext-dom": "*", + "ext-xmlwriter": "*", + "php": "^5.6 || ^7.0", + "phpunit/php-file-iterator": "^1.3", + "phpunit/php-text-template": "^1.2", + "phpunit/php-token-stream": "^1.4.2 || ^2.0", + "sebastian/code-unit-reverse-lookup": "^1.0", + "sebastian/environment": "^1.3.2 || ^2.0", + "sebastian/version": "^1.0 || ^2.0" + }, + "require-dev": { + "ext-xdebug": "^2.1.4", + "phpunit/phpunit": "^5.7" + }, + "suggest": { + "ext-xdebug": "^2.5.1" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "4.0.x-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sb@sebastian-bergmann.de", + "role": "lead" + } + ], + "description": "Library that provides collection, processing, and rendering functionality for PHP code coverage information.", + "homepage": "https://github.com/sebastianbergmann/php-code-coverage", + "keywords": [ + "coverage", + "testing", + "xunit" + ], + "time": "2017-04-02T07:44:40+00:00" + }, + { + "name": "phpunit/php-file-iterator", + "version": "1.4.2", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/php-file-iterator.git", + "reference": "3cc8f69b3028d0f96a9078e6295d86e9bf019be5" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/php-file-iterator/zipball/3cc8f69b3028d0f96a9078e6295d86e9bf019be5", + "reference": "3cc8f69b3028d0f96a9078e6295d86e9bf019be5", + "shasum": "" + }, + "require": { + "php": ">=5.3.3" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.4.x-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sb@sebastian-bergmann.de", + "role": "lead" + } + ], + "description": "FilterIterator implementation that filters files based on a list of suffixes.", + "homepage": "https://github.com/sebastianbergmann/php-file-iterator/", + "keywords": [ + "filesystem", + "iterator" + ], + "time": "2016-10-03T07:40:28+00:00" + }, + { + "name": "phpunit/php-text-template", + "version": "1.2.1", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/php-text-template.git", + "reference": "31f8b717e51d9a2afca6c9f046f5d69fc27c8686" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/php-text-template/zipball/31f8b717e51d9a2afca6c9f046f5d69fc27c8686", + "reference": "31f8b717e51d9a2afca6c9f046f5d69fc27c8686", + "shasum": "" + }, + "require": { + "php": ">=5.3.3" + }, + "type": "library", + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de", + "role": "lead" + } + ], + "description": "Simple template engine.", + "homepage": "https://github.com/sebastianbergmann/php-text-template/", + "keywords": [ + "template" + ], + "time": "2015-06-21T13:50:34+00:00" + }, + { + "name": "phpunit/php-timer", + "version": "1.0.9", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/php-timer.git", + "reference": "3dcf38ca72b158baf0bc245e9184d3fdffa9c46f" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/php-timer/zipball/3dcf38ca72b158baf0bc245e9184d3fdffa9c46f", + "reference": "3dcf38ca72b158baf0bc245e9184d3fdffa9c46f", + "shasum": "" + }, + "require": { + "php": "^5.3.3 || ^7.0" + }, + "require-dev": { + "phpunit/phpunit": "^4.8.35 || ^5.7 || ^6.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sb@sebastian-bergmann.de", + "role": "lead" + } + ], + "description": "Utility class for timing", + "homepage": "https://github.com/sebastianbergmann/php-timer/", + "keywords": [ + "timer" + ], + "time": "2017-02-26T11:10:40+00:00" + }, + { + "name": "phpunit/php-token-stream", + "version": "2.0.1", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/php-token-stream.git", + "reference": "9a02332089ac48e704c70f6cefed30c224e3c0b0" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/php-token-stream/zipball/9a02332089ac48e704c70f6cefed30c224e3c0b0", + "reference": "9a02332089ac48e704c70f6cefed30c224e3c0b0", + "shasum": "" + }, + "require": { + "ext-tokenizer": "*", + "php": "^7.0" + }, + "require-dev": { + "phpunit/phpunit": "^6.2.4" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "2.0-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de" + } + ], + "description": "Wrapper around PHP's tokenizer extension.", + "homepage": "https://github.com/sebastianbergmann/php-token-stream/", + "keywords": [ + "tokenizer" + ], + "time": "2017-08-20T05:47:52+00:00" + }, + { + "name": "phpunit/phpunit", + "version": "5.7.22", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/phpunit.git", + "reference": "10df877596c9906d4110b5b905313829043f2ada" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/10df877596c9906d4110b5b905313829043f2ada", + "reference": "10df877596c9906d4110b5b905313829043f2ada", + "shasum": "" + }, + "require": { + "ext-dom": "*", + "ext-json": "*", + "ext-libxml": "*", + "ext-mbstring": "*", + "ext-xml": "*", + "myclabs/deep-copy": "~1.3", + "php": "^5.6 || ^7.0", + "phpspec/prophecy": "^1.6.2", + "phpunit/php-code-coverage": "^4.0.4", + "phpunit/php-file-iterator": "~1.4", + "phpunit/php-text-template": "~1.2", + "phpunit/php-timer": "^1.0.6", + "phpunit/phpunit-mock-objects": "^3.2", + "sebastian/comparator": "^1.2.4", + "sebastian/diff": "^1.4.3", + "sebastian/environment": "^1.3.4 || ^2.0", + "sebastian/exporter": "~2.0", + "sebastian/global-state": "^1.1", + "sebastian/object-enumerator": "~2.0", + "sebastian/resource-operations": "~1.0", + "sebastian/version": "~1.0.3|~2.0", + "symfony/yaml": "~2.1|~3.0" + }, + "conflict": { + "phpdocumentor/reflection-docblock": "3.0.2" + }, + "require-dev": { + "ext-pdo": "*" + }, + "suggest": { + "ext-xdebug": "*", + "phpunit/php-invoker": "~1.1" + }, + "bin": [ + "phpunit" + ], + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "5.7.x-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de", + "role": "lead" + } + ], + "description": "The PHP Unit Testing framework.", + "homepage": "https://phpunit.de/", + "keywords": [ + "phpunit", + "testing", + "xunit" + ], + "time": "2017-09-24T07:23:38+00:00" + }, + { + "name": "phpunit/phpunit-mock-objects", + "version": "3.4.4", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/phpunit-mock-objects.git", + "reference": "a23b761686d50a560cc56233b9ecf49597cc9118" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/phpunit-mock-objects/zipball/a23b761686d50a560cc56233b9ecf49597cc9118", + "reference": "a23b761686d50a560cc56233b9ecf49597cc9118", + "shasum": "" + }, + "require": { + "doctrine/instantiator": "^1.0.2", + "php": "^5.6 || ^7.0", + "phpunit/php-text-template": "^1.2", + "sebastian/exporter": "^1.2 || ^2.0" + }, + "conflict": { + "phpunit/phpunit": "<5.4.0" + }, + "require-dev": { + "phpunit/phpunit": "^5.4" + }, + "suggest": { + "ext-soap": "*" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "3.2.x-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sb@sebastian-bergmann.de", + "role": "lead" + } + ], + "description": "Mock Object library for PHPUnit", + "homepage": "https://github.com/sebastianbergmann/phpunit-mock-objects/", + "keywords": [ + "mock", + "xunit" + ], + "time": "2017-06-30T09:13:00+00:00" + }, + { + "name": "sebastian/code-unit-reverse-lookup", + "version": "1.0.1", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/code-unit-reverse-lookup.git", + "reference": "4419fcdb5eabb9caa61a27c7a1db532a6b55dd18" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/code-unit-reverse-lookup/zipball/4419fcdb5eabb9caa61a27c7a1db532a6b55dd18", + "reference": "4419fcdb5eabb9caa61a27c7a1db532a6b55dd18", + "shasum": "" + }, + "require": { + "php": "^5.6 || ^7.0" + }, + "require-dev": { + "phpunit/phpunit": "^5.7 || ^6.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0.x-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de" + } + ], + "description": "Looks up which function or method a line of code belongs to", + "homepage": "https://github.com/sebastianbergmann/code-unit-reverse-lookup/", + "time": "2017-03-04T06:30:41+00:00" + }, + { + "name": "sebastian/comparator", + "version": "1.2.4", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/comparator.git", + "reference": "2b7424b55f5047b47ac6e5ccb20b2aea4011d9be" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/comparator/zipball/2b7424b55f5047b47ac6e5ccb20b2aea4011d9be", + "reference": "2b7424b55f5047b47ac6e5ccb20b2aea4011d9be", + "shasum": "" + }, + "require": { + "php": ">=5.3.3", + "sebastian/diff": "~1.2", + "sebastian/exporter": "~1.2 || ~2.0" + }, + "require-dev": { + "phpunit/phpunit": "~4.4" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.2.x-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Jeff Welch", + "email": "whatthejeff@gmail.com" + }, + { + "name": "Volker Dusch", + "email": "github@wallbash.com" + }, + { + "name": "Bernhard Schussek", + "email": "bschussek@2bepublished.at" + }, + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de" + } + ], + "description": "Provides the functionality to compare PHP values for equality", + "homepage": "http://www.github.com/sebastianbergmann/comparator", + "keywords": [ + "comparator", + "compare", + "equality" + ], + "time": "2017-01-29T09:50:25+00:00" + }, + { + "name": "sebastian/diff", + "version": "1.4.3", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/diff.git", + "reference": "7f066a26a962dbe58ddea9f72a4e82874a3975a4" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/diff/zipball/7f066a26a962dbe58ddea9f72a4e82874a3975a4", + "reference": "7f066a26a962dbe58ddea9f72a4e82874a3975a4", + "shasum": "" + }, + "require": { + "php": "^5.3.3 || ^7.0" + }, + "require-dev": { + "phpunit/phpunit": "^4.8.35 || ^5.7 || ^6.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.4-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Kore Nordmann", + "email": "mail@kore-nordmann.de" + }, + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de" + } + ], + "description": "Diff implementation", + "homepage": "https://github.com/sebastianbergmann/diff", + "keywords": [ + "diff" + ], + "time": "2017-05-22T07:24:03+00:00" + }, + { + "name": "sebastian/environment", + "version": "2.0.0", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/environment.git", + "reference": "5795ffe5dc5b02460c3e34222fee8cbe245d8fac" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/environment/zipball/5795ffe5dc5b02460c3e34222fee8cbe245d8fac", + "reference": "5795ffe5dc5b02460c3e34222fee8cbe245d8fac", + "shasum": "" + }, + "require": { + "php": "^5.6 || ^7.0" + }, + "require-dev": { + "phpunit/phpunit": "^5.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "2.0.x-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de" + } + ], + "description": "Provides functionality to handle HHVM/PHP environments", + "homepage": "http://www.github.com/sebastianbergmann/environment", + "keywords": [ + "Xdebug", + "environment", + "hhvm" + ], + "time": "2016-11-26T07:53:53+00:00" + }, + { + "name": "sebastian/exporter", + "version": "2.0.0", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/exporter.git", + "reference": "ce474bdd1a34744d7ac5d6aad3a46d48d9bac4c4" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/exporter/zipball/ce474bdd1a34744d7ac5d6aad3a46d48d9bac4c4", + "reference": "ce474bdd1a34744d7ac5d6aad3a46d48d9bac4c4", + "shasum": "" + }, + "require": { + "php": ">=5.3.3", + "sebastian/recursion-context": "~2.0" + }, + "require-dev": { + "ext-mbstring": "*", + "phpunit/phpunit": "~4.4" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "2.0.x-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Jeff Welch", + "email": "whatthejeff@gmail.com" + }, + { + "name": "Volker Dusch", + "email": "github@wallbash.com" + }, + { + "name": "Bernhard Schussek", + "email": "bschussek@2bepublished.at" + }, + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de" + }, + { + "name": "Adam Harvey", + "email": "aharvey@php.net" + } + ], + "description": "Provides the functionality to export PHP variables for visualization", + "homepage": "http://www.github.com/sebastianbergmann/exporter", + "keywords": [ + "export", + "exporter" + ], + "time": "2016-11-19T08:54:04+00:00" + }, + { + "name": "sebastian/global-state", + "version": "1.1.1", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/global-state.git", + "reference": "bc37d50fea7d017d3d340f230811c9f1d7280af4" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/global-state/zipball/bc37d50fea7d017d3d340f230811c9f1d7280af4", + "reference": "bc37d50fea7d017d3d340f230811c9f1d7280af4", + "shasum": "" + }, + "require": { + "php": ">=5.3.3" + }, + "require-dev": { + "phpunit/phpunit": "~4.2" + }, + "suggest": { + "ext-uopz": "*" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de" + } + ], + "description": "Snapshotting of global state", + "homepage": "http://www.github.com/sebastianbergmann/global-state", + "keywords": [ + "global state" + ], + "time": "2015-10-12T03:26:01+00:00" + }, + { + "name": "sebastian/object-enumerator", + "version": "2.0.1", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/object-enumerator.git", + "reference": "1311872ac850040a79c3c058bea3e22d0f09cbb7" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/object-enumerator/zipball/1311872ac850040a79c3c058bea3e22d0f09cbb7", + "reference": "1311872ac850040a79c3c058bea3e22d0f09cbb7", + "shasum": "" + }, + "require": { + "php": ">=5.6", + "sebastian/recursion-context": "~2.0" + }, + "require-dev": { + "phpunit/phpunit": "~5" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "2.0.x-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de" + } + ], + "description": "Traverses array structures and object graphs to enumerate all referenced objects", + "homepage": "https://github.com/sebastianbergmann/object-enumerator/", + "time": "2017-02-18T15:18:39+00:00" + }, + { + "name": "sebastian/recursion-context", + "version": "2.0.0", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/recursion-context.git", + "reference": "2c3ba150cbec723aa057506e73a8d33bdb286c9a" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/recursion-context/zipball/2c3ba150cbec723aa057506e73a8d33bdb286c9a", + "reference": "2c3ba150cbec723aa057506e73a8d33bdb286c9a", + "shasum": "" + }, + "require": { + "php": ">=5.3.3" + }, + "require-dev": { + "phpunit/phpunit": "~4.4" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "2.0.x-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Jeff Welch", + "email": "whatthejeff@gmail.com" + }, + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de" + }, + { + "name": "Adam Harvey", + "email": "aharvey@php.net" + } + ], + "description": "Provides functionality to recursively process PHP variables", + "homepage": "http://www.github.com/sebastianbergmann/recursion-context", + "time": "2016-11-19T07:33:16+00:00" + }, + { + "name": "sebastian/resource-operations", + "version": "1.0.0", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/resource-operations.git", + "reference": "ce990bb21759f94aeafd30209e8cfcdfa8bc3f52" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/resource-operations/zipball/ce990bb21759f94aeafd30209e8cfcdfa8bc3f52", + "reference": "ce990bb21759f94aeafd30209e8cfcdfa8bc3f52", + "shasum": "" + }, + "require": { + "php": ">=5.6.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0.x-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de" + } + ], + "description": "Provides a list of PHP built-in functions that operate on resources", + "homepage": "https://www.github.com/sebastianbergmann/resource-operations", + "time": "2015-07-28T20:34:47+00:00" + }, + { + "name": "sebastian/version", + "version": "2.0.1", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/version.git", + "reference": "99732be0ddb3361e16ad77b68ba41efc8e979019" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/version/zipball/99732be0ddb3361e16ad77b68ba41efc8e979019", + "reference": "99732be0ddb3361e16ad77b68ba41efc8e979019", + "shasum": "" + }, + "require": { + "php": ">=5.6" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "2.0.x-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de", + "role": "lead" + } + ], + "description": "Library that helps with managing the version number of Git-hosted PHP projects", + "homepage": "https://github.com/sebastianbergmann/version", + "time": "2016-10-03T07:35:21+00:00" + }, + { + "name": "squizlabs/php_codesniffer", + "version": "3.1.0", + "source": { + "type": "git", + "url": "https://github.com/squizlabs/PHP_CodeSniffer.git", + "reference": "3c2d0a0fe39684ba0c1eb842a6a775d0b938d699" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/squizlabs/PHP_CodeSniffer/zipball/3c2d0a0fe39684ba0c1eb842a6a775d0b938d699", + "reference": "3c2d0a0fe39684ba0c1eb842a6a775d0b938d699", + "shasum": "" + }, + "require": { + "ext-simplexml": "*", + "ext-tokenizer": "*", + "ext-xmlwriter": "*", + "php": ">=5.4.0" + }, + "require-dev": { + "phpunit/phpunit": "^4.0 || ^5.0 || ^6.0" + }, + "bin": [ + "bin/phpcs", + "bin/phpcbf" + ], + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "3.x-dev" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Greg Sherwood", + "role": "lead" + } + ], + "description": "PHP_CodeSniffer tokenizes PHP, JavaScript and CSS files and detects violations of a defined set of coding standards.", + "homepage": "http://www.squizlabs.com/php-codesniffer", + "keywords": [ + "phpcs", + "standards" + ], + "time": "2017-09-19T22:47:14+00:00" + }, + { + "name": "symfony/yaml", + "version": "v3.3.10", + "source": { + "type": "git", + "url": "https://github.com/symfony/yaml.git", + "reference": "8c7bf1e7d5d6b05a690b715729cb4cd0c0a99c46" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/yaml/zipball/8c7bf1e7d5d6b05a690b715729cb4cd0c0a99c46", + "reference": "8c7bf1e7d5d6b05a690b715729cb4cd0c0a99c46", + "shasum": "" + }, + "require": { + "php": "^5.5.9|>=7.0.8" + }, + "require-dev": { + "symfony/console": "~2.8|~3.0" + }, + "suggest": { + "symfony/console": "For validating YAML files using the lint command" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "3.3-dev" + } + }, + "autoload": { + "psr-4": { + "Symfony\\Component\\Yaml\\": "" + }, + "exclude-from-classmap": [ + "/Tests/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Fabien Potencier", + "email": "fabien@symfony.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Symfony Yaml Component", + "homepage": "https://symfony.com", + "time": "2017-10-05T14:43:42+00:00" + }, + { + "name": "webmozart/assert", + "version": "1.2.0", + "source": { + "type": "git", + "url": "https://github.com/webmozart/assert.git", + "reference": "2db61e59ff05fe5126d152bd0655c9ea113e550f" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/webmozart/assert/zipball/2db61e59ff05fe5126d152bd0655c9ea113e550f", + "reference": "2db61e59ff05fe5126d152bd0655c9ea113e550f", + "shasum": "" + }, + "require": { + "php": "^5.3.3 || ^7.0" + }, + "require-dev": { + "phpunit/phpunit": "^4.6", + "sebastian/version": "^1.0.1" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.3-dev" + } + }, + "autoload": { + "psr-4": { + "Webmozart\\Assert\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Bernhard Schussek", + "email": "bschussek@gmail.com" + } + ], + "description": "Assertions to validate method input/output with nice error messages.", + "keywords": [ + "assert", + "check", + "validate" + ], + "time": "2016-11-23T20:04:58+00:00" + } + ], + "aliases": [], + "minimum-stability": "stable", + "stability-flags": [], + "prefer-stable": false, + "prefer-lowest": false, + "platform": { + "php": ">5.6" + }, + "platform-dev": [] +} diff --git a/phpcs.xml b/phpcs.xml new file mode 100644 index 0000000..8a8ef42 --- /dev/null +++ b/phpcs.xml @@ -0,0 +1,17 @@ + + + The Coding standard + + src/ + tests/ + + + + + + + + + */Tests/* + + \ No newline at end of file diff --git a/phpunit.xml b/phpunit.xml new file mode 100644 index 0000000..3f71fb9 --- /dev/null +++ b/phpunit.xml @@ -0,0 +1,26 @@ + + + + + + + + + + ./tests/ + + + + + + src + + + \ No newline at end of file diff --git a/src/Client.php b/src/Client.php new file mode 100644 index 0000000..22cebcd --- /dev/null +++ b/src/Client.php @@ -0,0 +1,185 @@ +accessKey = $accessKey; + + $this->httpClient = new HttpClient(); + if ($httpClient) { + $this->httpClient = $httpClient; + } + } + + /** + * @return \GuzzleHttp\ClientInterface + */ + private function getHttpClient() { + return $this->httpClient; + } + + /** + * @inheritdoc + */ + public function translate($text, $targetLanguage, &$sourceLanguage = null) + { + // used to return the same type of variable used in the text + $onceResult = !is_array($text); + + // prepare the string + $text = $this->prepareText($text); + + // validate if required fields has being filled. + if (!$text) { + throw new Exception\InvalidTextException(); + } + + if (!$this->isValidLanguage($targetLanguage)) { + throw new Exception\InvalidTargetLanguageException(); + } + + // query params + $query = [ + 'key' => $this->accessKey, + 'q' => $text, + 'target' => $targetLanguage, + ]; + + // validate if is necessary to pass the source language. + if ($sourceLanguage && !$this->isValidLanguage($sourceLanguage)) { + throw new Exception\InvalidSourceLanguageException(); + } + if ($sourceLanguage) { + $query['source'] = $sourceLanguage; + } + + try { + // send request + $query = $this->httpBuildQuery($query); + + $response = $this->getHttpClient()->request( + 'POST', + self::API_URI, + ['query' => $query] + ); + } + + catch (GuzzleException $e) { + throw new Exception\TranslationErrorException('Translation error: ' . $e->getMessage(), 4, $e); + } + + // check response json + $result = json_decode($response->getBody(), true); + if (!is_array($result) || !array_key_exists('data', $result) || !array_key_exists('translations', $result['data'])) { + throw new Exception\TranslationErrorException('Invalid response'); + } + + // prepare responses + $translations = []; + $sources = []; + foreach ($result['data']['translations'] as $translation) { + $translations[] = html_entity_decode($translation['translatedText'] , ENT_QUOTES, 'UTF-8'); + + if (array_key_exists('detectedSourceLanguage', $translation)) { + $sources[] = $translation['detectedSourceLanguage']; + } + } + + // add source language by reference if it was not passed. + if (!$sourceLanguage) { + $sourceLanguage = $onceResult ? current($sources) : $sources; + } + + return $onceResult ? current($translations) : $translations; + } + + /** + * Create a query string + * + * @param array $params + * @return string + */ + private function httpBuildQuery($params) + { + $query = []; + foreach ($params as $key => $param) { + if (!is_array($param)) { + continue; + } + // when a param has many values, it generate the query string separated to join late + foreach ($param as $subParam) { + $query[] = http_build_query([$key => $subParam]); + } + unset($params[$key]); + } + + // join queries strings + $query[] = http_build_query($params); + $query = implode('&', $query); + + return $query; + } + + /** + * Prepare text to be processed + * + * @param string|array $text + * @return array + */ + private function prepareText($text) + { + // convert no array text to array + if (is_array($text)) { + $text = [$text]; + } + + return $text; + } + + /** + * is a valid language? + * + * @param string $language language to be validate + * @return boolean + */ + private function isValidLanguage($language) { + $regexpValidLanguage = '%([a-z]{2})(-[a-z]{2})?%'; + + return preg_match($regexpValidLanguage, $language) === 1; + } +} diff --git a/src/Exception/InvalidAccessKeyException.php b/src/Exception/InvalidAccessKeyException.php new file mode 100644 index 0000000..8113fbc --- /dev/null +++ b/src/Exception/InvalidAccessKeyException.php @@ -0,0 +1,15 @@ +httpClientMock = $this->createMock(ClientInterface::class); + $this->client = new Client(self::ACCESS_KEY, $this->httpClientMock); + } + + public function tearDown() + { + $this->client = null; + } + + /** + * @expectedException \GoogleTranslate\Exception\InvalidAccessKeyException + * @expectedExceptionMessage Invalid access key + * @expectedExceptionCode 1 + */ + public function testInstanceClientWithInvalidAccessKeyShouldReturnInvalidAccessKeyException() + { + $this->client = new Client('asd'); + } + + /** + * @expectedException \GoogleTranslate\Exception\InvalidTextException + * @expectedExceptionMessage Invalid text + * @expectedExceptionCode 2 + */ + public function testTranslateMethodWithInvalidTextShouldReturnInvalidTextException() + { + $this->client->translate(null, 'en'); + } + + /** + * @expectedException \GoogleTranslate\Exception\InvalidTargetLanguageException + * @expectedExceptionMessage Invalid target language + * @expectedExceptionCode 3 + */ + public function testTranslateMethodWithInvalidTargetLanguageShouldReturnInvalidTargetLanguageException() + { + $this->client->translate('How are you?', '12'); + } + + /** + * @expectedException \GoogleTranslate\Exception\InvalidSourceLanguageException + * @expectedExceptionMessage Invalid source language + * @expectedExceptionCode 3 + */ + public function testTranslateMethodWithInvalidSourceLanguageShouldReturnInvalidSourceLanguageException() + { + $sourceLanguage = '34'; + $this->client->translate('How are you?', 'pt', $sourceLanguage); + } + + public function testTranslateMethodWithSingleStringShouldReturnTranslatedTextAsStringAndDetectedLanguageAsString() + { + $requestParams = [ + 'POST', + 'https://www.googleapis.com/language/translate/v2', + ['query' => 'key=' . self::ACCESS_KEY . '&q=What%27s+your+name%3F&target=pt'] + ]; + + $body = '{"data":{"translations":[{"translatedText":"Qual o seu nome?","detectedSourceLanguage":"en"}]}}'; + + $this->httpClientMock($requestParams, $body); + + $translatedText = $this->client->translate('What\'s your name?', 'pt', $sourceLanguage); + + $this->assertEquals('Qual o seu nome?', $translatedText); + $this->assertEquals('en', $sourceLanguage); + } + + public function testTranslateMethodWithMultipleStringShouldReturnTranslatedTextAsArrayAndDetectedLanguageAsArray() + { + $requestParams = [ + 'POST', + 'https://www.googleapis.com/language/translate/v2', + ['query' => 'q%5B0%5D=What%27s+your+name%3F%5D&q%5B1%5D=What+are+you+doing%3F&key=' . self::ACCESS_KEY . '&target=pt'] + ]; + + $body = '{"data":{"translations":[{"translatedText":"Qual o seu nome?","detectedSourceLanguage":"en"},{"translatedText":"O que você está fazendo?","detectedSourceLanguage":"en"}]}}'; + + $this->httpClientMock($requestParams, $body); + + $translatedText = $this->client->translate( + ['What\'s your name?]', 'What are you doing?'], + 'pt', + $sourceLanguage + ); + + $this->assertInternalType('array', $translatedText); + $this->assertEquals('Qual o seu nome?', $translatedText[0]); + $this->assertEquals('O que você está fazendo?', $translatedText[1]); + + $this->assertInternalType('array', $sourceLanguage); + $this->assertEquals('en', $sourceLanguage[0]); + $this->assertEquals('en', $sourceLanguage[0]); + } + + public function testTranslateMethodWithSingleStringAndSourceLanguageShouldReturnTranslatedTextAsString() + { + $requestParams = [ + 'POST', + 'https://www.googleapis.com/language/translate/v2', + ['query' => 'key=' . self::ACCESS_KEY . '&q=What%27s+your+name%3F&target=pt&source=en'] + ]; + + $body = '{"data":{"translations":[{"translatedText":"Qual o seu nome?"}]}}'; + + $this->httpClientMock($requestParams, $body); + + $sourceLanguage = 'en'; + + $translatedText = $this->client->translate('What\'s your name?', 'pt', $sourceLanguage); + + $this->assertEquals('Qual o seu nome?', $translatedText); + } + + public function testTranslateMethodWithSingleAndInvalidTargetLanguageShouldReturnTranslationErrorException() + { + $this->expectException('\GoogleTranslate\Exception\TranslationErrorException'); + $this->expectExceptionMessage('Translation error: Client error: `POST https://www.googleapis.com/language/translate/v2?key=' . self::ACCESS_KEY . '&q=estou+aqui&target=aa` resulted in a `400 Bad Request` response:'); + $this->expectExceptionCode(4); + + $requestParams = [ + 'POST', + 'https://www.googleapis.com/language/translate/v2', + ['query' => 'key=' . self::ACCESS_KEY . '&q=What%27s+your+name%3F&target=aa'] + ]; + + $mockGuzzleException = new TransferException('Client error: `POST https://www.googleapis.com/language/translate/v2?key=' . self::ACCESS_KEY . '&q=estou+aqui&target=aa` resulted in a `400 Bad Request` response:'); + $this->httpClientMock->method('request') + ->withConsecutive($requestParams) + ->willThrowException($mockGuzzleException); + + $this->client->translate('What\'s your name?', 'aa', $sourceLanguage); + } + + /** + * @expectedException \GoogleTranslate\Exception\TranslationErrorException + * @expectedExceptionMessage Invalid response + * @expectedExceptionCode 4 + */ + public function testTranslateMethodWithSingleStringAndSourceLanguageAndMalformedJsonResponseShouldReturnTranslationErrorException() + { + $requestParams = [ + 'POST', + 'https://www.googleapis.com/language/translate/v2', + ['query' => 'key=' . self::ACCESS_KEY . '&q=What%27s+your+name%3F&target=pt'] + ]; + + $body = '{"data":{}}'; + + $this->httpClientMock($requestParams, $body); + $this->client->translate('What\'s your name?', 'pt', $sourceLanguage); + } + + public function httpClientMock($requestParams, $body) + { + $responseMock = $this->createMock(ResponseInterface::class); + $responseMock->method('getBody') + ->willReturn($body); + + $this->httpClientMock->method('request') + ->withConsecutive($requestParams) + ->willReturn($responseMock); + } +} From 1d015437262325cbab7a7798d044230bcb6074c2 Mon Sep 17 00:00:00 2001 From: Vinicius Gava Date: Sat, 7 Oct 2017 19:47:19 -0300 Subject: [PATCH 2/9] Refactory languages method --- src/Client.php | 46 +++++++- src/Exception/LanguagesErrorException.php | 15 +++ src/LanguagesInterface.php | 23 ++++ tests/ClientTest.php | 123 ++++++++++++++++++++++ 4 files changed, 203 insertions(+), 4 deletions(-) create mode 100644 src/Exception/LanguagesErrorException.php create mode 100644 src/LanguagesInterface.php diff --git a/src/Client.php b/src/Client.php index 22cebcd..9cf4ec4 100644 --- a/src/Client.php +++ b/src/Client.php @@ -6,7 +6,7 @@ use GuzzleHttp\ClientInterface; use GuzzleHttp\Exception\GuzzleException; -class Client implements TranslateInterface +class Client implements TranslateInterface, LanguagesInterface { /** * API URI @@ -97,9 +97,7 @@ public function translate($text, $targetLanguage, &$sourceLanguage = null) self::API_URI, ['query' => $query] ); - } - - catch (GuzzleException $e) { + } catch (GuzzleException $e) { throw new Exception\TranslationErrorException('Translation error: ' . $e->getMessage(), 4, $e); } @@ -128,6 +126,46 @@ public function translate($text, $targetLanguage, &$sourceLanguage = null) return $onceResult ? current($translations) : $translations; } + /** + * @inheritdoc + */ + public function languages($targetLanguage = null) + { + if ($targetLanguage && !$this->isValidLanguage($targetLanguage)) { + throw new Exception\InvalidTargetLanguageException(); + } + + // query params + $query = [ + 'key' => $this->accessKey, + ]; + + if ($targetLanguage) { + $query['target'] = $targetLanguage; + } + + try { + // send request + $query = $this->httpBuildQuery($query); + + $response = $this->getHttpClient()->request( + 'GET', + self::API_URI . '/languages', + ['query' => $query] + ); + } catch (GuzzleException $e) { + throw new Exception\LanguagesErrorException('Languages error: ' . $e->getMessage(), 5, $e); + } + + // check response json + $result = json_decode($response->getBody(), true); + if (!is_array($result) || !array_key_exists('data', $result) || !array_key_exists('languages', $result['data'])) { + throw new Exception\LanguagesErrorException('Invalid response'); + } + + return $result['data']['languages']; + } + /** * Create a query string * diff --git a/src/Exception/LanguagesErrorException.php b/src/Exception/LanguagesErrorException.php new file mode 100644 index 0000000..9b65249 --- /dev/null +++ b/src/Exception/LanguagesErrorException.php @@ -0,0 +1,15 @@ + (string) Supported language code, generally consisting of its ISO 639-1 identifier, + * (string) 'name' => (string) Human readable name of the language localized to the target language + * ] + * + * @param string $targetLanguage Target language. ie: pt, en, es + * @return array + * + * @throws Exception\InvalidTargetLanguageException + * @throws Exception\TranslationErrorException + */ + public function languages($targetLanguage = null); +} diff --git a/tests/ClientTest.php b/tests/ClientTest.php index 98f56ff..97583cc 100644 --- a/tests/ClientTest.php +++ b/tests/ClientTest.php @@ -179,6 +179,129 @@ public function testTranslateMethodWithSingleStringAndSourceLanguageAndMalformed $this->client->translate('What\'s your name?', 'pt', $sourceLanguage); } + /** + * @expectedException \GoogleTranslate\Exception\InvalidTargetLanguageException + * @expectedExceptionMessage Invalid target language + * @expectedExceptionCode 3 + */ + public function testLanguagesMethodWithInvalidTargetLanguageShouldReturnInvalidTargetLanguageException() + { + $this->client->languages('12'); + } + + public function testLanguagesMethodWithTargetLanguageShouldReturnLanguagesSupportedCodeAndName() + { + $requestParams = [ + 'GET', + 'https://www.googleapis.com/language/translate/v2/languages', + ['query' => 'key=' . self::ACCESS_KEY . '&target=pt-br'] + ]; + + $body = '{"data":{"languages":[{"language":"de","name":"Alemão"},{"language":"ga","name":"Irlandês"},{"language":"it","name":"Italiano"},{"language":"ja","name":"Japonês"}]}}'; + + $this->httpClientMock($requestParams, $body); + + $languages = $this->client->languages('pt-br'); + + $this->assertInternalType('array', $languages); + $this->assertEquals(4, count($languages)); + + $expectedValues = [ + [ + 'language' => 'de', + 'name' => 'Alemão' + ], + [ + 'language' => 'ga', + 'name' => 'Irlandês' + ], + [ + 'language' => 'it', + 'name' => 'Italiano' + ], + [ + 'language' => 'ja', + 'name' => 'Japonês' + ] + ]; + + foreach ($languages as $index => $language) { + $this->assertArrayHasKey('language', $language); + $this->assertArrayHasKey('name', $language); + $this->assertEquals($expectedValues[$index]['language'], $language['language']); + $this->assertEquals($expectedValues[$index]['name'], $language['name']); + } + } + + public function testLanguagesMethodShouldReturnLanguagesSupportedCodes() + { + $requestParams = [ + 'GET', + 'https://www.googleapis.com/language/translate/v2/languages', + ['query' => 'key=' . self::ACCESS_KEY . '&target=pt-br'] + ]; + + $body = '{"data":{"languages":[{"language":"de"},{"language":"ga"},{"language":"it"},{"language":"ja"}]}}'; + + $this->httpClientMock($requestParams, $body); + + $languages = $this->client->languages('pt-br'); + + $this->assertInternalType('array', $languages); + $this->assertEquals(4, count($languages)); + + $expectedValues = [ + ['language' => 'de'], + ['language' => 'ga'], + ['language' => 'it'], + ['language' => 'ja'] + ]; + + foreach ($languages as $index => $language) { + $this->assertArrayHasKey('language', $language); + $this->assertEquals($expectedValues[$index]['language'], $language['language']); + } + } + + public function testLanguagesMethodWithInvalidTargetLanguageShouldReturnLanguagesErrorException() + { + $this->expectException('\GoogleTranslate\Exception\LanguagesErrorException'); + $this->expectExceptionMessage('Languages error: Client error: `GET https://www.googleapis.com/language/translate/v2/languages?key=' . self::ACCESS_KEY . '&target=aa-aa` resulted in a `400 Bad Request` response:'); + $this->expectExceptionCode(5); + + $requestParams = [ + 'GET', + 'https://www.googleapis.com/language/translate/v2/languages', + ['query' => 'key=' . self::ACCESS_KEY . '&target=aa-aa'] + ]; + + $mockGuzzleException = new TransferException('Client error: `GET https://www.googleapis.com/language/translate/v2/languages?key=' . self::ACCESS_KEY . '&target=aa-aa` resulted in a `400 Bad Request` response:'); + $this->httpClientMock->method('request') + ->withConsecutive($requestParams) + ->willThrowException($mockGuzzleException); + + $this->client->languages('aa-aa'); + } + + /** + * @expectedException \GoogleTranslate\Exception\LanguagesErrorException + * @expectedExceptionMessage Invalid response + * @expectedExceptionCode 5 + */ + public function testLanguagesMethodWithMalformedJsonResponseShouldReturnLanguagesErrorException() + { + $requestParams = [ + 'GET', + 'https://www.googleapis.com/language/translate/v2/languages', + ['query' => 'key=' . self::ACCESS_KEY] + ]; + + $body = '{"data":{}}'; + + $this->httpClientMock($requestParams, $body); + $this->client->languages(); + } + public function httpClientMock($requestParams, $body) { $responseMock = $this->createMock(ResponseInterface::class); From c3dc559e37f9c68a328793259c5bfd12da8bd696 Mon Sep 17 00:00:00 2001 From: Vinicius Gava Date: Sun, 8 Oct 2017 00:15:47 -0300 Subject: [PATCH 3/9] - Refactory detect method - Remove legacy lib - PHP doc improvements --- GoogleTranslate.php | 371 ------------------ src/Client.php | 54 ++- src/DetectInterface.php | 26 ++ src/Exception/DetectErrorException.php | 15 + src/Exception/InvalidAccessKeyException.php | 2 +- src/Exception/InvalidLanguageException.php | 2 +- .../InvalidSourceLanguageException.php | 2 +- .../InvalidTargetLanguageException.php | 2 +- src/Exception/InvalidTextException.php | 2 +- src/Exception/LanguagesErrorException.php | 2 +- src/Exception/TranslationErrorException.php | 2 +- src/LanguagesInterface.php | 9 +- src/TranslateInterface.php | 3 +- tests/ClientTest.php | 119 ++++++ 14 files changed, 228 insertions(+), 383 deletions(-) delete mode 100644 GoogleTranslate.php create mode 100644 src/DetectInterface.php create mode 100644 src/Exception/DetectErrorException.php diff --git a/GoogleTranslate.php b/GoogleTranslate.php deleted file mode 100644 index 74304d1..0000000 --- a/GoogleTranslate.php +++ /dev/null @@ -1,371 +0,0 @@ -setAccessKey($accessKey); - } - /** - * Set access key - * @param string $key - */ - public function setAccessKey($key) { - if (strlen($key) == 39) { - $this->accessKey = $key; - } else { - throw new GoogleTranslateInvalidKey(); - } - } - - /** - * Translate text - * @param string|array $text The text to be translated - * @param string $targetLanguage The language to translate the source text into - * @param string|null|array $sourceLanguage The language of the source text. If a language is not specified, the system will attempt to identify the source language automatically - */ - public function translate($text, $targetLanguage, &$sourceLanguage = null) { - if ($this->isValid($text, $targetLanguage, $sourceLanguage)) { - reset($text); - //add keyAccess - $this->addQueryParam('key', $this->accessKey); - //add text to be translate - $this->addQueryParam('q', $text); - //add target language - $this->addQueryParam('target', $targetLanguage); - //if source not null, add param to query - if(!is_null($sourceLanguage)){ - $this->addQueryParam('source', $sourceLanguage); - } - //init connect - $this->initConnect(); - //get content - $result = $this->execConnect(); - //close connect - $this->closeConnect(); - //verify this is multiple text - if (!is_array($text)) { - //get only info necessary - $result = current($result->translations); - - if (isset($result->detectedSourceLanguage)) { - //return by reference the language in case detected language - $sourceLanguage = $result->detectedSourceLanguage; - } - - //return translate - return $result->translatedText; - } else { - //this is multiple text - //get only info necessary - $result = $result->translations; - //save translate list - $arrTranslateReturn = array(); - //save source list - $arrSourceReturn = array(); - //get translates - foreach ($result as $itemResult) { - $arrTranslateReturn[] = $itemResult->translatedText; - if (isset($itemResult->detectedSourceLanguage)) { - $arrSourceReturn[] = $itemResult->detectedSourceLanguage; - } - } - //return by reference the language in case detected language - $sourceLanguage = $arrSourceReturn; - //return list of translate - return $arrTranslateReturn; - } - } else { - return false; - } - } - - /** - * Deletect Language - * @param string|array $text The text or list the text to be detect - * @param null|string|array $isReliable is reliable - * @return string|array language or list of language detected - */ - public function detect($text, &$isReliable = null) { - if ($this->isValid($text, null, null, false)) { - reset($text); - - //add keyAccess - $this->addQueryParam('key', $this->accessKey); - //add text to be translate - $this->addQueryParam('q', $text); - //init connect - $this->initConnect(self::SERVICE_DETECT); - //get content - $result = $this->execConnect(); - //close connect - $this->closeConnect(); - //verify this is multiple text - if (count($result->detections) == 1) { - //get only info necessary - $result = current(current($result->detections)); - //return by reference the language is realiable - $isReliable = $result->isReliable; - //return translate - return $result->language; - } else { - //this is multiple text - //get only info necessary - $result = $result->detections; - //save is reliable list - $arrIsReliable = array(); - //save detect language list - $arrSourceReturn = array(); - //get translates - foreach ($result as $itemResult) { - $itemResult = current($itemResult); - $arrIsReliable[] = $itemResult->isReliable; - $arrSourceReturn[] = $itemResult->language; - } - //return by reference the language is realiable - $isReliable = $arrIsReliable; - //return list of detect language - return $arrSourceReturn; - } - } else { - return false; - } - } - - /** - * Language support - * @param string|null $target language target - */ - public function languageSupport($target = null) { - //add keyAccess - $this->addQueryParam('key', $this->accessKey); - if(!is_null($target)){ - if ($this->validLanguage($target)) { - $this->addQueryParam('target', $target); - }else{ - return false; - } - } - //init connect - $this->initConnect(self::SERVICE_LANGUAGE); - //get content - $result = $this->execConnect(); - //close connect - $this->closeConnect(); - - //get only info necessary - $result = $result->languages; - //return list of language support - return $result; - - } - - /** - * Validate info - * @param string|array $text The text or list the text to be validate - * @param string $targetLanguage target language to be validate - * @param string $sourceLanguage source language to be validate - * @param boolean $targetRequired the target language is required? - * @return boolean - */ - private function isValid(&$text, $targetLanguage = null, $sourceLanguage = null, $targetRequired = true) { - //in case of numeric only in text, return false - if (!is_array($text)) { - $text = array($text); - } - //valid list of translate - foreach ($text as $keyText => $oneText) { - //is numeric? - if (is_numeric($oneText) || strlen($oneText) < 2) { - //remove from list translate - unset($text[$keyText]); - } - } - //no text valid? - if (count($text) <= 0) { - return false; - } - //target required? - if ($targetRequired) { - //is valid language target - if (!$this->validLanguage($targetLanguage)) { - return false; - } - } - //is valid language source - if (!is_null($sourceLanguage)) { - if (!$this->validLanguage($sourceLanguage)) { - return false; - } - } - //valid! - return true; - } - - /** - * validate language - * @param string $lang language text to be validate - * @return boolean - */ - private function validLanguage($lang) { - $regexpValidLanguage = '%([a-z]{2})(-[a-z]{2})?%'; - if (preg_match($regexpValidLanguage, $lang) == 0) { - return false; - } else { - return true; - } - } - - /** - * Init the connect with parameters find - */ - private function initConnect($service = self::SERVICE_TRANSLATE) { - //choose service - switch ($service) { - case self::SERVICE_DETECT : $url = $this->apiUri . '/detect'; - break; - case self::SERVICE_LANGUAGE : $url = $this->apiUri . '/languages'; - break; - case self::SERVICE_TRANSLATE : default: $url = $this->apiUri; - } - //case exists parameters, add to url - if (count($this->parameters) > 0) { - $url .= '?'; - //add for each item - foreach ($this->parameters as $keyParam => $param) { - //used in case de multiple text translate - if (is_array($param)) { - foreach ($param as $subParam) { - $url .= '&' . urlencode($keyParam) . '=' . urlencode($subParam); - } - } else { - $url .= '&' . urlencode($keyParam) . '=' . urlencode($param); - } - } - } - //init curl - $this->connect = curl_init($url); - //return data receive - curl_setopt($this->connect, CURLOPT_RETURNTRANSFER, true); - } - - /** - * Add Query param in connect - * @param type $key - * @param type $value - */ - private function addQueryParam($key, $value) { - //remove possible whitespaces, utf8 encode AND add to params list - if (is_array($value)) { - foreach ($value as $keyValue => $itemValue) { - $value[$keyValue] = utf8_encode($itemValue); - } - } else { - $value = utf8_encode($value); - } - //add to param list - $this->parameters[utf8_encode(str_replace(' ', '', $key))] = $value; - } - - /** - * Close the connect - */ - private function closeConnect() { - //close curl connect - curl_close($this->connect); - //clear params to next request - $this->parameters = array(); - } - /** - * Execute connect and return array with data - * @return array - */ - private function execConnect() { - //exec curl - $result = curl_exec($this->connect); - //transform json in stdClass - $result = json_decode($result); - - //get request info - $arrInfo = curl_getinfo($this->connect); - //found? - if ($arrInfo['http_code'] == 404) { - //no connect - throw new GoogleTranslateNotFoundException(); - } - - if (($arrInfo['http_code'] == 200 || $arrInfo['http_code'] == 304) && !isset($result->error)) { - //request ok, return data - return $result->data; - } else { - //invalid key - throw new GoogleTranslateInvalidKeyException(); - } - } - -} - -/** - * Google Translate Exception Invalid Access Key - */ -class GoogleTranslateInvalidKeyException extends Exception { - - function __construct() { - parent::__construct('Invalid Access Key'); - } - -} - -/** - * Google Translate Exception Not found 404, probable problem in connect internert - */ -class GoogleTranslateNotFoundException extends Exception { - - function __construct() { - parent::__construct('Not Found Request', 404); - } -} diff --git a/src/Client.php b/src/Client.php index 9cf4ec4..aca613e 100644 --- a/src/Client.php +++ b/src/Client.php @@ -6,7 +6,7 @@ use GuzzleHttp\ClientInterface; use GuzzleHttp\Exception\GuzzleException; -class Client implements TranslateInterface, LanguagesInterface +class Client implements TranslateInterface, LanguagesInterface, DetectInterface { /** * API URI @@ -166,6 +166,58 @@ public function languages($targetLanguage = null) return $result['data']['languages']; } + /** + * @inheritdoc + */ + public function detect($text) + { + // used to return the same type of variable used in the text + $onceResult = !is_array($text); + + // prepare the string + $text = $this->prepareText($text); + + // validate if required fields has being filled. + if (!$text) { + throw new Exception\InvalidTextException(); + } + + // query params + $query = [ + 'key' => $this->accessKey, + 'q' => $text + ]; + + try { + // send request + $query = $this->httpBuildQuery($query); + + $response = $this->getHttpClient()->request( + 'POST', + self::API_URI . '/detect', + ['query' => $query] + ); + } catch (GuzzleException $e) { + throw new Exception\DetectErrorException('Detect error: ' . $e->getMessage(), 6, $e); + } + + // check response json + $result = json_decode($response->getBody(), true); + if (!is_array($result) || !array_key_exists('data', $result) || !array_key_exists('detections', $result['data'])) { + throw new Exception\DetectErrorException('Invalid response'); + } + + $result = $result['data']['detections']; + + // remove array of array in the results + $processedResult = []; + foreach ($result as $item) { + $processedResult[] = current($item); + } + + return $onceResult ? current($processedResult) : $processedResult; + } + /** * Create a query string * diff --git a/src/DetectInterface.php b/src/DetectInterface.php new file mode 100644 index 0000000..f82d8b0 --- /dev/null +++ b/src/DetectInterface.php @@ -0,0 +1,26 @@ + (string) Supported language code, generally consisting of its ISO 639-1 identifier, + * (string) 'name' => (string) Human readable name of the language localized to the target language + * ] + * + * @param string|array $text String or multiple strings(array) to be detected + * @return array array structure return above + * + * @throws Exception\InvalidTextException + * @throws Exception\DetectErrorException + */ + public function detect($text); +} diff --git a/src/Exception/DetectErrorException.php b/src/Exception/DetectErrorException.php new file mode 100644 index 0000000..831e0f1 --- /dev/null +++ b/src/Exception/DetectErrorException.php @@ -0,0 +1,15 @@ + (string) Supported language code, generally consisting of its ISO 639-1 identifier, - * (string) 'name' => (string) Human readable name of the language localized to the target language + * [ + * (string) 'language' => (string) Supported language code, generally consisting of its ISO 639-1 id, + * (string) 'name' => (string) Human readable name of the language localized to the target language + * ] + * (...) * ] * * @param string $targetLanguage Target language. ie: pt, en, es - * @return array + * @return array array structure return above * * @throws Exception\InvalidTargetLanguageException * @throws Exception\TranslationErrorException diff --git a/src/TranslateInterface.php b/src/TranslateInterface.php index 7a37a4d..2e11ff5 100644 --- a/src/TranslateInterface.php +++ b/src/TranslateInterface.php @@ -6,6 +6,7 @@ interface TranslateInterface { /** * Translate a text or multiple texts + * * If pass a string as text, it will return a string. * If pass a array as text, it will return a array. * If you do not fill the source language param, but you pass a variable, you can capture the detected @@ -14,7 +15,7 @@ interface TranslateInterface * @param string|array $text String or multiple strings(array) to be translated * @param string $targetLanguage Target language. ie: pt, en, es * @param null|string|array $sourceLanguage Source language. If not passed, google will try figure out it. - * @return string|array + * @return string|array Return retails above * * @throws Exception\InvalidTextException * @throws Exception\InvalidTargetLanguageException diff --git a/tests/ClientTest.php b/tests/ClientTest.php index 97583cc..ca65d6d 100644 --- a/tests/ClientTest.php +++ b/tests/ClientTest.php @@ -302,6 +302,125 @@ public function testLanguagesMethodWithMalformedJsonResponseShouldReturnLanguage $this->client->languages(); } + /** + * @expectedException \GoogleTranslate\Exception\InvalidTextException + * @expectedExceptionMessage Invalid text + * @expectedExceptionCode 2 + */ + public function testDetectMethodWithInvalidTextShouldReturnInvalidTextException() + { + $this->client->detect(null); + } + + public function testDetectMethodWithSingleStringShouldReturnLanguageAndConfidenceLevelAndIsReliable() + { + $requestParams = [ + 'POST', + 'https://www.googleapis.com/language/translate/v2/detect', + ['query' => 'key=' . self::ACCESS_KEY . '&q=What%27s+your+name%3F'] + ]; + + $body = '{"data":{"detections":[[{"confidence":0.25199580192565918,"isReliable":false,"language":"en"}]]}}'; + + $this->httpClientMock($requestParams, $body); + + $detection = $this->client->detect('What\'s your name?'); + + $this->assertInternalType('array', $detection); + + $this->assertArrayHasKey('confidence', $detection); + $this->assertEquals(0.25199580192565918, $detection['confidence']); + + $this->assertArrayHasKey('isReliable', $detection); + $this->assertFalse($detection['isReliable']); + + $this->assertArrayHasKey('language', $detection); + $this->assertEquals('en', $detection['language']); + } + + public function testDetectMethodWithMultipleStringShouldReturnLanguageAndConfidenceLevelAndIsReliable() + { + $requestParams = [ + 'POST', + 'https://www.googleapis.com/language/translate/v2/detect', + ['query' => 'q%5B0%5D=What%27s+your+name%3F&q%5B1%5D=Quem+%C3%A9+voc%C3%AA%3F&key=' . self::ACCESS_KEY] + ]; + + $body = '{"data":{"detections":[[{"isReliable":false,"language":"en","confidence":0.25199580192565918}],[{"confidence":0.28993061184883118,"isReliable":false,"language":"pt"}]]}}'; + + $this->httpClientMock($requestParams, $body); + + $expectedValues = [ + [ + 'isReliable' => false, + 'language' => 'en', + 'confidence' => 0.25199580192565918 + ], + [ + 'isReliable' => false, + 'language' => 'pt', + 'confidence' => 0.28993061184883118 + ] + ]; + + $detections = $this->client->detect([ + 'What\'s your name?', + 'Quem é você?' + ]); + + $this->assertInternalType('array', $detections); + + foreach ($detections as $index => $detection) { + $this->assertArrayHasKey('confidence', $detection); + $this->assertEquals($expectedValues[$index]['confidence'], $detection['confidence']); + + $this->assertArrayHasKey('isReliable', $detection); + $this->assertEquals($expectedValues[$index]['isReliable'], $detection['isReliable']); + + $this->assertArrayHasKey('language', $detection); + $this->assertEquals($expectedValues[$index]['language'], $detection['language']); + } + } + + /** + * @expectedException \GoogleTranslate\Exception\DetectErrorException + * @expectedExceptionMessage Invalid response + * @expectedExceptionCode 6 + */ + public function testDetectMethodWithSingleStringAndMalformedJsonResponseShouldReturnDetectErrorException() + { + $requestParams = [ + 'POST', + 'https://www.googleapis.com/language/translate/v2/detect', + ['query' => 'key=' . self::ACCESS_KEY . '&q=What%27s+your+name%3F'] + ]; + + $body = '{"data":{}}'; + + $this->httpClientMock($requestParams, $body); + $this->client->detect('What\'s your name?'); + } + + public function testDetectMethodWithSingleStringShouldReturnTranslationErrorException() + { + $this->expectException('\GoogleTranslate\Exception\DetectErrorException'); + $this->expectExceptionMessage('Detect error: Client error: `POST https://www.googleapis.com/language/translate/v2/detect?key=' . self::ACCESS_KEY . '&q=What%27s+your+name%3F` resulted in a `400 Bad Request` response:'); + $this->expectExceptionCode(6); + + $requestParams = [ + 'POST', + 'https://www.googleapis.com/language/translate/v2/detect', + ['query' => 'key=' . self::ACCESS_KEY . '&q=What%27s+your+name%3F'] + ]; + + $mockGuzzleException = new TransferException('Client error: `POST https://www.googleapis.com/language/translate/v2/detect?key=' . self::ACCESS_KEY . '&q=What%27s+your+name%3F` resulted in a `400 Bad Request` response:'); + $this->httpClientMock->method('request') + ->withConsecutive($requestParams) + ->willThrowException($mockGuzzleException); + + $this->client->detect('What\'s your name?'); + } + public function httpClientMock($requestParams, $body) { $responseMock = $this->createMock(ResponseInterface::class); From 406ae6dc9c8f98c89f513713aee9b465c714b758 Mon Sep 17 00:00:00 2001 From: Vinicius Gava Date: Sun, 8 Oct 2017 00:19:19 -0300 Subject: [PATCH 4/9] phpcs fixes --- src/Client.php | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/src/Client.php b/src/Client.php index aca613e..bd98ec5 100644 --- a/src/Client.php +++ b/src/Client.php @@ -49,7 +49,8 @@ public function __construct($accessKey, ClientInterface $httpClient = null) /** * @return \GuzzleHttp\ClientInterface */ - private function getHttpClient() { + private function getHttpClient() + { return $this->httpClient; } @@ -103,7 +104,10 @@ public function translate($text, $targetLanguage, &$sourceLanguage = null) // check response json $result = json_decode($response->getBody(), true); - if (!is_array($result) || !array_key_exists('data', $result) || !array_key_exists('translations', $result['data'])) { + if (!is_array($result) || + !array_key_exists('data', $result) || + !array_key_exists('translations', $result['data']) + ) { throw new Exception\TranslationErrorException('Invalid response'); } @@ -111,7 +115,7 @@ public function translate($text, $targetLanguage, &$sourceLanguage = null) $translations = []; $sources = []; foreach ($result['data']['translations'] as $translation) { - $translations[] = html_entity_decode($translation['translatedText'] , ENT_QUOTES, 'UTF-8'); + $translations[] = html_entity_decode($translation['translatedText'], ENT_QUOTES, 'UTF-8'); if (array_key_exists('detectedSourceLanguage', $translation)) { $sources[] = $translation['detectedSourceLanguage']; @@ -159,7 +163,10 @@ public function languages($targetLanguage = null) // check response json $result = json_decode($response->getBody(), true); - if (!is_array($result) || !array_key_exists('data', $result) || !array_key_exists('languages', $result['data'])) { + if (!is_array($result) || + !array_key_exists('data', $result) || + !array_key_exists('languages', $result['data']) + ) { throw new Exception\LanguagesErrorException('Invalid response'); } @@ -203,7 +210,10 @@ public function detect($text) // check response json $result = json_decode($response->getBody(), true); - if (!is_array($result) || !array_key_exists('data', $result) || !array_key_exists('detections', $result['data'])) { + if (!is_array($result) || + !array_key_exists('data', $result) || + !array_key_exists('detections', $result['data']) + ) { throw new Exception\DetectErrorException('Invalid response'); } @@ -267,7 +277,8 @@ private function prepareText($text) * @param string $language language to be validate * @return boolean */ - private function isValidLanguage($language) { + private function isValidLanguage($language) + { $regexpValidLanguage = '%([a-z]{2})(-[a-z]{2})?%'; return preg_match($regexpValidLanguage, $language) === 1; From 464484c3422dcea7889b1b9028b938daaf0ccad7 Mon Sep 17 00:00:00 2001 From: Vinicius Gava Date: Sun, 8 Oct 2017 00:25:13 -0300 Subject: [PATCH 5/9] fix exception translation name --- src/Client.php | 4 ++-- ...lationErrorException.php => TranslateErrorException.php} | 4 ++-- src/LanguagesInterface.php | 2 +- src/TranslateInterface.php | 2 +- tests/ClientTest.php | 6 +++--- 5 files changed, 9 insertions(+), 9 deletions(-) rename src/Exception/{TranslationErrorException.php => TranslateErrorException.php} (70%) diff --git a/src/Client.php b/src/Client.php index bd98ec5..8a9f443 100644 --- a/src/Client.php +++ b/src/Client.php @@ -99,7 +99,7 @@ public function translate($text, $targetLanguage, &$sourceLanguage = null) ['query' => $query] ); } catch (GuzzleException $e) { - throw new Exception\TranslationErrorException('Translation error: ' . $e->getMessage(), 4, $e); + throw new Exception\TranslateErrorException('Translate error: ' . $e->getMessage(), 4, $e); } // check response json @@ -108,7 +108,7 @@ public function translate($text, $targetLanguage, &$sourceLanguage = null) !array_key_exists('data', $result) || !array_key_exists('translations', $result['data']) ) { - throw new Exception\TranslationErrorException('Invalid response'); + throw new Exception\TranslateErrorException('Invalid response'); } // prepare responses diff --git a/src/Exception/TranslationErrorException.php b/src/Exception/TranslateErrorException.php similarity index 70% rename from src/Exception/TranslationErrorException.php rename to src/Exception/TranslateErrorException.php index a353cb2..efcc206 100644 --- a/src/Exception/TranslationErrorException.php +++ b/src/Exception/TranslateErrorException.php @@ -2,11 +2,11 @@ namespace GoogleTranslate\Exception; -class TranslationErrorException extends \DomainException +class TranslateErrorException extends \DomainException { /** @inheritdoc */ public function __construct( - $message = 'Translation Error', + $message = 'Translate Error', $code = 4, \Exception $previous = null ) { diff --git a/src/LanguagesInterface.php b/src/LanguagesInterface.php index 0d41929..315abd3 100644 --- a/src/LanguagesInterface.php +++ b/src/LanguagesInterface.php @@ -20,7 +20,7 @@ interface LanguagesInterface * @return array array structure return above * * @throws Exception\InvalidTargetLanguageException - * @throws Exception\TranslationErrorException + * @throws Exception\TranslateErrorException */ public function languages($targetLanguage = null); } diff --git a/src/TranslateInterface.php b/src/TranslateInterface.php index 2e11ff5..fd4e29f 100644 --- a/src/TranslateInterface.php +++ b/src/TranslateInterface.php @@ -20,7 +20,7 @@ interface TranslateInterface * @throws Exception\InvalidTextException * @throws Exception\InvalidTargetLanguageException * @throws Exception\InvalidSourceLanguageException - * @throws Exception\TranslationErrorException + * @throws Exception\TranslateErrorException */ public function translate($text, $targetLanguage, &$sourceLanguage = null); } diff --git a/tests/ClientTest.php b/tests/ClientTest.php index ca65d6d..4fd269b 100644 --- a/tests/ClientTest.php +++ b/tests/ClientTest.php @@ -142,8 +142,8 @@ public function testTranslateMethodWithSingleStringAndSourceLanguageShouldReturn public function testTranslateMethodWithSingleAndInvalidTargetLanguageShouldReturnTranslationErrorException() { - $this->expectException('\GoogleTranslate\Exception\TranslationErrorException'); - $this->expectExceptionMessage('Translation error: Client error: `POST https://www.googleapis.com/language/translate/v2?key=' . self::ACCESS_KEY . '&q=estou+aqui&target=aa` resulted in a `400 Bad Request` response:'); + $this->expectException('\GoogleTranslate\Exception\TranslateErrorException'); + $this->expectExceptionMessage('Translate error: Client error: `POST https://www.googleapis.com/language/translate/v2?key=' . self::ACCESS_KEY . '&q=estou+aqui&target=aa` resulted in a `400 Bad Request` response:'); $this->expectExceptionCode(4); $requestParams = [ @@ -161,7 +161,7 @@ public function testTranslateMethodWithSingleAndInvalidTargetLanguageShouldRetur } /** - * @expectedException \GoogleTranslate\Exception\TranslationErrorException + * @expectedException \GoogleTranslate\Exception\TranslateErrorException * @expectedExceptionMessage Invalid response * @expectedExceptionCode 4 */ From 751f8acf40c9773febe33cb21d48145199cc9c05 Mon Sep 17 00:00:00 2001 From: Vinicius Gava Date: Sun, 8 Oct 2017 00:40:08 -0300 Subject: [PATCH 6/9] .travis.yml --- .travis.yml | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 .travis.yml diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..9f8c0f9 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,17 @@ +language: php + +php: + - 5.6 + - 7.0 + - 7.1 + +before_install: + - composer self-update + - composer clear-cache + +install: + - composer install --prefer-source --no-interaction --dev + +script: + - vendor/bin/phpcs + - vendor/bin/phpunit --coverage-text From b3b83162e1d13dbd57a1cabe0c6ced1288ce0a40 Mon Sep 17 00:00:00 2001 From: Vinicius Gava Date: Sun, 8 Oct 2017 00:56:57 -0300 Subject: [PATCH 7/9] add credits into the files --- README.md | 4 ++++ src/Client.php | 8 ++++++++ src/DetectInterface.php | 8 ++++++++ src/Exception/DetectErrorException.php | 8 ++++++++ src/Exception/InvalidAccessKeyException.php | 8 ++++++++ src/Exception/InvalidLanguageException.php | 8 ++++++++ src/Exception/InvalidSourceLanguageException.php | 8 ++++++++ src/Exception/InvalidTargetLanguageException.php | 8 ++++++++ src/Exception/InvalidTextException.php | 8 ++++++++ src/Exception/LanguagesErrorException.php | 8 ++++++++ src/Exception/TranslateErrorException.php | 8 ++++++++ src/LanguagesInterface.php | 8 ++++++++ src/TranslateInterface.php | 8 ++++++++ 13 files changed, 100 insertions(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..cbf4338 --- /dev/null +++ b/README.md @@ -0,0 +1,4 @@ +# Google Translate API PHP Client + +This project abstract the google translate api versio 2.0 in PHP. +The lib has been refactored to support unit tests, composer, and a better architecture. diff --git a/src/Client.php b/src/Client.php index 8a9f443..0ad1ac9 100644 --- a/src/Client.php +++ b/src/Client.php @@ -6,6 +6,14 @@ use GuzzleHttp\ClientInterface; use GuzzleHttp\Exception\GuzzleException; +/** + * Google Translate API PHP Client + * + * @link https://github.com/viniciusgava/google-translate-php-client + * @license http://www.gnu.org/copyleft/gpl.html + * @version 2.0 + * @author Vinicius Gava (gava.vinicius@gmail.com) + */ class Client implements TranslateInterface, LanguagesInterface, DetectInterface { /** diff --git a/src/DetectInterface.php b/src/DetectInterface.php index f82d8b0..86d6959 100644 --- a/src/DetectInterface.php +++ b/src/DetectInterface.php @@ -2,6 +2,14 @@ namespace GoogleTranslate; +/** + * Google Translate API PHP Client + * + * @link https://github.com/viniciusgava/google-translate-php-client + * @license http://www.gnu.org/copyleft/gpl.html + * @version 2.0 + * @author Vinicius Gava (gava.vinicius@gmail.com) + */ interface DetectInterface { /** diff --git a/src/Exception/DetectErrorException.php b/src/Exception/DetectErrorException.php index 831e0f1..53e2da0 100644 --- a/src/Exception/DetectErrorException.php +++ b/src/Exception/DetectErrorException.php @@ -2,6 +2,14 @@ namespace GoogleTranslate\Exception; +/** + * Google Translate API PHP Client + * + * @link https://github.com/viniciusgava/google-translate-php-client + * @license http://www.gnu.org/copyleft/gpl.html + * @version 2.0 + * @author Vinicius Gava (gava.vinicius@gmail.com) + */ class DetectErrorException extends \DomainException { /** @inheritdoc */ diff --git a/src/Exception/InvalidAccessKeyException.php b/src/Exception/InvalidAccessKeyException.php index 024e5ad..2696eb0 100644 --- a/src/Exception/InvalidAccessKeyException.php +++ b/src/Exception/InvalidAccessKeyException.php @@ -2,6 +2,14 @@ namespace GoogleTranslate\Exception; +/** + * Google Translate API PHP Client + * + * @link https://github.com/viniciusgava/google-translate-php-client + * @license http://www.gnu.org/copyleft/gpl.html + * @version 2.0 + * @author Vinicius Gava (gava.vinicius@gmail.com) + */ class InvalidAccessKeyException extends \InvalidArgumentException { /** @inheritdoc */ diff --git a/src/Exception/InvalidLanguageException.php b/src/Exception/InvalidLanguageException.php index 9234c9e..4513bc9 100644 --- a/src/Exception/InvalidLanguageException.php +++ b/src/Exception/InvalidLanguageException.php @@ -2,6 +2,14 @@ namespace GoogleTranslate\Exception; +/** + * Google Translate API PHP Client + * + * @link https://github.com/viniciusgava/google-translate-php-client + * @license http://www.gnu.org/copyleft/gpl.html + * @version 2.0 + * @author Vinicius Gava (gava.vinicius@gmail.com) + */ class InvalidLanguageException extends \InvalidArgumentException { /** @inheritdoc */ diff --git a/src/Exception/InvalidSourceLanguageException.php b/src/Exception/InvalidSourceLanguageException.php index 083127c..ff19445 100644 --- a/src/Exception/InvalidSourceLanguageException.php +++ b/src/Exception/InvalidSourceLanguageException.php @@ -2,6 +2,14 @@ namespace GoogleTranslate\Exception; +/** + * Google Translate API PHP Client + * + * @link https://github.com/viniciusgava/google-translate-php-client + * @license http://www.gnu.org/copyleft/gpl.html + * @version 2.0 + * @author Vinicius Gava (gava.vinicius@gmail.com) + */ class InvalidSourceLanguageException extends InvalidLanguageException { /** @inheritdoc */ diff --git a/src/Exception/InvalidTargetLanguageException.php b/src/Exception/InvalidTargetLanguageException.php index e7b1778..bd89196 100644 --- a/src/Exception/InvalidTargetLanguageException.php +++ b/src/Exception/InvalidTargetLanguageException.php @@ -2,6 +2,14 @@ namespace GoogleTranslate\Exception; +/** + * Google Translate API PHP Client + * + * @link https://github.com/viniciusgava/google-translate-php-client + * @license http://www.gnu.org/copyleft/gpl.html + * @version 2.0 + * @author Vinicius Gava (gava.vinicius@gmail.com) + */ class InvalidTargetLanguageException extends InvalidLanguageException { /** @inheritdoc */ diff --git a/src/Exception/InvalidTextException.php b/src/Exception/InvalidTextException.php index 6a531ca..92ba91c 100644 --- a/src/Exception/InvalidTextException.php +++ b/src/Exception/InvalidTextException.php @@ -2,6 +2,14 @@ namespace GoogleTranslate\Exception; +/** + * Google Translate API PHP Client + * + * @link https://github.com/viniciusgava/google-translate-php-client + * @license http://www.gnu.org/copyleft/gpl.html + * @version 2.0 + * @author Vinicius Gava (gava.vinicius@gmail.com) + */ class InvalidTextException extends \InvalidArgumentException { /** @inheritdoc */ diff --git a/src/Exception/LanguagesErrorException.php b/src/Exception/LanguagesErrorException.php index b5e7167..2311184 100644 --- a/src/Exception/LanguagesErrorException.php +++ b/src/Exception/LanguagesErrorException.php @@ -2,6 +2,14 @@ namespace GoogleTranslate\Exception; +/** + * Google Translate API PHP Client + * + * @link https://github.com/viniciusgava/google-translate-php-client + * @license http://www.gnu.org/copyleft/gpl.html + * @version 2.0 + * @author Vinicius Gava (gava.vinicius@gmail.com) + */ class LanguagesErrorException extends \DomainException { /** @inheritdoc */ diff --git a/src/Exception/TranslateErrorException.php b/src/Exception/TranslateErrorException.php index efcc206..372ccec 100644 --- a/src/Exception/TranslateErrorException.php +++ b/src/Exception/TranslateErrorException.php @@ -2,6 +2,14 @@ namespace GoogleTranslate\Exception; +/** + * Google Translate API PHP Client + * + * @link https://github.com/viniciusgava/google-translate-php-client + * @license http://www.gnu.org/copyleft/gpl.html + * @version 2.0 + * @author Vinicius Gava (gava.vinicius@gmail.com) + */ class TranslateErrorException extends \DomainException { /** @inheritdoc */ diff --git a/src/LanguagesInterface.php b/src/LanguagesInterface.php index 315abd3..a308486 100644 --- a/src/LanguagesInterface.php +++ b/src/LanguagesInterface.php @@ -2,6 +2,14 @@ namespace GoogleTranslate; +/** + * Google Translate API PHP Client + * + * @link https://github.com/viniciusgava/google-translate-php-client + * @license http://www.gnu.org/copyleft/gpl.html + * @version 2.0 + * @author Vinicius Gava (gava.vinicius@gmail.com) + */ interface LanguagesInterface { /** diff --git a/src/TranslateInterface.php b/src/TranslateInterface.php index fd4e29f..8580bd0 100644 --- a/src/TranslateInterface.php +++ b/src/TranslateInterface.php @@ -2,6 +2,14 @@ namespace GoogleTranslate; +/** + * Google Translate API PHP Client + * + * @link https://github.com/viniciusgava/google-translate-php-client + * @license http://www.gnu.org/copyleft/gpl.html + * @version 2.0 + * @author Vinicius Gava (gava.vinicius@gmail.com) + */ interface TranslateInterface { /** From c0e446bf7ebb328b27da29816b921b60666ff899 Mon Sep 17 00:00:00 2001 From: Vinicius Gava Date: Sun, 8 Oct 2017 01:05:37 -0300 Subject: [PATCH 8/9] update composer --- composer.json | 6 ++++-- composer.lock | 4 ++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/composer.json b/composer.json index 8a911d7..d1b00c7 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,9 @@ { "name": "viniciusgava/google-translate-api", "description": "Google translate API V2 client for PHP", - "license": "http://www.gnu.org/copyleft/gpl.html", + "license": "GNU", + "homepage": "https://github.com/viniciusgava/google-translate-php-client", + "keywords": ["google", "translate", "client", "adapter"], "authors": [ { "name": "Vinicius Gava", @@ -19,7 +21,7 @@ } }, "require": { - "php": ">5.6", + "php": ">=5.6", "guzzlehttp/guzzle": "^6.3" }, "require-dev": { diff --git a/composer.lock b/composer.lock index 19bf7a9..7d5bdac 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "content-hash": "ae6dfdcce2f8860b0d87246490275b0c", + "content-hash": "bf29b45d9c7e390442aad8afcbbabe31", "packages": [ { "name": "guzzlehttp/guzzle", @@ -1610,7 +1610,7 @@ "prefer-stable": false, "prefer-lowest": false, "platform": { - "php": ">5.6" + "php": ">=5.6" }, "platform-dev": [] } From 72000e6c18cbc49a9c143e31ad8c29bee860f9b2 Mon Sep 17 00:00:00 2001 From: Vinicius Gava Date: Sun, 8 Oct 2017 01:35:19 -0300 Subject: [PATCH 9/9] remove php 5.6 from travis --- .travis.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 9f8c0f9..4dd841b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,6 @@ language: php php: - - 5.6 - 7.0 - 7.1