From b081b3b056cd8d2947a4697758d21e0076411f03 Mon Sep 17 00:00:00 2001 From: Karel Wintersky Date: Fri, 23 Aug 2024 15:14:31 +0300 Subject: [PATCH] 1.99.0 * [*] PHP8+ compatible version --- composer.json | 6 ++++- interfaces/CurrencyInterface.php | 6 +++-- sources/Currency.php | 39 ++++++++++++++++---------------- tests/test.php | 2 +- 4 files changed, 29 insertions(+), 24 deletions(-) diff --git a/composer.json b/composer.json index d3f7f90..958ad0c 100644 --- a/composer.json +++ b/composer.json @@ -10,12 +10,13 @@ } ], "require": { + "php": "^7.4 | 8.*", "ext-curl": "*", "ext-json": "*", "ext-intl": "*", "ext-simplexml": "*", "psr/log": "^1.1", - "curl/curl": "^2.3" + "curl/curl": "^2" }, "autoload": { "psr-4": { @@ -28,5 +29,8 @@ "support": { "source": "https://github.com/ajur-media/ajur.currency", "issues": "https://github.com/ajur-media/ajur.currency/issues" + }, + "require-dev": { + "rector/rector": "^1.2" } } diff --git a/interfaces/CurrencyInterface.php b/interfaces/CurrencyInterface.php index dcda3c2..a5398cc 100644 --- a/interfaces/CurrencyInterface.php +++ b/interfaces/CurrencyInterface.php @@ -6,6 +6,9 @@ interface CurrencyInterface { + /** + * + */ const CBR_URL = 'http://www.cbr.ru/scripts/XML_daily.asp'; const credentials = [ @@ -23,7 +26,7 @@ interface CurrencyInterface * @param LoggerInterface|null $logger * @return mixed */ - public static function init($options = [], LoggerInterface $logger = null); + public static function init(array $options = [], LoggerInterface $logger = null); /** * Фильтрует набор данных из ЦБР на предмет валют по набору кодов @@ -59,7 +62,6 @@ public static function storeFile(string $filename) ; * Загружает данные из файла * * @param string $filename - * @param LoggerInterface|null $logger * @return array */ public static function loadFile(string $filename):array; diff --git a/sources/Currency.php b/sources/Currency.php index c77b36e..db6385e 100644 --- a/sources/Currency.php +++ b/sources/Currency.php @@ -5,7 +5,6 @@ use Curl\Curl; use DateTime; use RuntimeException; -use NumberFormatter; use Psr\Log\LoggerInterface; use Psr\Log\NullLogger; @@ -45,24 +44,25 @@ class Currency implements CurrencyInterface * @var NullLogger|LoggerInterface|null */ private static $logger = null; - + /** * @param array $options * @param LoggerInterface|null $logger */ - public static function init($options = [], LoggerInterface $logger = null) + public static function init(array $options = [], LoggerInterface $logger = null) { self::$options['locale'] = array_key_exists('locale', $options) ? $options['locale'] : 'ru_RU'; + setlocale(LC_MONETARY, self::$options['locale']); self::$options['out_format'] = array_key_exists('out_format', $options) ? $options['out_format'] : "%01.2f"; - + self::$options['format_method'] = array_key_exists('format_method', $options) ? $options['format_method'] @@ -70,7 +70,7 @@ public static function init($options = [], LoggerInterface $logger = null) if (!in_array(self::$options['format_method'], ['legacy', 'numfmt'])) { self::$options['format_method'] = 'legacy'; } - + self::$logger = $logger instanceof LoggerInterface ? $logger @@ -169,15 +169,15 @@ public static function loadFile(string $filename): array if (empty($filename)) { throw new RuntimeException( "Currency file not defined (null or empty string given)", 4 ); } - + if (!file_exists($filename)) { throw new RuntimeException("Currency file `{$filename}` not found", 1); } - + if (!is_readable($filename)) { throw new RuntimeException("Currency file `{$filename}` not readable", 1); } - + $file_content = file_get_contents($filename); if ($file_content === false) { throw new RuntimeException( "Currency file `{$filename}` can't be retrieved", 1 ); @@ -201,7 +201,7 @@ public static function loadFile(string $filename): array if (self::$logger instanceof LoggerInterface) { self::$logger->error('[ERROR] Load Currency ', [$e->getMessage()]); } - + } return $current_currency; @@ -213,7 +213,7 @@ public static function loadFile(string $filename): array */ private static function loadCurrencyDataset($fetch_date) { - $fetch_date = $fetch_date ?? (new DateTime())->format('d/m/Y'); + $fetch_date ??= (new DateTime())->format('d/m/Y'); $url = self::CBR_URL; $curl = new Curl(); @@ -239,12 +239,12 @@ private static function loadCurrencyDataset($fetch_date) * @param $value * @return string */ - private static function formatCurrencyValue($value) + private static function formatCurrencyValue($value): string { // return money_format('%i', str_replace(',', '.', $value)); return number_format(str_replace(',', '.', $value), 2, '.', ''); } - + /** * Форматирует деньги с учетом параметров локали. * Это НЕ числовое форматирование @@ -255,8 +255,7 @@ private static function formatCurrencyValue($value) */ public static function money_format_with_locale($format, $number) { - $regex = '/%((?:[\^!\-]|\+|\(|\=.)*)([0-9]+)?'. - '(?:#([0-9]+))?(?:\.([0-9]+))?([in%])/'; + $regex = '/%((?:[\^!\-]|\+|\(|\=.)*)([0-9]+)?(?:#([0-9]+))?(?:\.([0-9]+))?([in%])/'; if (setlocale(LC_MONETARY, 0) == 'C') { setlocale(LC_MONETARY, ''); } @@ -275,16 +274,16 @@ public static function money_format_with_locale($format, $number) $left = trim($fmatch[3]) ? (int)$fmatch[3] : 0; $right = trim($fmatch[4]) === '' ? $locale['int_frac_digits'] : (int)$fmatch[4]; $conversion = $fmatch[5]; - + $positive = true; if ($value < 0) { $positive = false; $value *= -1; } $letter = $positive ? 'p' : 'n'; - + $prefix = $suffix = $cprefix = $csuffix = $signal = ''; - + $signal = $positive ? $locale['positive_sign'] : $locale['negative_sign']; switch (true) { case $locale["{$letter}_sign_posn"] == 1 && $flags['usesignal'] == '+': @@ -311,7 +310,7 @@ public static function money_format_with_locale($format, $number) $currency = ''; } $space = $locale["{$letter}_sep_by_space"] ? ' ' : ''; - + $value = number_format( $value, $right, @@ -319,7 +318,7 @@ public static function money_format_with_locale($format, $number) $flags['nogroup'] ? '' : $locale['mon_thousands_sep'] ); $value = @explode($locale['mon_decimal_point'], $value); - + $n = strlen($prefix) + strlen($currency) + strlen($value[0]); if ($left > 0 && $left > $n) { $value[0] = str_repeat($flags['fillchar'], $left - $n) . $value[0]; @@ -338,7 +337,7 @@ public static function money_format_with_locale($format, $number) $flags['isleft'] ? STR_PAD_RIGHT : STR_PAD_LEFT ); } - + $format = str_replace($fmatch[0], $value, $format); } return $format; diff --git a/tests/test.php b/tests/test.php index b2bf8ea..049bdb2 100644 --- a/tests/test.php +++ b/tests/test.php @@ -2,7 +2,7 @@ use AJUR\Toolkit\Currency; -require_once '../vendor/autoload.php'; +require_once __DIR__ . '/../vendor/autoload.php'; try { Currency::init([