diff options
author | Daniel Weipert <code@drogueronin.de> | 2023-08-22 15:01:03 +0200 |
---|---|---|
committer | Daniel Weipert <code@drogueronin.de> | 2023-08-22 15:01:03 +0200 |
commit | c0354b250f84d578b609a7f25d71dee7fc24e9ca (patch) | |
tree | af586e0a4c44a2f2c8df956ca3b992be15daaba3 /db/currencies/currencies.php | |
parent | 54e5ffaeb79f989463c144e58dfcd677b752c5a9 (diff) |
currency, save/load
Diffstat (limited to 'db/currencies/currencies.php')
-rw-r--r-- | db/currencies/currencies.php | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/db/currencies/currencies.php b/db/currencies/currencies.php new file mode 100644 index 0000000..7c8b2ac --- /dev/null +++ b/db/currencies/currencies.php @@ -0,0 +1,42 @@ +<?php + +$currencySymbols = []; +$handle = fopen('https://raw.githubusercontent.com/bengourley/currency-symbol-map/master/map.js', 'r'); +while ($line = fgets($handle)) { + preg_match("/(\w+): '(.+)'/", $line, $match); + + if (isset($match[1])) { + $currencySymbols[$match[1]] = $match[2]; + } +} + +$currencyNames = json_decode(file_get_contents('https://openexchangerates.org/api/currencies.json'), true); + +$decimals = [ + 'JPY' => 0, +]; + +$currencies = []; +$apiResponse = json_decode(file_get_contents('https://api.frankfurter.app/latest?base=eur'), true); +$currencies['last_updated'] = $apiResponse['date']; +foreach ($apiResponse['rates'] as $code => $rate) { + $currencies['map'][$code] = [ + 'rate' => $rate, + 'symbol' => $currencySymbols[$code], + 'name' => $currencyNames[$code], + 'decimals' => $decimals[$code] ?? 2, + ]; +} + +// add EUR +$currencies['map']['EUR'] = [ + 'rate' => 1, + 'symbol' => $currencySymbols['EUR'], + 'name' => $currencyNames['EUR'], + 'decimals' => 2, +]; + +file_put_contents( + dirname(__DIR__) . '/_generated/currencies.json', + json_encode($currencies) +); |