From a970f18ed7fa99940ca8f10f6d72dc9e0006ed79 Mon Sep 17 00:00:00 2001 From: Laurynas Butkus Date: Sun, 20 Jul 2025 12:13:04 +0300 Subject: [PATCH 01/13] Update to v3 api --- .github/workflows/main.yml | 2 + .gitignore | 2 + lib/DetectLanguage/Client.php | 31 ++++------- lib/DetectLanguage/DetectLanguage.php | 60 ++++++++++++--------- phpunit.xml.dist | 24 ++++----- tests/DetectLanguage/DetectLanguageTest.php | 32 ++++++----- 6 files changed, 75 insertions(+), 76 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 8b16fa4..c5bd6de 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -10,6 +10,8 @@ jobs: - '7.0' - '7.4' - '8.2' + - '8.3' + - '8.4' name: PHP ${{ matrix.php-version }} sample steps: - uses: actions/checkout@v3 diff --git a/.gitignore b/.gitignore index bd4126e..b39dec0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +.env.local .DS_Store .AppleDouble .LSOverride @@ -6,6 +7,7 @@ Icon .Spotlight-V100 .Trashes *.sublime-* +.phpunit.result.cache vendor composer.lock bin diff --git a/lib/DetectLanguage/Client.php b/lib/DetectLanguage/Client.php index d78e1d4..dc8d35f 100644 --- a/lib/DetectLanguage/Client.php +++ b/lib/DetectLanguage/Client.php @@ -33,15 +33,15 @@ class Client * * @return object */ - public static function request($method, $params = null) + public static function request($method, $path, $params = null) { - $url = self::getUrl($method); + $url = self::getUrl($path); - $request_method = self::getRequestMethodName(); - $response_body = self::$request_method($url, $params); + $engine_method = self::getEngineMethodName(); + $response_body = self::$engine_method($method,$url, $params); $response = json_decode($response_body); - if (!is_object($response)) + if (!is_object($response) && !is_array($response)) throw new Error("Invalid server response: $response_body"); if (isset($response->error)) @@ -55,7 +55,7 @@ public static function request($method, $params = null) * * @return string */ - protected static function getRequestMethodName() + protected static function getEngineMethodName() { $request_engine = self::$requestEngine; @@ -84,11 +84,11 @@ protected static function getRequestMethodName() * * @return string Response body */ - protected static function requestStream($url, $params) + protected static function requestStream($method, $url, $params) { $opts = array('http' => array( - 'method' => 'POST', + 'method' => $method, 'header' => implode("\n", self::getHeaders()), 'content' => json_encode($params), 'timeout' => self::$requestTimeout, @@ -109,11 +109,12 @@ protected static function requestStream($url, $params) * * @return string Response body */ - protected static function requestCurl($url, $params) + protected static function requestCurl($method, $url, $params) { $ch = curl_init(); $options = array( + CURLOPT_CUSTOMREQUEST => $method, CURLOPT_URL => $url, CURLOPT_HTTPHEADER => self::getHeaders(), CURLOPT_POSTFIELDS => json_encode($params), @@ -146,17 +147,7 @@ protected static function requestCurl($url, $params) */ protected static function getUrl($method) { - return self::getProtocol() . '://' . DetectLanguage::$host . '/' . DetectLanguage::$apiVersion . '/' . $method; - } - - /** - * Get protocol for request. - * - * @return string 'https' or 'http' - */ - protected static function getProtocol() - { - return DetectLanguage::$secure ? 'https' : 'http'; + return 'https://' . DetectLanguage::$host . '/' . DetectLanguage::$apiVersion . '/' . $method; } /** diff --git a/lib/DetectLanguage/DetectLanguage.php b/lib/DetectLanguage/DetectLanguage.php index cfb4ae5..2a300b4 100644 --- a/lib/DetectLanguage/DetectLanguage.php +++ b/lib/DetectLanguage/DetectLanguage.php @@ -31,20 +31,12 @@ class DetectLanguage * @static * @var string */ - public static $apiVersion = '0.2'; - - /** - * Enable secure mode (SSL). - * - * @static - * @var boolean - */ - public static $secure; + public static $apiVersion = 'v3'; /** * API Client Version. */ - const VERSION = '2.2.1'; + const VERSION = '3.0.0'; /** * Set API key @@ -57,17 +49,6 @@ public static function setApiKey($apiKey) self::$apiKey = $apiKey; } - /** - * Set secure mode - * - * @static - * @param boolean $secure - */ - public static function setSecure($secure) - { - self::$secure = $secure; - } - /** * Detect text language. * @@ -77,9 +58,7 @@ public static function setSecure($secure) */ public static function detect($text) { - $result = Client::request('detect', array('q' => $text)); - - return $result->data->detections; + return Client::request('POST', 'detect', array('q' => $text)); } /** @@ -89,7 +68,7 @@ public static function detect($text) * @param string $text The text for language detection * @return string|null detected language code */ - public static function simpleDetect($text) + public static function detectCode($text) { $detections = self::detect($text); @@ -99,8 +78,37 @@ public static function simpleDetect($text) return null; } + /** + * Detect text language in batch. + * + * @static + * @param array $texts The texts for language detection + * @return array detected languages information + */ + public static function detectBatch($texts) + { + return Client::request('POST', 'detect-batch', array('q' => $texts)); + } + + /** + * Get account status. + * + * @static + * @return array account status information + */ public static function getStatus() { - return Client::request('user/status'); + return Client::request('GET', 'account/status'); + } + + /** + * Get supported languages. + * + * @static + * @return array languages information + */ + public static function getLanguages() + { + return Client::request('GET', 'languages'); } } diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 3c2c692..f79f7cd 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,15 +1,13 @@ - - - - - ./tests/DetectLanguage/ - - - - - - ./lib/DetectLanguage/ - - + + + + ./lib/DetectLanguage/ + + + + + ./tests/DetectLanguage/ + + diff --git a/tests/DetectLanguage/DetectLanguageTest.php b/tests/DetectLanguage/DetectLanguageTest.php index 4174d2a..8761b15 100644 --- a/tests/DetectLanguage/DetectLanguageTest.php +++ b/tests/DetectLanguage/DetectLanguageTest.php @@ -12,7 +12,6 @@ public function set_up() parent::set_up(); DetectLanguage::$apiKey = getenv('DETECTLANGUAGE_API_KEY'); - DetectLanguage::$apiVersion = '0.2'; } public function testConstructor() @@ -37,9 +36,9 @@ public function testDetection() 'To detect Lithuanian language.'); } - public function testSimpleDetection() + public function testDetectCode() { - $result = DetectLanguage::simpleDetect('Hello world'); + $result = DetectLanguage::detectCode('Hello world'); $this->assertEquals('en', $result, 'To detect English language.'); @@ -57,20 +56,13 @@ public function testStreamRequest() $this->__request(); } - public function testSecureRequest() - { - DetectLanguage::setSecure(true); - $this->__request(); - DetectLanguage::setSecure(false); - } - public function testInvalidApiKey() { $this->expectException('\DetectLanguage\Error'); DetectLanguage::setApiKey('invalid'); - $result = DetectLanguage::simpleDetect('Hello world'); + $result = DetectLanguage::detectCode('Hello world'); } public function testErrorBackwardsCompatibility() @@ -79,15 +71,14 @@ public function testErrorBackwardsCompatibility() DetectLanguage::setApiKey('invalid'); - $result = DetectLanguage::simpleDetect('Hello world'); + $result = DetectLanguage::detectCode('Hello world'); } public function testInvalidResponse() { $this->expectException('\DetectLanguage\Error'); - DetectLanguage::$apiVersion = '0.0'; - DetectLanguage::simpleDetect('Hello world'); + DetectLanguage::detect(''); } public function testBatchDetectionWithCurl() @@ -120,6 +111,13 @@ public function testGetStatus() $this->assertEquals($response->status, 'ACTIVE'); } + public function testGetLanguages() + { + $response = DetectLanguage::getLanguages(); + $this->assertIsArray($response); + $this->assertIsString($response[0]->code); + } + private function setRequestEngine($engine) { \DetectLanguage\Client::$requestEngine = $engine; @@ -127,14 +125,14 @@ private function setRequestEngine($engine) private function __request() { - $result = DetectLanguage::simpleDetect('Hello world'); + $result = DetectLanguage::detectCode('Hello world'); $this->assertEquals('en', $result, 'To detect English language.'); } private function __batchDetection() { - $result = DetectLanguage::detect(array('Hello world', 'Jau saulelė vėl atkopdama budino svietą')); + $result = DetectLanguage::detectBatch(array('Hello world', 'Jau saulelė vėl atkopdama budino svietą')); $this->assertEquals('en', $result[0][0]->language, 'To detect English language.'); @@ -170,7 +168,7 @@ private function __batchDetectionOrder() 'hello', ); - $result = DetectLanguage::detect($request); + $result = DetectLanguage::detectBatch($request); foreach ($request as $i => $phrase) { $language = $phrase == 'hello' ? 'en' : 'ru'; From 41315f4630742a063bb39b3f24662fb21f22b49c Mon Sep 17 00:00:00 2001 From: Laurynas Butkus Date: Sun, 20 Jul 2025 12:22:24 +0300 Subject: [PATCH 02/13] Do not set body for GET requests --- lib/DetectLanguage/Client.php | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/lib/DetectLanguage/Client.php b/lib/DetectLanguage/Client.php index dc8d35f..eeb11f5 100644 --- a/lib/DetectLanguage/Client.php +++ b/lib/DetectLanguage/Client.php @@ -29,16 +29,21 @@ class Client * Perform a request * * @param string $method Method name - * @param array $params The parameters to use for the POST body + * @param array $payload Request payload * * @return object */ - public static function request($method, $path, $params = null) + public static function request($method, $path, $payload = null) { $url = self::getUrl($path); + if ($payload !== null) + $body = json_encode($payload); + else + $body = null; + $engine_method = self::getEngineMethodName(); - $response_body = self::$engine_method($method,$url, $params); + $response_body = self::$engine_method($method,$url, $body); $response = json_decode($response_body); if (!is_object($response) && !is_array($response)) @@ -80,22 +85,25 @@ protected static function getEngineMethodName() * Perform request using native PHP streams * * @param string $url Request URL - * @param array $params The parameters to use for the POST body + * @param string $body Request body * * @return string Response body */ - protected static function requestStream($method, $url, $params) + protected static function requestStream($method, $url, $body) { $opts = array('http' => array( 'method' => $method, 'header' => implode("\n", self::getHeaders()), - 'content' => json_encode($params), 'timeout' => self::$requestTimeout, 'ignore_errors' => true, ) ); + if ($body !== null) { + $opts['http']['content'] = $body; + } + $context = stream_context_create($opts); return file_get_contents($url, false, $context); @@ -105,11 +113,11 @@ protected static function requestStream($method, $url, $params) * Perform request using CURL extension. * * @param string $url Request URL - * @param array $params The parameters to use for the POST body + * @param string $body Request body * * @return string Response body */ - protected static function requestCurl($method, $url, $params) + protected static function requestCurl($method, $url, $body) { $ch = curl_init(); @@ -117,13 +125,16 @@ protected static function requestCurl($method, $url, $params) CURLOPT_CUSTOMREQUEST => $method, CURLOPT_URL => $url, CURLOPT_HTTPHEADER => self::getHeaders(), - CURLOPT_POSTFIELDS => json_encode($params), CURLOPT_CONNECTTIMEOUT => self::$connectTimeout, CURLOPT_TIMEOUT => self::$requestTimeout, CURLOPT_USERAGENT => self::getUserAgent(), CURLOPT_RETURNTRANSFER => true ); + if ($body !== null) { + $options[CURLOPT_POSTFIELDS] = $body; + } + curl_setopt_array($ch, $options); $result = curl_exec($ch); From e29161a00da1f76b84a8ea1a3a43c8001a7addfe Mon Sep 17 00:00:00 2001 From: Laurynas Butkus Date: Sun, 20 Jul 2025 16:05:22 +0300 Subject: [PATCH 03/13] Update readme --- README.md | 51 +++++++++++++++++++++++++++++++++++---------------- 1 file changed, 35 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 1abecb3..b3657e5 100644 --- a/README.md +++ b/README.md @@ -33,21 +33,18 @@ Create or add the following to composer.json in your project root: ### Configuration Before using Detect Language API client you have to setup your personal API key. -You can get it by signing up at http://detectlanguage.com +You can get it by signing up at https://detectlanguage.com ```php use \DetectLanguage\DetectLanguage; DetectLanguage::setApiKey("YOUR API KEY"); - -// Enable secure mode if passing sensitive information -// DetectLanguage::setSecure(true); ``` ### Language detection ```php -$results = DetectLanguage::detect("Buenos dias señor"); +$results = DetectLanguage::detect("Dolce far niente"); ``` #### Results @@ -57,9 +54,8 @@ Array ( [0] => stdClass Object ( - [language] => es - [isReliable] => 1 - [confidence] => 10.24 + [language] => it + [score] => 0.5074 ) ) @@ -70,13 +66,13 @@ Array If you need just a language code you can use `simpleDetect`. It returns just the language code. ```php -$languageCode = DetectLanguage::simpleDetect("Buenos dias señor"); +$languageCode = DetectLanguage::detectCode("Dolce far niente"); ``` #### Result ```php -"es" +"it" ``` ### Batch detection @@ -86,7 +82,7 @@ This method is faster than doing one request per text. To use batch detection just pass array of texts to `detect` method. ```php -$results = DetectLanguage::detect(array("Buenos dias señor", "Hello world")); +$results = DetectLanguage::detectBatch(array("Dolce far niente", "Hello world")); ``` #### Results @@ -100,9 +96,8 @@ Array ( [0] => stdClass Object ( - [language] => es - [isReliable] => 1 - [confidence] => 10.24 + [language] => it + [score] => 0.5074 ) ) @@ -112,8 +107,7 @@ Array [0] => stdClass Object ( [language] => en - [isReliable] => 1 - [confidence] => 11.94 + [score] => 0.9098 ) ) @@ -143,6 +137,31 @@ stdClass Object ) ``` +### Get supported languages + +```php +$results = DetectLanguage::getLanguages(); +``` + +#### Result + +```php +Array +( + [0] => stdClass Object + ( + [code] => aa + [name] => Afar + ) + + [1] => stdClass Object + ( + [code] => ab + [name] => Abkhazian + ) + ... +``` + ## License Detect Language API Client is free software, and may be redistributed under the terms specified in the MIT-LICENSE file. From a9a52e04626145444000510e8c3066bdc222a352 Mon Sep 17 00:00:00 2001 From: Laurynas Butkus Date: Sun, 20 Jul 2025 16:05:36 +0300 Subject: [PATCH 04/13] Add mise --- mise.toml | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 mise.toml diff --git a/mise.toml b/mise.toml new file mode 100644 index 0000000..4c09fe6 --- /dev/null +++ b/mise.toml @@ -0,0 +1,4 @@ +[tools] + +[env] +_.file = ".env.local" From 00c432ee491fc0b8031b3fa48974efc61b782848 Mon Sep 17 00:00:00 2001 From: Laurynas Butkus Date: Sun, 20 Jul 2025 16:11:38 +0300 Subject: [PATCH 05/13] Add changelog --- CHANGELOG.md | 19 +++++++++++++++++++ README.md | 4 ++++ lib/DetectLanguage/DetectLanguage.php | 4 ++++ tests/DetectLanguage/DetectLanguageTest.php | 8 +++++++- 4 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 CHANGELOG.md diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..67c9dad --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,19 @@ +# Changelog + +All notable changes to this project will be documented in this file. + +The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), +and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). + + +## v3.0.0 + +### Added +- `detectBatch` method for batch detections + +### Changed +- Switched to v3 API which uses updated language detection model +- ⚠️ `simpleDetect` method renamed to `detectCode` +- ⚠️ `detect` method result fields are `language` and `score` +- ⚠️ `detect` method no longer accept arrays - use `detectBatch` instead +- HTTPS is used by default. Removed secure mode configuration. diff --git a/README.md b/README.md index b3657e5..823adad 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,10 @@ Create or add the following to composer.json in your project root: } ``` +## Upgrading + +When upgrading please check [changelog](CHANGELOG.md) for breaking changes. + ## Usage ### Configuration diff --git a/lib/DetectLanguage/DetectLanguage.php b/lib/DetectLanguage/DetectLanguage.php index 2a300b4..34c419a 100644 --- a/lib/DetectLanguage/DetectLanguage.php +++ b/lib/DetectLanguage/DetectLanguage.php @@ -58,6 +58,10 @@ public static function setApiKey($apiKey) */ public static function detect($text) { + if (is_array($text)) { + throw new Error('detect method does not accept arrays, use detectBatch instead'); + } + return Client::request('POST', 'detect', array('q' => $text)); } diff --git a/tests/DetectLanguage/DetectLanguageTest.php b/tests/DetectLanguage/DetectLanguageTest.php index 8761b15..38da860 100644 --- a/tests/DetectLanguage/DetectLanguageTest.php +++ b/tests/DetectLanguage/DetectLanguageTest.php @@ -23,7 +23,7 @@ public function testConstructor() 'Expect the API key to be set.'); } - public function testDetection() + public function testDetect() { $result = DetectLanguage::detect('Hello world'); @@ -36,6 +36,12 @@ public function testDetection() 'To detect Lithuanian language.'); } + public function testDetectWithArray() + { + $this->expectException('\DetectLanguage\Error'); + $result = DetectLanguage::detect(array('Hello world')); + } + public function testDetectCode() { $result = DetectLanguage::detectCode('Hello world'); From 44adca22eb27f25ec156b91cd0e7368c48b7e876 Mon Sep 17 00:00:00 2001 From: Laurynas Butkus Date: Sun, 20 Jul 2025 19:20:51 +0300 Subject: [PATCH 06/13] Add publish action --- .github/workflows/publish.yml | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 .github/workflows/publish.yml diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml new file mode 100644 index 0000000..a325db2 --- /dev/null +++ b/.github/workflows/publish.yml @@ -0,0 +1,13 @@ +name: Release to Packagist +on: + release: + types: [published] +jobs: + release: + runs-on: ubuntu-latest + steps: + - name: Dispatch packagist update + run: | + curl -X POST https://packagist.org/api/update-package?username=${{ secrets.PACKAGIST_USERNAME }}&apiToken=${{ secrets.PACKAGIST_API_TOKEN }} \ + -d '{"repository":{"url":"https://github.com/detectlanguage/detectlanguage-php"}}' \ + -H "Content-Type: application/json" From 6efec7ac7aea579d4e52a1123dc93b3bed621ff8 Mon Sep 17 00:00:00 2001 From: Laurynas Butkus Date: Sun, 20 Jul 2025 21:18:30 +0300 Subject: [PATCH 07/13] Adjust readme --- README.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 823adad..dc0eb60 100644 --- a/README.md +++ b/README.md @@ -45,7 +45,7 @@ use \DetectLanguage\DetectLanguage; DetectLanguage::setApiKey("YOUR API KEY"); ``` -### Language detection +### Detect language ```php $results = DetectLanguage::detect("Dolce far niente"); @@ -65,9 +65,9 @@ Array ) ``` -### Simple language detection +### Detect single code -If you need just a language code you can use `simpleDetect`. It returns just the language code. +If you need just a language code you can use `detectCode`. It returns just the language code. ```php $languageCode = DetectLanguage::detectCode("Dolce far niente"); @@ -83,7 +83,7 @@ $languageCode = DetectLanguage::detectCode("Dolce far niente"); It is possible to detect language of several texts with one request. This method is faster than doing one request per text. -To use batch detection just pass array of texts to `detect` method. +To use batch detection just pass array of texts to `detectBatch` method. ```php $results = DetectLanguage::detectBatch(array("Dolce far niente", "Hello world")); @@ -141,7 +141,7 @@ stdClass Object ) ``` -### Get supported languages +### Get list of supported languages ```php $results = DetectLanguage::getLanguages(); From 3efe417d592d6bf30d76cdaaeeea548de7fb96a2 Mon Sep 17 00:00:00 2001 From: Laurynas Butkus Date: Wed, 23 Jul 2025 21:33:46 +0300 Subject: [PATCH 08/13] Add deprecations --- CHANGELOG.md | 8 +++++--- lib/DetectLanguage/DetectLanguage.php | 16 +++++++++++++++- tests/DetectLanguage/DetectLanguageTest.php | 11 ++++++++++- 3 files changed, 30 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 67c9dad..4311f7c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,7 +13,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed - Switched to v3 API which uses updated language detection model -- ⚠️ `simpleDetect` method renamed to `detectCode` - ⚠️ `detect` method result fields are `language` and `score` -- ⚠️ `detect` method no longer accept arrays - use `detectBatch` instead -- HTTPS is used by default. Removed secure mode configuration. +- ⚠️ `simpleDetect` deprecated, use `detectCode` instead +- ⚠️ `detect` for batch detection is deprecated, use `detectBatch` instead + +### Removed +- Secure mode configuration. HTTPS is always used. diff --git a/lib/DetectLanguage/DetectLanguage.php b/lib/DetectLanguage/DetectLanguage.php index 34c419a..a296e18 100644 --- a/lib/DetectLanguage/DetectLanguage.php +++ b/lib/DetectLanguage/DetectLanguage.php @@ -59,7 +59,8 @@ public static function setApiKey($apiKey) public static function detect($text) { if (is_array($text)) { - throw new Error('detect method does not accept arrays, use detectBatch instead'); + trigger_error('detect method does not accept arrays, use detectBatch instead', E_USER_DEPRECATED); + return self::detectBatch($text); } return Client::request('POST', 'detect', array('q' => $text)); @@ -115,4 +116,17 @@ public static function getLanguages() { return Client::request('GET', 'languages'); } + + // DEPRECATED METHODS + + /** + * @deprecated use self::detectCode instead + * @param string $text The text for language detection + * @return string|null detected language code + */ + public static function simpleDetect($text) + { + trigger_error('simpleDetect method is deprecated, use detectCode instead', E_USER_DEPRECATED); + return self::detectCode($text); + } } diff --git a/tests/DetectLanguage/DetectLanguageTest.php b/tests/DetectLanguage/DetectLanguageTest.php index 38da860..c2a877a 100644 --- a/tests/DetectLanguage/DetectLanguageTest.php +++ b/tests/DetectLanguage/DetectLanguageTest.php @@ -38,8 +38,10 @@ public function testDetect() public function testDetectWithArray() { - $this->expectException('\DetectLanguage\Error'); $result = DetectLanguage::detect(array('Hello world')); + + $this->assertEquals('en', $result[0][0]->language, + 'To detect English language.'); } public function testDetectCode() @@ -50,6 +52,13 @@ public function testDetectCode() 'To detect English language.'); } + public function testSimpleDetect() + { + $result = DetectLanguage::simpleDetect('Hello world'); + $this->assertEquals('en', $result, + 'To detect English language.'); + } + public function testCurlRequest() { $this->setRequestEngine('curl'); From 17a89a2d25e45e4bbc1b5beeac938dca2ca35448 Mon Sep 17 00:00:00 2001 From: Laurynas Butkus Date: Wed, 23 Jul 2025 21:45:40 +0300 Subject: [PATCH 09/13] Add test tasks --- .github/workflows/main.yml | 2 +- composer.json | 5 +++++ mise.toml | 5 +++++ phpunit.xml.dist | 2 +- 4 files changed, 12 insertions(+), 2 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index c5bd6de..af7d253 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -22,4 +22,4 @@ jobs: - run: composer install - env: DETECTLANGUAGE_API_KEY: ${{ secrets.DETECTLANGUAGE_API_KEY }} - run: bin/phpunit + run: composer test diff --git a/composer.json b/composer.json index d8168da..461baa8 100644 --- a/composer.json +++ b/composer.json @@ -15,6 +15,7 @@ "php": ">=5.3.0" }, "require-dev": { + "phpunit/phpunit": "^9.6", "yoast/phpunit-polyfills": "^1.0" }, "config": { @@ -24,5 +25,9 @@ "psr-0": { "DetectLanguage": "lib/" } + }, + "scripts": { + "test": "php -d memory_limit=512M vendor/phpunit/phpunit/phpunit", + "test-silent": "php -d memory_limit=512M -d error_reporting='E_ALL & ~E_DEPRECATED & ~E_USER_DEPRECATED' vendor/phpunit/phpunit/phpunit" } } diff --git a/mise.toml b/mise.toml index 4c09fe6..3560a5f 100644 --- a/mise.toml +++ b/mise.toml @@ -2,3 +2,8 @@ [env] _.file = ".env.local" + +[tasks] +test = "composer test" +test-silent = "composer test-silent" + diff --git a/phpunit.xml.dist b/phpunit.xml.dist index f79f7cd..d83512b 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,5 +1,5 @@ - + ./lib/DetectLanguage/ From af51f854935f67581b15a853876ee14ad6a26680 Mon Sep 17 00:00:00 2001 From: Laurynas Butkus Date: Wed, 23 Jul 2025 21:55:58 +0300 Subject: [PATCH 10/13] Do not specify phpunit version --- composer.json | 1 - 1 file changed, 1 deletion(-) diff --git a/composer.json b/composer.json index 461baa8..6b1c902 100644 --- a/composer.json +++ b/composer.json @@ -15,7 +15,6 @@ "php": ">=5.3.0" }, "require-dev": { - "phpunit/phpunit": "^9.6", "yoast/phpunit-polyfills": "^1.0" }, "config": { From 1bf7d006689c31681e314bab95b2e74bf62c9336 Mon Sep 17 00:00:00 2001 From: Laurynas Butkus Date: Wed, 23 Jul 2025 21:57:39 +0300 Subject: [PATCH 11/13] Use test-silent in github actions --- .github/workflows/main.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index af7d253..a8833dc 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -22,4 +22,4 @@ jobs: - run: composer install - env: DETECTLANGUAGE_API_KEY: ${{ secrets.DETECTLANGUAGE_API_KEY }} - run: composer test + run: composer test-silent From 9e41bd06a662171142278becc6d969f3258449a5 Mon Sep 17 00:00:00 2001 From: Laurynas Butkus Date: Sat, 26 Jul 2025 10:06:52 +0300 Subject: [PATCH 12/13] Add deprecations --- CHANGELOG.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4311f7c..45a389c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,8 +14,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed - Switched to v3 API which uses updated language detection model - ⚠️ `detect` method result fields are `language` and `score` -- ⚠️ `simpleDetect` deprecated, use `detectCode` instead -- ⚠️ `detect` for batch detection is deprecated, use `detectBatch` instead + +### Deprecated +- `simpleDetect()` - Use `detectCode()` instead. +- Calling `detect()` with array argument. Use `detectBatch()` instead. ### Removed - Secure mode configuration. HTTPS is always used. From b99626f47d96d1d2130cd5b4199fd48ddd437fc3 Mon Sep 17 00:00:00 2001 From: Laurynas Butkus Date: Wed, 30 Jul 2025 21:45:53 +0300 Subject: [PATCH 13/13] Add phpdoc throws --- lib/DetectLanguage/Client.php | 26 +++++++++++++------------- lib/DetectLanguage/DetectLanguage.php | 19 +++++++++++++------ lib/DetectLanguage/Error.php | 4 +++- 3 files changed, 29 insertions(+), 20 deletions(-) diff --git a/lib/DetectLanguage/Client.php b/lib/DetectLanguage/Client.php index eeb11f5..ed8a912 100644 --- a/lib/DetectLanguage/Client.php +++ b/lib/DetectLanguage/Client.php @@ -28,10 +28,12 @@ class Client /** * Perform a request * - * @param string $method Method name - * @param array $payload Request payload + * @param string $method HTTP method name (GET, POST, etc.) + * @param string $path API endpoint path + * @param array|null $payload Request payload data * - * @return object + * @return array Response data + * @throws \DetectLanguage\Error When API request fails, invalid response received, or authentication fails */ public static function request($method, $path, $payload = null) { @@ -55,11 +57,6 @@ public static function request($method, $path, $payload = null) return $response; } - /** - * Get request method name. - * - * @return string - */ protected static function getEngineMethodName() { $request_engine = self::$requestEngine; @@ -84,8 +81,9 @@ protected static function getEngineMethodName() /** * Perform request using native PHP streams * + * @param string $method HTTP method name * @param string $url Request URL - * @param string $body Request body + * @param string|null $body Request body * * @return string Response body */ @@ -112,10 +110,12 @@ protected static function requestStream($method, $url, $body) /** * Perform request using CURL extension. * + * @param string $method HTTP method name * @param string $url Request URL - * @param string $body Request body + * @param string|null $body Request body * * @return string Response body + * @throws \DetectLanguage\Error When CURL request fails, times out, or connection fails */ protected static function requestCurl($method, $url, $body) { @@ -154,7 +154,7 @@ protected static function requestCurl($method, $url, $body) * Build URL for given method * * @param string $method Method name - * @return string + * @return string Complete API URL */ protected static function getUrl($method) { @@ -164,7 +164,7 @@ protected static function getUrl($method) /** * Build request headers. * - * @return array + * @return array Array of HTTP headers */ protected static function getHeaders() { @@ -179,7 +179,7 @@ protected static function getHeaders() /** * Get User-Agent for the request. * - * @return string + * @return string User-Agent string */ protected static function getUserAgent() { diff --git a/lib/DetectLanguage/DetectLanguage.php b/lib/DetectLanguage/DetectLanguage.php index a296e18..55c9189 100644 --- a/lib/DetectLanguage/DetectLanguage.php +++ b/lib/DetectLanguage/DetectLanguage.php @@ -42,7 +42,8 @@ class DetectLanguage * Set API key * * @static - * @param string $apiKey + * @param string $apiKey The API key for authentication + * @return void */ public static function setApiKey($apiKey) { @@ -54,7 +55,8 @@ public static function setApiKey($apiKey) * * @static * @param string $text The text for language detection - * @return array detected languages information + * @return array Detected languages information + * @throws \DetectLanguage\Error When API request fails, invalid response received, or authentication fails */ public static function detect($text) { @@ -71,7 +73,8 @@ public static function detect($text) * * @static * @param string $text The text for language detection - * @return string|null detected language code + * @return string|null Detected language code + * @throws \DetectLanguage\Error When API request fails, invalid response received, or authentication fails */ public static function detectCode($text) { @@ -88,7 +91,8 @@ public static function detectCode($text) * * @static * @param array $texts The texts for language detection - * @return array detected languages information + * @return array Detected languages information + * @throws \DetectLanguage\Error When API request fails, invalid response received, or authentication fails */ public static function detectBatch($texts) { @@ -100,6 +104,7 @@ public static function detectBatch($texts) * * @static * @return array account status information + * @throws \DetectLanguage\Error When API request fails, invalid response received, or authentication fails */ public static function getStatus() { @@ -110,7 +115,8 @@ public static function getStatus() * Get supported languages. * * @static - * @return array languages information + * @return array Supported languages information + * @throws \DetectLanguage\Error When API request fails, invalid response received, or authentication fails */ public static function getLanguages() { @@ -122,7 +128,8 @@ public static function getLanguages() /** * @deprecated use self::detectCode instead * @param string $text The text for language detection - * @return string|null detected language code + * @return string|null Detected language code + * @throws \DetectLanguage\Error When API request fails, invalid response received, or authentication fails */ public static function simpleDetect($text) { diff --git a/lib/DetectLanguage/Error.php b/lib/DetectLanguage/Error.php index 731f46c..c905e37 100644 --- a/lib/DetectLanguage/Error.php +++ b/lib/DetectLanguage/Error.php @@ -2,7 +2,9 @@ namespace DetectLanguage; -// backwards compatibility +/** + * @deprecated Use Error class instead + */ class DetectLanguageError extends \Exception { }