maxlzp/money

该软件包最新版本(dev-master)没有提供许可信息。

货币实体的PHP实现

dev-master 2020-08-14 13:18 UTC

This package is auto-updated.

Last update: 2024-09-14 22:15:50 UTC


README

创建

$fiveCents = Money::USD(5);
$oneEuro = Money::EUR(100); 

重置支持的货币

创建一个实现CurrenciesSourceInterface接口的类

class UpdatedCurrenciesSource implements CurrenciesSourceInterface
{
    protected $data = [
            'USD' => [
                'name' => 'US Dollar',
                'isoCode' => '840',
            ],
        ];
        
    /**
     * Return CurrencyDto. Possibly from some storage/
     * @param string $currencyCode
     * @return CurrencyDto
     * @throws CurrencyUnsupportedException
     */
    public function getWithCode(string $currencyCode): CurrencyDto
    {
        if (\array_key_exists($currencyCode, $this->data))
        {
            return new CurrencyDto(
                $currencyCode,
                $this->data[$currencyCode]['isoCode'],
                $this->data[$currencyCode]['name']
            );
        }
        throw new CurrencyUnsupportedException($currencyCode);
    }
}

在Currency类中重置货币源

Currency::resetCurrenciesSource(new UpdatedCurrenciesSource());