ob-ivan / sd-currency
此包最新版本(v4.1)没有可用的许可信息。
v4.1
2018-03-20 10:52 UTC
Requires
- ob-ivan/sd-config: ^1.1
- ob-ivan/sd-dependency-injection: ^1.5
Requires (Dev)
- phpunit/phpunit: ^6.2
README
一组实用函数,用于枚举可用货币及其实际汇率。
安装
composer require ob-ivan/sd-currency
用法
注册表
货币实例将货币代码与其Unicode表示和HTML实体表示关联。
支持以下货币
- RUB
- EUR
- USD
您可以使用注册表实例来枚举可用货币
use SD\Currency\Model\Registry; $registry = new Registry(); foreach ($registry->getAll() as $currency) { print "<p>The Unicode symbol for {$currency->getCode()} is {$currency->getUnicode()}</p>\n"; }
或者如果您使用依赖注入(见下文)
foreach ($this->getCurrency()->getRegistry()->getAll() as $currency) { ... }
格式化器
使用格式化器服务根据给定货币格式化价格。格式化器实例使用多个配置参数。
use SD\Currency\Model\Money; use SD\Currency\Model\Registry; use SD\Currency\Service\Formatter; $registry = new Registry(); $formatter = new Formatter($registry, ['thousandSeparator' => '\'']); // or using dependency injection: $formatter = $this->getCurrency()->getFormatter($formatName); echo $formatter->formatMoney(new Money(10000, $registry->getByCode('USD'))); // $ 10'000 echo $formatter->formatMoney(new Money(590000, $registry->getByCode('RUB'))); // 590'000 ₽
存储库 & 存储库
所有功能都可在存储库实例中使用,该实例充当库外观
use SD\Currency\Repository; $repository = new Repository($config);
其中一些方法需要配置存储类
use SD\Currency\Repository; use SD\Currency\Store\FileStore; $repository = new Repository([ 'store' => FileStore::class, 'args' => [ 'dir' => __DIR__ . '/currency_cache', ], ]); $options = $repository->getOptions();
存储库用于检索货币汇率并在更新时持久化它们(见更新部分)。
此库提供了两种存储实现
FileStore
在硬盘上使用json文件。ArrayStore
仅在内存中保留数据,非常适合单元测试。
您还可以实现 SD\Currency\Store\StoreInterface
以提供自己的存储类型,例如数据库或memcache。然后将其类名传递给上述 Repository
构造函数。
更新器
更新器服务针对提供的存储库运行,并从固定源设置汇率。默认情况下使用俄罗斯中央银行的官方API。如果您不想对源进行过多请求,可以配置更新器以使用自己的XML源和XPath以及更新间隔(默认为'1天')。
use SD\Currency\Model\Registry; use SD\Currency\Service\Updater; use SD\Currency\Store\FileStore; $registry = new Registry(); $store = new FileStore(__DIR__); $updaterConfig = [ 'update_interval' => '30 minutes', ]; $updater = new Updater($registry, $store, $updaterConfig); $updater->updateRates();
或者如果您使用依赖注入
$this->getCurrency()->getUpdater()->updateRates();
依赖注入
如果您在应用程序中使用 SD\DependencyInjection\Container
,消费者可以导入依赖注入特性以利用自动声明功能。
use SD\Currency\DependencyInjection\CurrencyAwareTrait; use SD\DependencyInjection\AutoDeclarerInterface; use SD\DependencyInjection\AutoDeclarerTrait; class ExampleController implements AutoDeclarerInterface { use AutoDeclarerTrait; use CurrencyAwareTrait; public function exampleAction() { return $this->render('example.twig', [ 'currencyOptions' => $this->getCurrency()->getOptions(), ]); } }
配置
使用依赖注入时,您可能需要提供一个配置文件来设置服务。
# config/currency.yaml currency: store: class: App\Currency\Store formatter: class: App\Currency\Formatter myAwesomeFormat: thousandSeparator: ',' symbolType: fontAwesome roundDirection: ceil roundDigits: 2 myOtherFormat: ... updater: class: App\Currency\Updater config: url: https://money.example.com/ xpath: //currency[code = "$code"]/rate updateInterval: 3 hours
使用 ConfigLoader
用配置值填充容器
use SD\Config\ConfigLoader; use SD\Currency\DependencyInjection\CurrencyProvider; use SD\DependencyInjection\Container; $loader = new ConfigLoader('/path/to/config/dir'); $config = $loader->load(); $container = new Container(['config' => $config]); $container->connect(new CurrencyProvider());
这将配置值注入到相应的服务中
$container->inject(function ($currency) { $store = $currency->getStore(); // instance of App\Currency\Store $formatter = $currency->getFormatter('myAwesomeFormat'); // uses config values $updater = $currency->getUpdater(); // uses config values });
开发
运行测试
composer install vendor/bin/phpunit