Skip to content

Commit

Permalink
1.3.0
Browse files Browse the repository at this point in the history
* [+] added format_method option: `legacy` for old `money_format()` (default), `numfmt` for `numfmt_format_currency()` (PHP 7.4+)
* [*] now throws RuntimeException instead of Exception
* [*] `selectCurrencySet()` method have all params optional
  • Loading branch information
KarelWintersky committed Jul 12, 2021
1 parent f1b05f3 commit 0bf2eac
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 13 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "AJUR Media Steamboat Currency Toolkit",
"type": "library",
"license": "MIT",
"version": "1.2.0",
"version": "1.3.0",
"authors": [
{
"name": "Karel Wintersky",
Expand Down
27 changes: 16 additions & 11 deletions sources/Currency.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@

use Curl\Curl;
use DateTime;
use Exception;
use RuntimeException;
use NumberFormatter;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use RuntimeException;

class Currency implements CurrencyInterface
{
Expand All @@ -20,6 +19,7 @@ class Currency implements CurrencyInterface
*/
private static $options = [
'out_format' => "%01.2f",
'format_method' => 'legacy',
];

/**
Expand Down Expand Up @@ -62,6 +62,14 @@ public static function init($options = [], LoggerInterface $logger = null)
= 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
Expand All @@ -75,18 +83,16 @@ public static function init($options = [], LoggerInterface $logger = null)
* @param array $codes
* @param null $fetch_date
* @return bool
* @throws Exception
*/
public static function selectCurrencySet(array $codes, $fetch_date = null):bool
public static function selectCurrencySet(array $codes = [], $fetch_date = null):bool
{
$cbr_prices = [];
$cbr_prices_compact = [];

$daily = self::loadCurrencyDataset($fetch_date);

if (!$daily || !array_key_exists('Valute', $daily)) {
// self::$logger->error("[ERROR] CBR API returns empty data", [ $daily ]);
throw new Exception("[ERROR] CBR API returns empty data");
throw new RuntimeException("[ERROR] CBR API returns empty data");
}

$cbr_prices_full = array_filter($daily['Valute'], function ($price) use (&$cbr_prices, &$cbr_prices_compact, $codes) {
Expand Down Expand Up @@ -166,12 +172,12 @@ public static function loadFile(string $filename): array
}

$file_content = file_get_contents($filename);
if ($file_content === FALSE) {
if ($file_content === false) {
throw new RuntimeException( "Currency file `{$filename}` not found", 1 );
}

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

Expand All @@ -195,7 +201,6 @@ public static function loadFile(string $filename): array
/**
* @param $fetch_date
* @return mixed
* @throws Exception
*/
private static function loadCurrencyDataset($fetch_date)
{
Expand All @@ -212,7 +217,7 @@ private static function loadCurrencyDataset($fetch_date)
]);

if ($curl->error)
throw new Exception("[CURL] Error", $curl->error_code);
throw new RuntimeException("[CURL] Error", $curl->error_code);

$xml = simplexml_load_string($curl->response);
$json = json_encode( $xml );
Expand All @@ -227,7 +232,7 @@ private static function loadCurrencyDataset($fetch_date)
*/
private static function formatCurrencyValue($value)
{
if (PHP_VERSION_ID >= 70400) {
if (PHP_VERSION_ID >= 70400 && self::$options['format_method'] === 'numfmt') {
return numfmt_format_currency(numfmt_create( 'ru_RU', NumberFormatter::CURRENCY ), $value, "RUR");
}

Expand Down
3 changes: 2 additions & 1 deletion tests/test.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
require_once '../vendor/autoload.php';

try {
Currency::selectCurrencySet([]);
Currency::init();
Currency::selectCurrencySet();

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

0 comments on commit 0bf2eac

Please sign in to comment.