1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
<?php
/**
* @author @jenschude <jens.schulze@commercetools.de>
*/
namespace Commercetools\Core\Helper;
use Commercetools\Core\Model\Common\Context;
/**
* Formats a given currency for display. As default the intl extensions capabilities are used for formatting.
* Given the locale of the context and the currency, the amount will be formatted with intl NumberFormatter.
* The formatter reads the fraction digits from the formatter for the given currency and locale. This information
* is used to calculate the currency value from the centAmount
*
* Example:
* $centAmount = 123456;
* $currency = 'JPY';
* $str = $this->format($centAmount, $currency); // '¥123,456'
* $currency = 'USD';
* $str = $this->format($centAmount, $currency); // '$1,234.56'
* $currency = 'EUR';
* $str = $this->format($centAmount, $currency); // '1.234,56 €'
* @package Commercetools\Core\Helper
*/
class CurrencyFormatter implements CurrencyFormatterInterface
{
protected $context;
protected $formatCallback;
public function __construct(Context $context)
{
$this->context = $context;
}
/**
* @param $centAmount
* @param $currency
* @return string
*/
protected function defaultFormat($centAmount, $currency)
{
$locale = $this->context->getLocale();
$formatter = new \NumberFormatter($locale, \NumberFormatter::CURRENCY);
$fractionUnits = pow(10, $formatter->getAttribute(\NumberFormatter::FRACTION_DIGITS));
$amount = $centAmount / $fractionUnits;
$currency = strtoupper($currency);
return $formatter->formatCurrency($amount, $currency);
}
/**
* @param callable $formatCallback
*/
public function setFormatCallback(callable $formatCallback)
{
$this->formatCallback = $formatCallback;
}
/**
* @param int $centAmount
* @param string $currency
* @return string
*/
public function format($centAmount, $currency)
{
if (is_null($this->formatCallback)) {
return $this->defaultFormat($centAmount, $currency);
}
return call_user_func_array($this->formatCallback, [$centAmount, $currency]);
}
}