Brieuc Brieuc - 6 months ago 33
PHP Question

Sf2 : Intl component, formatCurrency

I'm using in a twig extension the intl component to get the symbol of the currency.

Pretty simple as it is well explained here.

But what i would like to do is to format the price based on the currency / local.

There is indeed a method formatCurrency in the intl component (NumberFormatter class)

<?php
namespace SE\AppBundle\Twig;

use Doctrine\ORM\EntityManager;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Intl\Intl;

class PriceExtension extends \Twig_Extension
{

private $em;
private $requestStack;

public function __construct(EntityManager $em, RequestStack $requestStack)
{
$this->em = $em;
$this->requestStack = $requestStack;
}

public function getFilters()
{
return array(
new \Twig_SimpleFilter('price', array($this, 'priceFilter')),
);
}

public function priceFilter($price)
{

$request = $this->requestStack->getCurrentRequest();
$currency_code = $request->cookies->get('currency');

$exchange_rate = $this->em->getRepository('ApiBundle:ExchangeRates')->findOneBy(array('code' => $currency_code));

$price = $price*$exchange_rate->getRate();

// Get the currency symbol
// $symbol = Intl::getCurrencyBundle()->getCurrencySymbol($currency_code);
// $price = $symbol.$price;

// Undefined formatCurrency method
$price = Intl::getCurrencyBundle()->formatCurrency($price, $currency_code);

return $price;

}

public function getName()
{
return 'price_extension';
}
}


How could i be able to use the formatCurrency method?

Answer

Whole Intl Component is just replacement layer for cases where you don't have installed intl extension.

So your code should look like:

<?php
namespace SE\AppBundle\Twig;

use Doctrine\ORM\EntityManager;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Intl\NumberFormatter\NumberFormatter;

class PriceExtension extends \Twig_Extension
{

    private $em;
    private $requestStack;

    public function __construct(EntityManager $em, RequestStack $requestStack)
    {
        $this->em = $em;
        $this->requestStack = $requestStack;
    }

    public function getFilters()
    {
        return array(
            new \Twig_SimpleFilter('price', array($this, 'priceFilter')),
        );
    }

    public function priceFilter($price)
    {

        $request = $this->requestStack->getCurrentRequest();
        $currency_code = $request->cookies->get('currency');

        $exchange_rate = $this->em->getRepository('ApiBundle:ExchangeRates')->findOneBy([
            'code' => $currency_code
        ]);

        $price = $price*$exchange_rate->getRate();

        if(false === extension_loaded('intl')) {
            $formatter = new NumberFormatter('en', NumberFormatter::CURRENCY);
        } else {
            $formatter = new \NumberFormatter(
                $request->getLocale(),
                \NumberFormatter::CURRENCY
            );
        }

        return $formatter->formatCurrency($price, $currency_code);
    }

    public function getName()
    {
        return 'price_extension';
    }
}