From edc6ee6ddd489ca6b2086545d2a5c355810820fa Mon Sep 17 00:00:00 2001 From: Anil Chaudhari Date: Sat, 6 Apr 2024 16:10:16 +0545 Subject: [PATCH 1/3] enh: remove psalm --- composer.json | 7 +------ phpunit.xml.dist | 36 +++++++++++++----------------------- psalm.xml | 19 ------------------- 3 files changed, 14 insertions(+), 48 deletions(-) delete mode 100644 psalm.xml diff --git a/composer.json b/composer.json index 6af9565..09a1fa0 100644 --- a/composer.json +++ b/composer.json @@ -28,11 +28,8 @@ "yiisoft/yii2-httpclient": "^2.0" }, "require-dev": { - "maglnet/composer-require-checker": "^3.8", "phpunit/phpunit": "^8.5", - "rector/rector": "^1.0", - "spatie/phpunit-watcher": "^1.23", - "vimeo/psalm": "^4.30" + "rector/rector": "^1.0" }, "autoload": { "psr-4": { @@ -47,8 +44,6 @@ "config": { "sort-packages": true, "allow-plugins": { - "infection/extension-installer": true, - "composer/package-versions-deprecated": true, "yiisoft/yii2-composer": true } }, diff --git a/phpunit.xml.dist b/phpunit.xml.dist index f49a158..c66ebf1 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,28 +1,18 @@ - - - - + + + + ./tests + + - - - ./tests - - - - + ./src diff --git a/psalm.xml b/psalm.xml deleted file mode 100644 index 639e1a9..0000000 --- a/psalm.xml +++ /dev/null @@ -1,19 +0,0 @@ - - - - - - - - - - - - From f70af905ec0e1748f1cd6e6bcada9a9ef5e40c74 Mon Sep 17 00:00:00 2001 From: Anil Chaudhari Date: Sat, 6 Apr 2024 16:10:39 +0545 Subject: [PATCH 2/3] docs: improved docs --- README.md | 105 ++++++++++++++++++++++--------------------------- docs/README.md | 56 ++++++++++++++++++++++---- 2 files changed, 95 insertions(+), 66 deletions(-) diff --git a/README.md b/README.md index c3028c5..044cb6d 100644 --- a/README.md +++ b/README.md @@ -10,21 +10,10 @@ [![Total Downloads](https://poser.pugx.org/imanilchaudhari/yii2-currency-converter/downloads)](https://packagist.org/packages/imanilchaudhari/yii2-currency-converter) [![Build Status](https://travis-ci.org/imanilchaudhari/yii2-currency-converter.svg?branch=master)](https://travis-ci.org/imanilchaudhari/yii2-currency-converter) [![Code Coverage](https://codecov.io/gh/imanilchaudhari/yii2-currency-converter/branch/master/graph/badge.svg)](https://codecov.io/gh/imanilchaudhari/yii2-currency-converter) -[![Mutation testing badge](https://img.shields.io/endpoint?style=flat&url=https%3A%2F%2Fbadge-api.stryker-mutator.io%2Fgithub.com%2Fyiisoft%2F_____%2Fmaster)](https://dashboard.stryker-mutator.io/reports/github.com/imanilchaudhari/yii2-currency-converter/master) -[![type-coverage](https://shepherd.dev/github/imanilchaudhari/yii2-currency-converter/coverage.svg)](https://shepherd.dev/github/imanilchaudhari/yii2-currency-converter) -[![psalm-level](https://shepherd.dev/github/imanilchaudhari/yii2-currency-converter/level.svg)](https://shepherd.dev/github/imanilchaudhari/yii2-currency-converter) -This extension will help to find out current currency conversion rate. This extension uses Yahoo's currency conversion API. +This extension will help to find out current currency conversion rate using various providers. -Why Use It ------------ -* Reliable Rate. Uses Yahoo API, Open Exchange Rates API. -* Conversion without curreny code (from country code). -* Caching of rate, to avoid connecting to Yahoo again and again. - -Important Notice ----------------- -As of recent changes on Yahoo Terms of Service. As such, the service is being discontinued. I highly recommend you to use [Open Exchange Rates API](http://openexchangerates.org/). As suggested by [chaimleich](https://github.com/chaimleich) on this [pull request](https://github.com/imanilchaudhari/yii2-currency-converter/pull/3). You can find Open Exchnage Rates working example below. +Documentation is at [docs/README.md](docs/README.md). Requirements ----------- @@ -32,7 +21,6 @@ Requirements * Curl Extension (Optional) - Installation ------------ @@ -41,13 +29,13 @@ The preferred way to install this extension is through [composer](http://getcomp Either run ``` -php composer.phar require --prefer-dist imanilchaudhari/yii2-currency-converter "2.0" +php composer.phar require --prefer-dist imanilchaudhari/yii2-currency-converter "3.0" ``` or add ``` -"imanilchaudhari/yii2-currency-converter": "2.0" +"imanilchaudhari/yii2-currency-converter": "3.0" ``` to the require section of your `composer.json` file. @@ -57,63 +45,64 @@ Usage ----- Once the extension is installed, simply use it in your code by : +```php +'components' => [ + 'currencyConverter' => [ + 'class' => 'imanilchaudhari\CurrencyConverter\CurrencyConverter', + 'provider' => [ + 'class' => 'imanilchaudhari\CurrencyConverter\Provider\ExchangeRatesApi', + ], + ], + ... +], + +$converter = Yii::$app->currencyConverter; +$rate = $converter->convert('USD', 'NPR'); + +``` + +***OR*** ```php -use Yii; use imanilchaudhari\CurrencyConverter\CurrencyConverter; +use imanilchaudhari\CurrencyConverter\Provider\OpenExchangeRatesApi; -$converter = new CurrencyConverter(); +$converter = new CurrencyConverter([ + 'provider' => [ + 'class' => OpenExchangeRatesApi::class, + 'appId' => Yii::$app->params['openExchangeRate']['appId'], + ], +]); $rate = $converter->convert('USD', 'NPR'); print_r($rate); // it will print current Nepalese currency (NPR) rate according to USD - ``` -Available Currency Codes +Exchange Rate Providers ----------------------- -```php -$currencies = [ - 'AF' => 'AFA', - 'AL' => 'ALL', - 'DZ' => 'DZD', - 'AS' => 'USD', - 'AD' => 'EUR', - ... - ]; +- [ApiForexApi](./src/Provider/ApiForexApi.php) - Get exchange rates from https://api.forex/ +- [CurrencyApi](./src/Provider/CurrencyApi.php) - Get exchange rates from https://currencyapi.com/ +- [CurrencyFreaksApi](./src/Provider/CurrencyFreaksApi.php) - Get exchange rates from https://currencyfreaks.com/ +- [CurrencylayerApi](./src/Provider/CurrencylayerApi.php) - Get exchange rates from https://currencylayer.com/ +- [ExchangeRatesApi](./src/Provider/ExchangeRatesApi.php) - Get exchange rates from https://www.exchangerate-api.com/ +- [FixerApi](./src/Provider/FixerApi.php) - Get exchange rates from https://fixer.io/ +- [OpenExchangeRatesApi](./src/Provider/OpenExchangeRatesApi.php) - Get exchange rates from https://openexchangerates.org/ -``` -Note: to view all the applicable currencies [click here](https://github.com/imanilchaudhari/yii2-currency-converter/blob/1.1/CountryToCurrency.php). +## Testing -Contributors -------- -* [Robot72](https://github.com/Robot72 "Robot72") -* [chaimleich](https://github.com/chaimleich "chaimleich") +### Unit testing -Open Exchange Rates APi Integration ------------------------------------ -Here is a code snippets suggested by [chaimleich](https://github.com/chaimleich) on [this pull request](https://github.com/imanilchaudhari/yii2-currency-converter/pull/3). -```php +The package is tested with [PHPUnit](https://phpunit.de/). To run tests: -use Yii; -use imanilchaudhari\CurrencyConverter\Provider\OpenExchangeRatesApi; - -class CurrencyConverter extends \imanilchaudhari\CurrencyConverter\CurrencyConverter -{ - /** - * @inheritdoc - */ - public function getRateProvider() - { - if (!$this->rateProvider) { - $this->setRateProvider(new OpenExchangeRatesApi([ - 'appId' => Yii::$app->params['openExchangeRate']['appId'], - ])); - } - - return $this->rateProvider; - } -} +```shell +./vendor/bin/phpunit ``` +## License + +The Yii2 Currency Converter is free software. It is released under the terms of the MIT License. Please see [`LICENSE`](./LICENSE.md) for more information. + + +[![Powered by](https://img.shields.io/badge/Powered_by-Yii_Framework-green.svg?style=flat)](https://www.yiiframework.com/) diff --git a/docs/README.md b/docs/README.md index 18eafd9..1d89801 100644 --- a/docs/README.md +++ b/docs/README.md @@ -2,11 +2,51 @@ This package provides the most famous currency rates providers' implementations. The package was originally built on Yahoo API, but there was a discontinuation of it. It is therefore highly recommended that you make use of only active providers, such as the ones listed below. -### Active Provider -- [ApiForex API](ApiForexApi.md) -- [Currency API](CurrencyApi.md) -- [CurrencyFreaks API](CurrencyFreaksApi.md) -- [Currency Layer API](CurrencylayerApi.md) -- [Exchange Rates API](ExchangeRatesApi.md) -- [Fixer API](FixerApi.md) -- [Open Exchange Rates API](OpenExchangeRatesApi.md) +Usage +----- + +Once the extension is installed, simply use it in your code by : +```php +'components' => [ + 'currencyConverter' => [ + 'class' => 'imanilchaudhari\CurrencyConverter\CurrencyConverter', + 'provider' => [ + 'class' => 'imanilchaudhari\CurrencyConverter\Provider\ExchangeRatesApi', + ], + ], + ... +], + +$converter = Yii::$app->currencyConverter; +$rate = $converter->convert('USD', 'NPR'); + +``` + +***OR*** + +```php +use imanilchaudhari\CurrencyConverter\CurrencyConverter; +use imanilchaudhari\CurrencyConverter\Provider\OpenExchangeRatesApi; + +$converter = new CurrencyConverter([ + 'provider' => [ + 'class' => OpenExchangeRatesApi::class, + 'appId' => Yii::$app->params['openExchangeRate']['appId'], + ], +]); +$rate = $converter->convert('USD', 'NPR'); + +print_r($rate); // it will print current Nepalese currency (NPR) rate according to USD + +``` + +Active Providers +----------------------- +- [ApiForex API](ApiForexApi.md) - Get exchange rates from https://api.forex/ +- [Currency API](CurrencyApi.md) - Get exchange rates from https://currencyapi.com/ +- [CurrencyFreaks API](CurrencyFreaksApi.md) - Get exchange rates from https://currencyfreaks.com/ +- [Currency Layer API](CurrencylayerApi.md) - Get exchange rates from https://currencylayer.com/ +- [Exchange Rates API](ExchangeRatesApi.md) - Get exchange rates from https://www.exchangerate-api.com/ +- [Fixer API](FixerApi.md) - Get exchange rates from https://fixer.io/ +- [Open Exchange Rates API](OpenExchangeRatesApi.md) - Get exchange rates from https://openexchangerates.org/ + From 621879963a2cec769f970742c2e739433f2342f4 Mon Sep 17 00:00:00 2001 From: Anil Chaudhari Date: Sat, 6 Apr 2024 16:11:09 +0545 Subject: [PATCH 3/3] enh: code refactured --- {resource => resources}/codes.json | 0 src/CurrencyConverter.php | 38 ++++++++++-------------- src/Interface/RateConverterInterface.php | 2 +- src/Interface/RateProviderInterface.php | 8 +++-- src/Provider/ApiForexApi.php | 6 +++- src/Provider/CurrencyApi.php | 6 +++- src/Provider/CurrencyFreaksApi.php | 6 +++- src/Provider/CurrencylayerApi.php | 8 +++-- src/Provider/ExchangeRatesApi.php | 2 ++ src/Provider/FixerApi.php | 6 +++- src/Provider/OpenExchangeRatesApi.php | 6 +++- 11 files changed, 55 insertions(+), 33 deletions(-) rename {resource => resources}/codes.json (100%) diff --git a/resource/codes.json b/resources/codes.json similarity index 100% rename from resource/codes.json rename to resources/codes.json diff --git a/src/CurrencyConverter.php b/src/CurrencyConverter.php index fbc1d5d..7fa562d 100644 --- a/src/CurrencyConverter.php +++ b/src/CurrencyConverter.php @@ -40,11 +40,13 @@ class CurrencyConverter extends Component implements RateConverterInterface { /** * Cache duration + * + * @var int $duration */ public $duration = 3600; /** - * @var array request object configuration. + * @var array object configuration. */ public $provider = [ 'class' => 'imanilchaudhari\CurrencyConverter\Provider\ExchangeRatesApi' @@ -62,8 +64,8 @@ public function convert($source, $target, $amount = 1) { $cache = Yii::$app->cache; - $sourceCurrency = $this->parseCurrencyArgument($source); - $targetCurrency = $this->parseCurrencyArgument($target); + $sourceCurrency = is_array($source) ? $this->parseCurrencyArgument($source) : $source; + $targetCurrency = is_array($source) ? $this->parseCurrencyArgument($target) : $target; if ($cache) { if ($rate = $cache->get($sourceCurrency . '_ ' . $targetCurrency . '_cache')) { @@ -89,11 +91,10 @@ public function convert($source, $target, $amount = 1) */ public function getRateProvider() { - if (!is_object($this->_provider)) { - $this->setRateProvider($this->provider); + if ($this->_provider instanceof RateProviderInterface) { + return $this->_provider; } - - return $this->_provider; + return $this->setRateProvider($this->provider); } /** @@ -104,31 +105,24 @@ public function getRateProvider() */ protected function setRateProvider($config) { - $this->_provider = Yii::createObject($config); - return $this; + return $this->_provider = Yii::createObject($config); } /** * Parses the Currency Arguments * - * @param string|array $data + * @param array $data * @return string * @throws InvalidArgumentException */ protected function parseCurrencyArgument($data) { - if (is_string($data)) { - $currency = $data; - } elseif (is_array($data)) { - if (isset($data['country'])) { - $currency = $this->getCurrencyCode($data['country']); - } elseif (isset($data['currency'])) { - $currency = $data['currency']; - } else { - throw new InvalidArgumentException('Please provide country or currency!'); - } + if (isset($data['country'])) { + $currency = $this->getCurrencyCode($data['country']); + } elseif (isset($data['currency'])) { + $currency = $data['currency']; } else { - throw new InvalidArgumentException('Invalid currency provided. String or array expected.'); + throw new InvalidArgumentException('Please provide country or currency!'); } return $currency; @@ -143,7 +137,7 @@ protected function parseCurrencyArgument($data) */ protected function getCurrencyCode($countryCode) { - $currencies = Json::decode([file_get_contents(__DIR__ . '/resource/codes.json')], true); + $currencies = Json::decode(file_get_contents(__DIR__ . '/resources/codes.json'), true); if (!array_key_exists($countryCode, $currencies)) { throw new InvalidArgumentException(sprintf('Unsupported country code, %s', $countryCode)); } diff --git a/src/Interface/RateConverterInterface.php b/src/Interface/RateConverterInterface.php index 33038cb..5b6022f 100644 --- a/src/Interface/RateConverterInterface.php +++ b/src/Interface/RateConverterInterface.php @@ -15,7 +15,7 @@ interface RateConverterInterface * * @param array|string $source * @param array|string $target - * @param float optional $amount + * @param int|float $amount * * @return float */ diff --git a/src/Interface/RateProviderInterface.php b/src/Interface/RateProviderInterface.php index 633dbf9..aebb1b0 100644 --- a/src/Interface/RateProviderInterface.php +++ b/src/Interface/RateProviderInterface.php @@ -13,9 +13,11 @@ interface RateProviderInterface /** * Gets exchange rate from cache * - * @param string $sourceCurrency - * @param string $targetCurrency + * @param string $source + * @param string $target * @return float + * + * @throws \Exception */ - public function getRate($sourceCurrency, $targetCurrency); + public function getRate($source, $target); } diff --git a/src/Provider/ApiForexApi.php b/src/Provider/ApiForexApi.php index bef375b..429ffd0 100644 --- a/src/Provider/ApiForexApi.php +++ b/src/Provider/ApiForexApi.php @@ -46,16 +46,20 @@ class ApiForexApi implements RateProviderInterface /** * Yii http client + * + * @var Client */ private $_client; /** * Create a new provider instance. * + * @param string $apiKey * @return void */ - public function __construct() + public function __construct($apiKey) { + $this->apiKey = $apiKey; $this->_client = new Client([ 'baseUrl' => 'https://v2.api.forex', 'transport' => 'yii\httpclient\CurlTransport', diff --git a/src/Provider/CurrencyApi.php b/src/Provider/CurrencyApi.php index 965dd53..9dd19f8 100644 --- a/src/Provider/CurrencyApi.php +++ b/src/Provider/CurrencyApi.php @@ -48,16 +48,20 @@ class CurrencyApi implements RateProviderInterface /** * Yii http client + * + * @var Client */ private $_client; /** * Create a new provider instance. * + * @param string $apiKey * @return void */ - public function __construct() + public function __construct($apiKey) { + $this->apiKey = $apiKey; $this->_client = new Client([ 'baseUrl' => 'https://api.currencyapi.com', 'transport' => 'yii\httpclient\CurlTransport', diff --git a/src/Provider/CurrencyFreaksApi.php b/src/Provider/CurrencyFreaksApi.php index fb7f41d..6496405 100644 --- a/src/Provider/CurrencyFreaksApi.php +++ b/src/Provider/CurrencyFreaksApi.php @@ -47,16 +47,20 @@ class CurrencyFreaksApi implements RateProviderInterface /** * Yii http client + * + * @var Client */ private $_client; /** * Create a new provider instance. * + * @param string $apiKey * @return void */ - public function __construct() + public function __construct($apiKey) { + $this->apiKey = $apiKey; $this->_client = new Client([ 'baseUrl' => 'https://api.currencyfreaks.com', 'transport' => 'yii\httpclient\CurlTransport', diff --git a/src/Provider/CurrencylayerApi.php b/src/Provider/CurrencylayerApi.php index 80d4ca0..dab7d32 100644 --- a/src/Provider/CurrencylayerApi.php +++ b/src/Provider/CurrencylayerApi.php @@ -9,8 +9,8 @@ namespace imanilchaudhari\CurrencyConverter\Provider; use yii\httpclient\Client; -use imanilchaudhari\CurrencyConverter\Interface\RateProviderInterface; use yii\base\InvalidConfigException; +use imanilchaudhari\CurrencyConverter\Interface\RateProviderInterface; /** * Currency Layer provides currency conversion, current and historical forex exchange rate @@ -47,16 +47,20 @@ class CurrencylayerApi implements RateProviderInterface /** * Yii http client + * + * @var Client */ private $_client; /** * Create a new provider instance. * + * @param string $access_key * @return void */ - public function __construct() + public function __construct($access_key) { + $this->access_key = $access_key; $this->_client = new Client([ 'baseUrl' => 'http://www.apilayer.net', 'transport' => 'yii\httpclient\CurlTransport', diff --git a/src/Provider/ExchangeRatesApi.php b/src/Provider/ExchangeRatesApi.php index fa057af..b9b188e 100644 --- a/src/Provider/ExchangeRatesApi.php +++ b/src/Provider/ExchangeRatesApi.php @@ -39,6 +39,8 @@ class ExchangeRatesApi implements RateProviderInterface { /** * Yii http client + * + * @var Client */ private $_client; diff --git a/src/Provider/FixerApi.php b/src/Provider/FixerApi.php index 06ce95e..bb5ae19 100644 --- a/src/Provider/FixerApi.php +++ b/src/Provider/FixerApi.php @@ -48,16 +48,20 @@ class FixerApi implements RateProviderInterface /** * Yii http client + * + * @var Client */ private $_client; /** * Create a new provider instance. * + * @param string $access_key * @return void */ - public function __construct() + public function __construct($access_key) { + $this->access_key = $access_key; $this->_client = new Client([ 'baseUrl' => 'https://data.fixer.io', 'transport' => 'yii\httpclient\CurlTransport', diff --git a/src/Provider/OpenExchangeRatesApi.php b/src/Provider/OpenExchangeRatesApi.php index 731bd82..f0a12d4 100644 --- a/src/Provider/OpenExchangeRatesApi.php +++ b/src/Provider/OpenExchangeRatesApi.php @@ -47,16 +47,20 @@ class OpenExchangeRatesApi implements RateProviderInterface /** * Yii http client + * + * @var Client */ private $_client; /** * Create a new provider instance. * + * @param string $appId * @return void */ - public function __construct() + public function __construct($appId) { + $this->appId = $appId; $this->_client = new Client([ 'baseUrl' => 'https://openexchangerates.org', 'transport' => 'yii\httpclient\CurlTransport',