-1

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?

Brieuc
  • 3,994
  • 9
  • 37
  • 70
  • Check again. as i mentioned i'm using the component. But then if i'm getting the currency bundle that is where i did a mistake. All i need to know is how can i use the one from the component then. https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Intl/NumberFormatter/NumberFormatter.php – Brieuc May 16 '16 at 11:03

1 Answers1

1

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';
    }
}
jkucharovic
  • 4,214
  • 1
  • 31
  • 46