Skip to content

Commit

Permalink
1.99.0
Browse files Browse the repository at this point in the history
* [*] PHP8+ compatible version
  • Loading branch information
Karel Wintersky committed Aug 23, 2024
1 parent 0b22899 commit b081b3b
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 24 deletions.
6 changes: 5 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand All @@ -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"
}
}
6 changes: 4 additions & 2 deletions interfaces/CurrencyInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

interface CurrencyInterface
{
/**
*
*/
const CBR_URL = 'http://www.cbr.ru/scripts/XML_daily.asp';

const credentials = [
Expand All @@ -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);

/**
* Фильтрует набор данных из ЦБР на предмет валют по набору кодов
Expand Down Expand Up @@ -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;
Expand Down
39 changes: 19 additions & 20 deletions sources/Currency.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Curl\Curl;
use DateTime;
use RuntimeException;
use NumberFormatter;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;

Expand Down Expand Up @@ -45,32 +44,33 @@ 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']
: 'legacy';
if (!in_array(self::$options['format_method'], ['legacy', 'numfmt'])) {
self::$options['format_method'] = 'legacy';
}

self::$logger
= $logger instanceof LoggerInterface
? $logger
Expand Down Expand Up @@ -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 );
Expand All @@ -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;
Expand All @@ -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();
Expand All @@ -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, '.', '');
}

/**
* Форматирует деньги с учетом параметров локали.
* Это НЕ числовое форматирование
Expand All @@ -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, '');
}
Expand All @@ -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'] == '+':
Expand All @@ -311,15 +310,15 @@ public static function money_format_with_locale($format, $number)
$currency = '';
}
$space = $locale["{$letter}_sep_by_space"] ? ' ' : '';

$value = number_format(
$value,
$right,
$locale['mon_decimal_point'],
$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];
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion tests/test.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use AJUR\Toolkit\Currency;

require_once '../vendor/autoload.php';
require_once __DIR__ . '/../vendor/autoload.php';

try {
Currency::init([
Expand Down

0 comments on commit b081b3b

Please sign in to comment.