Skip to content

Commit

Permalink
1.3.1
Browse files Browse the repository at this point in the history
* [*] more exception checks
* [*] check Logger instanceof
  • Loading branch information
KarelWintersky committed Dec 11, 2021
1 parent 0bf2eac commit 56dde1d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
23 changes: 16 additions & 7 deletions sources/Currency.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class Currency implements CurrencyInterface
/**
* @var NullLogger|LoggerInterface|null
*/
private static $logger;
private static $logger = null;

/**
* @param array $options
Expand Down Expand Up @@ -159,7 +159,6 @@ public static function storeFile(string $filename)
* Загружает данные из файла
*
* @param string $filename
* @param LoggerInterface|null $logger
* @return array
*/
public static function loadFile(string $filename): array
Expand All @@ -171,13 +170,21 @@ public static function loadFile(string $filename): array
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}` not found", 1 );
throw new RuntimeException( "Currency file `{$filename}` can't be retrieved", 1 );
}

$file_content = json_decode($file_content, true);
if (($file_content === null) || !is_array($file_content)) {
if (!is_array($file_content)) {
throw new RuntimeException( "Currency data can't be parsed", 2 );
}

Expand All @@ -187,12 +194,14 @@ public static function loadFile(string $filename): array

// добиваем валюту до $MAX_CURRENCY_STRING_LENGTH нулями (то есть 55.4 (4 десятых) добивается до 55.40 (40 копеек)
foreach ($file_content['summary'] as $currency_code => $currency_data) {
// $current_currency[ $currency_code ] = str_pad($currency_data, self::$options['max_currency_string_length'], '0');
$current_currency[ $currency_code ] = sprintf(self::$options['out_format'], $currency_data);
}

} catch (RuntimeException $e) {
self::$logger->error('[ERROR] Load Currency ', [$e->getMessage()]);
if (self::$logger instanceof LoggerInterface) {
self::$logger->error('[ERROR] Load Currency ', [$e->getMessage()]);
}

}

return $current_currency;
Expand Down Expand Up @@ -232,7 +241,7 @@ private static function loadCurrencyDataset($fetch_date)
*/
private static function formatCurrencyValue($value)
{
if (PHP_VERSION_ID >= 70400 && self::$options['format_method'] === 'numfmt') {
if (function_exists('numfmt_format_currency') && function_exists('numfmt_create')) {
return numfmt_format_currency(numfmt_create( 'ru_RU', NumberFormatter::CURRENCY ), $value, "RUR");
}

Expand Down
6 changes: 4 additions & 2 deletions tests/test.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
require_once '../vendor/autoload.php';

try {
Currency::init();
Currency::selectCurrencySet();
Currency::init([
'format_method' => 'numfmp'
]);
Currency::selectCurrencySet(['USD', 'EUR']);

Currency::storeFile('test.json');
} catch (Exception $e) {
Expand Down

0 comments on commit 56dde1d

Please sign in to comment.