PHP code example of svk-digital / laravel-currency
1. Go to this page and download the library: Download svk-digital/laravel-currency library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
svk-digital / laravel-currency example snippets
use DateTimeImmutable;
use SvkDigital\Currency\Contracts\CurrencyServiceContract;
use SvkDigital\Currency\Facades\Currency;
use SvkDigital\Currency\ValueObjects\FiatCurrency;
final class RatesController
{
public function __construct(private CurrencyServiceContract $currencyService) {}
public function __invoke()
{
$date = new DateTimeImmutable('2024-11-30');
// Via dependency injection
$rate = $this->currencyService
->rate()
->base(new FiatCurrency('RUB'))
->quote(new FiatCurrency('USD'))
->date($date)
->get();
// Or via facade
$rate = Currency::rate()
->base(new FiatCurrency('RUB'))
->quote(new FiatCurrency('USD'))
->date($date)
->get();
}
}
use DateTimeImmutable;
use SvkDigital\Currency\Facades\Currency;
use SvkDigital\Currency\ValueObjects\FiatCurrency;
// In your test setup
Currency::fake()
->setRateValue(
new FiatCurrency('RUB'),
new FiatCurrency('USD'),
new DateTimeImmutable('2024-01-01'),
90.5
);
// Now your code will use the fake adapter
$rate = Currency::rate()
->base(new FiatCurrency('RUB'))
->quote(new FiatCurrency('USD'))
->date(new DateTimeImmutable('2024-01-01'))
->get();