[Money] Refactoring Money twig extensions

This commit is contained in:
Łukasz Chruściel 2016-04-15 10:05:26 +02:00
parent 04eefef2ed
commit 3fbcb4b778
5 changed files with 47 additions and 71 deletions

View file

@ -24,6 +24,7 @@
<service id="sylius.templating.helper.money" class="%sylius.templating.helper.money.class%" lazy="true">
<argument>%sylius.money.locale%</argument>
<argument>%sylius.money.currency%</argument>
<argument type="service" id="sylius.formatter.money" />
<tag name="templating.helper" alias="sylius_money" />
</service>
</services>

View file

@ -11,52 +11,47 @@
namespace Sylius\Bundle\MoneyBundle\Templating\Helper;
use Sylius\Bundle\MoneyBundle\Formatter\MoneyFormatterInterface;
use Symfony\Component\Templating\Helper\Helper;
class MoneyHelper extends Helper implements MoneyHelperInterface
{
/**
* The default currency.
*
* @var string
*/
private $currency;
private $defaultCurrency;
/**
* @var string
*/
private $locale;
private $defaultLocale;
/**
* @param string $locale The locale used to format money.
* @param string $currency The default currency.
* @var MoneyFormatterInterface
*/
public function __construct($locale, $currency = null)
private $moneyFormatter;
/**
* @param string $defaultCurrency
* @param string $defaultLocale
* @param MoneyFormatterInterface $moneyFormatter
*/
public function __construct($defaultCurrency, $defaultLocale, MoneyFormatterInterface $moneyFormatter)
{
$this->locale = $locale ?: \Locale::getDefault();
$this->currency = $currency;
$this->defaultCurrency = $defaultCurrency;
$this->defaultLocale = $defaultLocale;
$this->moneyFormatter = $moneyFormatter;
}
/**
* {@inheritdoc}
*/
public function formatAmount($amount, $currency = null, $decimal = false, $locale = null)
public function formatAmount($amount, $currency = null, $locale = null)
{
$locale = $locale ?: $this->getDefaultLocale();
$currency = $currency ?: $this->getDefaultCurrency();
$locale = $locale ?: $this->defaultLocale;
$currency = $currency ?: $this->defaultCurrency;
if ($decimal) {
$formatter = new \NumberFormatter($locale, \NumberFormatter::DECIMAL);
} else {
$formatter = new \NumberFormatter($locale, \NumberFormatter::CURRENCY);
}
$result = $formatter->formatCurrency($amount / 100, $currency);
if (false === $result) {
throw new \InvalidArgumentException(sprintf('The amount "%s" of type %s cannot be formatted to currency "%s".', $amount, gettype($amount), $currency));
}
return $result;
return $this->moneyFormatter->format($amount, $currency, $locale);
}
/**
@ -66,24 +61,4 @@ class MoneyHelper extends Helper implements MoneyHelperInterface
{
return 'sylius_money';
}
/**
* Get the default currency if none is provided as argument.
*
* @return string The currency code
*/
protected function getDefaultCurrency()
{
return $this->currency;
}
/**
* Get the default locale if none is provided as argument.
*
* @return string The locale code
*/
protected function getDefaultLocale()
{
return $this->locale;
}
}

View file

@ -19,12 +19,11 @@ interface MoneyHelperInterface
/**
* @param int $amount
* @param string|null $currency
* @param bool $decimal
* @param string|null $locale
*
* @return string
*
* @throws \InvalidArgumentException
*/
public function formatAmount($amount, $currency = null, $decimal = false, $locale = null);
public function formatAmount($amount, $currency = null, $locale = null);
}

View file

@ -39,23 +39,10 @@ class MoneyExtension extends \Twig_Extension
public function getFilters()
{
return [
new \Twig_SimpleFilter('sylius_money', [$this, 'formatAmount']),
new \Twig_SimpleFilter('sylius_money', [$this->helper, 'formatAmount']),
];
}
/**
* Format the money amount to nice display form.
*
* @param int $amount
* @param string|null $currency
*
* @return string
*/
public function formatAmount($amount, $currency = null, $locale = null)
{
return $this->helper->formatAmount($amount, $currency, false, $locale);
}
/**
* {@inheritdoc}
*/

View file

@ -12,8 +12,9 @@
namespace spec\Sylius\Bundle\MoneyBundle\Templating\Helper;
use PhpSpec\ObjectBehavior;
use Sylius\Bundle\MoneyBundle\Formatter\AmountFormatterInterface;
use Sylius\Bundle\MoneyBundle\Formatter\MoneyFormatterInterface;
use Sylius\Bundle\MoneyBundle\Templating\Helper\MoneyHelper;
use Sylius\Bundle\MoneyBundle\Templating\Helper\MoneyHelperInterface;
use Symfony\Component\Templating\Helper\Helper;
/**
@ -24,9 +25,9 @@ use Symfony\Component\Templating\Helper\Helper;
*/
class MoneyHelperSpec extends ObjectBehavior
{
function let(AmountFormatterInterface $amountFormatter)
function let(MoneyFormatterInterface $moneyFormatter)
{
$this->beConstructedWith('en', 'EUR', $amountFormatter);
$this->beConstructedWith('EUR', 'en_US', $moneyFormatter);
}
function it_is_initializable()
@ -39,16 +40,29 @@ class MoneyHelperSpec extends ObjectBehavior
$this->shouldHaveType(Helper::class);
}
function it_allows_to_format_money_in_different_currencies(AmountFormatterInterface $amountFormatter)
function it_implements_money_helper_interface()
{
$amountFormatter->format(15, 'en', 'USD')->willReturn('$0.15');
$amountFormatter->format(2500, 'en', 'USD')->willReturn('$25.00');
$amountFormatter->format(312, 'en', 'EUR')->willReturn('€3.12');
$amountFormatter->format(500, 'en', 'EUR')->willReturn('€5.00');
$this->shouldImplement(MoneyHelperInterface::class);
}
function it_formats_money_using_default_currency_and_locale_if_only_amount_is_given(MoneyFormatterInterface $moneyFormatter)
{
$moneyFormatter->format(500, 'EUR', 'en_US')->willReturn('€5.00');
$this->formatAmount(15, 'USD')->shouldReturn('$0.15');
$this->formatAmount(2500, 'USD')->shouldReturn('$25.00');
$this->formatAmount(312, 'EUR')->shouldReturn('€3.12');
$this->formatAmount(500)->shouldReturn('€5.00');
}
function it_formats_money_using_default_locale_if_not_given(MoneyFormatterInterface $moneyFormatter)
{
$moneyFormatter->format(312, 'EUR', 'en_US')->willReturn('€3.12');
$this->formatAmount(312, 'EUR')->shouldReturn('€3.12');
}
function it_formats_money_using_given_currency_and_locale(MoneyFormatterInterface $moneyFormatter)
{
$moneyFormatter->format(2500, 'USD', 'fr_FR')->willReturn('$25.00');
$this->formatAmount(2500, 'USD', 'fr_FR')->shouldReturn('$25.00');
}
}