Skip to content

Instantly share code, notes, and snippets.

@mgsmus
Created September 6, 2019 11:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mgsmus/a46803e01e59847c90da899b607bb651 to your computer and use it in GitHub Desktop.
Save mgsmus/a46803e01e59847c90da899b607bb651 to your computer and use it in GitHub Desktop.
<?php
use Money\Currencies\ISOCurrencies;
use Money\Parser\DecimalMoneyParser;
use Money\Formatter\IntlMoneyFormatter;
use Money\Converter;
use Money\Currency;
use Money\Exchange\FixedExchange;
use Money\Exchange\ReversedCurrenciesExchange;
$currencies = new ISOCurrencies();
$moneyParser = new DecimalMoneyParser($currencies);
$moneyFormatter = new IntlMoneyFormatter(
new \NumberFormatter('tr_TR', \NumberFormatter::CURRENCY),
$currencies
);
$usdMoneyFormatter = new IntlMoneyFormatter(
new \NumberFormatter('en_US', \NumberFormatter::CURRENCY),
$currencies
);
$converter = new Converter($currencies,
new ReversedCurrenciesExchange(
new FixedExchange([
'USD' => [
'TRY' => 5.70
]
])
)
);
$price = $moneyParser->parse('1115.55', new Currency('TRY'));
$tax = $price->multiply(18)->divide(100);
$priceWithTax = $price->add($tax);
$installments = [];
$USD = $converter->convert($priceWithTax, new Currency('USD'));
foreach ($priceWithTax->allocateTo(4) as $installment) {
$installments[] = $moneyFormatter->format($installment);
}
$result = [
'price' => $moneyFormatter->format($price),
'tax' => $moneyFormatter->format($tax),
'priceWithTax' => $moneyFormatter->format($priceWithTax),
'usd' => $usdMoneyFormatter->format($USD),
'installments' => $installments
];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment