maxlzp/money

There is no license information available for the latest version (dev-master) of this package.

Php implementation of Money entity

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

This package is auto-updated.

Last update: 2024-04-14 21:23:40 UTC


README

Create

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

Reset supported currencies

Create a class implementing 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);
    }
}

Reset currencies source at Currency class:

Currency::resetCurrenciesSource(new UpdatedCurrenciesSource());