Completed
Pull Request — master (#13)
by Alexandre
13:17 queued 11:14
created

PriceExtension::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace AppBundle\Price\Twig;
4
5
use AppBundle\Price\Price;
6
7
class PriceExtension extends \Twig_Extension
8
{
9
    private $numberFormatter;
10
11
    public function __construct($locale = 'fr_FR', $style = \NumberFormatter::CURRENCY)
12
    {
13
        $this->numberFormatter = new \NumberFormatter($locale, $style);
14
    }
15
16
    public function getFilters()
17
    {
18
        return array(
19
            new \Twig_SimpleFilter('format_currency', array($this, 'formatCurrency'))
20
        );
21
    }
22
23
    public function formatCurrency($price)
24
    {
25
        if (is_array($price)) {
26
            $price = new Price($price[0], $price[1]);
27
        }
28
29
        $amount = $price->getAmount();
30
        $currency = $price->getCurrency();
31
32
        return $this->numberFormatter->formatCurrency($amount, $currency);
33
    }
34
}
35