Advanced Usage#
In case you need to use all power of Peso, you can use the core directly.
Installation#
Depend on peso/core if you are creating an integration:
composer require peso/core
If you just want to use services directly, just install the service you need, ECB for example:
composer require peso/ecb-service php-http/discovery guzzlehttp/guzzle symfony/cache
Model#
Services operate on request/response model.
Action |
Request |
Response |
|---|---|---|
Current exchange rate |
|
|
Historical exchange rate |
|
|
Conversion by the current rate |
|
|
Conversion by a historical rate |
|
|
Future scope and invalid classes |
any other object |
|
Exchange Rates#
\Peso\Core\Requests\CurrentExchangeRateRequestqueries the currently active exchange rates.\Peso\Core\Requests\HistoricalExchangeRateRequestqueries exchange rates on a specific date.
Examples:
<?php
use Arokettu\Date\Calendar;
use Peso\Core\Requests\CurrentExchangeRateRequest;
use Peso\Core\Requests\HistoricalExchangeRateRequest;
use Peso\Services\EuropeanCentralBankService;
$service = new EuropeanCentralBankService();
// {
// value => Decimal("66.161")
// date => Date("2025-06-30")
// }
var_dump(
$service->send(new CurrentExchangeRateRequest('EUR', 'PHP'))
);
// {
// value => Decimal("64.706")
// // there are no rates for Sunday, so the returned rates are from Friday
// // not all services provide the date correctly
// date => Date("2025-06-13")
// }
var_dump(
$service->send(new HistoricalExchangeRateRequest('EUR', 'PHP', Calendar::parse('2025-06-15')))
);
Conversion#
\Peso\Core\Requests\CurrentConversionRequestconverts currencies by current conversion rates.\Peso\Core\Requests\HistoricalConversionRequestconverts currencies a conversion rate on a given date.
Some services support conversion natively (Fixer, for example), other services can be wrapped with the ConversionService:
<?php
use Arokettu\Date\Calendar;
use Peso\Core\Requests\CurrentConversionRequest;
use Peso\Core\Requests\HistoricalConversionRequest;
use Peso\Core\Services\ConversionService;
use Peso\Core\Types\Decimal;
use Peso\Services\EuropeanCentralBankService;
$service = new ConversionService(new EuropeanCentralBankService());
// {
// amount => Decimal("8167.57545")
// date => Date("2025-06-30")
// }
var_dump(
$service->send(new CurrentConversionRequest(new Decimal('123.45'), 'EUR', 'PHP')
));
// {
// amount => Decimal("7987.95570")
// date => Date("2025-06-13")
// }
var_dump(
$service->send(new HistoricalConversionRequest(new Decimal('123.45'), 'EUR', 'PHP', Calendar::parse('2025-06-15'))
));
Data Types#
Specific data types used in this framework:
\Peso\Core\Types\Decimalis a string wrapper over a decimal string that enforces its validity.\Arokettu\Date\Dateis an object that represents a date without time and a time zone, see arokettu/date.