ordinary9843/currency-master

CurrencyMaster 是一个用于管理货币兑换率的综合性库。它允许您高效、方便地检索和转换汇率。该库通过实现可定制的缓存系统,消除了对第三方服务的过度依赖。

v1.0.1 2024-03-26 16:44 UTC

This package is auto-updated.

Last update: 2024-09-16 18:20:37 UTC


README

build codecov

如果您有任何期望的功能,请提出问题,我会尽力满足您的需求!

简介

CurrencyMaster 是一个用于管理货币兑换率的综合性库。它允许您高效、方便地检索和转换汇率。该库通过实现可定制的缓存系统,消除了对第三方服务的过度依赖。

核心功能

此库具有以下特点:

  • 全功能:此库提供对检索和转换汇率的全面支持。
  • 对外部库的依赖性较低。
  • 兼容多个 PHP 版本:它可以在 PHP 7.2 - 8.x 上正常运行。

安装

安装 ordinary9843/currency-master

composer require ordinary9843/currency-master

用法

示例用法

<?php
require './vendor/autoload.php';

use Ordinary9843\CurrentMaster;
use Ordinary9843\Constants\ExchangeRateTypeConstant;
use Ordinary9843\Constants\SourceConstant;

$currentMaster = new CurrentMaster();

/**
 * Sets the data source to be used.
 */
$currentMaster->setSourceType(SourceConstant::BOT);

/**
 * Sets the duration, in seconds, for which the data source should be cached.
 */
$currentMaster->setCacheTimeInSeconds(60);

/**
 * Retrieves all exchange rates from the source set. 
 *
 * Output:
 * [
 *   'TWD' => [
 *     'currency' => 'TWD',
 *     'cashBuy' => 1,
 *     'cashSell' => 1,
 *     'spotBuy' => 1,
 *     'spotSell' => 1
 *   ],
 *   'USD' => [
 *     'currency' => 'USD',
 *     'cashBuy' => 31.465,
 *     'cashSell' => 32.135,
 *     'spotBuy' => 31.815,
 *     'spotSell' => 31.915
 *   ]
 * ]
 */
$currentMaster->getExchangeRates();

/**
 * Converts a specified amount of money from one currency to another.
 *
 * Output: [
 *  'fromCurrency' => 'TWD',
 *  'toCurrency' => 'USD',
 *  'convertExchangeRate' => '0.031690698779',
 *  'convertedAmount' => '31.690698779000',
 *  'formattedConvertedAmount' => '$31.690698779000',
 *  'currencySymbol' => '$'
 * ]
 */
$currentMaster->exchangeRate(CurrencyConstant::TWD, CurrencyConstant::USD, ExchangeRateTypeConstant::CASH_BUY, 1000);

测试

运行测试

composer test

支持的货币单位

以下表格列出了 CurrencyMaster 支持的所有货币单位。

货币单位的当前来源

许可证

(MIT 许可证)