LocalizedMoneyExtension   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 3
dl 0
loc 92
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getFunctions() 0 7 1
A getLocalizedMoney() 0 22 3
A getCurrencySign() 0 19 3
A getName() 0 4 1
1
<?php
2
3
namespace Admingenerator\GeneratorBundle\Twig\Extension;
4
5
use Symfony\Component\Form\Extension\Core\DataTransformer\MoneyToLocalizedStringTransformer;
6
7
/**
8
 * @author Piotr Gołębiewski <[email protected]>
9
 */
10
class LocalizedMoneyExtension extends \Twig_Extension
0 ignored issues
show
Deprecated Code introduced by
The class Twig_Extension has been deprecated with message: since Twig 2.7, use "Twig\Extension\AbstractExtension" instead

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
11
{
12
    public function getFunctions()
13
    {
14
        return array(
15
            'localized_money' => new \Twig_SimpleFunction('localized_money', array($this, 'getLocalizedMoney')),
0 ignored issues
show
Deprecated Code introduced by
The class Twig_SimpleFunction has been deprecated with message: since Twig 2.7, use "Twig\TwigFunction" instead

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
16
            'currency_sign'   => new \Twig_SimpleFunction('currency_sign', array($this, 'getCurrencySign')),
0 ignored issues
show
Deprecated Code introduced by
The class Twig_SimpleFunction has been deprecated with message: since Twig 2.7, use "Twig\TwigFunction" instead

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
17
        );
18
    }
19
20
    /**
21
     * @param float $value Money amount.
22
     *
23
     * @param string $currency This can be any 3 letter ISO 4217 code. You
24
     * can also set this to false to hide the currency symbol.
25
     *
26
     * @param integer $precision For some reason, if you need some precision
27
     * other than 2 decimal places, you can modify this value. You probably
28
     * won't need to do this unless, for example, you want to round to the
29
     * nearest dollar (set the precision to 0).
30
     *
31
     * @param string $grouping This value is used internally as the
32
     * NumberFormatter::GROUPING_USED value when using PHP's NumberFormatter
33
     * class. Its documentation is non-existent, but it appears that if you set
34
     * this to true, numbers will be grouped with a comma or period (depending
35
     * on your locale): 12345.123 would display as 12,345.123.
36
     *
37
     * @param integer $divisor If, for some reason, you need to divide your
38
     * starting value by a number before rendering it to the user, you can use
39
     * the divisor option.
40
     *
41
     * @return string Localized money
42
     */
43
    public function getLocalizedMoney($value, $currency = 'EUR', $precision = 2, $grouping = true, $divisor = 1)
44
    {
45
        $locale = \Locale::getDefault();
46
47
        $format = new \NumberFormatter($locale, \NumberFormatter::CURRENCY);
48
        $pattern = $format->formatCurrency('123', $currency);
49
50
        $dt = new MoneyToLocalizedStringTransformer($precision, $grouping, null, $divisor);
0 ignored issues
show
Bug introduced by
It seems like $grouping defined by parameter $grouping on line 43 can also be of type string; however, Symfony\Component\Form\E...nsformer::__construct() does only seem to accept boolean|null, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
51
        $transformed_value = $dt->transform($value);
52
53
        preg_match('/^([^\s\xc2\xa0]*)[\s\xc2\xa0]*123(?:[,.]0+)?[\s\xc2\xa0]*([^\s\xc2\xa0]*)$/u', $pattern, $matches);
54
55
        if (!empty($matches[1])) {
56
            $localized_money = $matches[1].' '.$transformed_value;
57
        } elseif (!empty($matches[2])) {
58
            $localized_money = $transformed_value.' '.$matches[2];
59
        } else {
60
            $localized_money = $transformed_value;
61
        }
62
63
        return $localized_money;
64
    }
65
66
    /**
67
     * @param string $currency This can be any 3 letter ISO 4217 code. You
68
     * can also set this to false to return the general currency symbol.
69
     *
70
     * @return string Currency sign
71
     */
72
    public function getCurrencySign($currency = false)
73
    {
74
        $locale = \Locale::getDefault();
75
76
        $format = new \NumberFormatter($locale, \NumberFormatter::CURRENCY);
77
        $pattern = $format->formatCurrency('123', $currency);
78
79
        preg_match('/^([^\s\xc2\xa0]*)[\s\xc2\xa0]*123(?:[,.]0+)?[\s\xc2\xa0]*([^\s\xc2\xa0]*)$/u', $pattern, $matches);
80
81
        if (!empty($matches[1])) {
82
            $currency_sign = $matches[1];
83
        } elseif (!empty($matches[2])) {
84
            $currency_sign = $matches[2];
85
        } else {
86
            $currency_sign = '¤';
87
        }
88
89
        return $currency_sign;
90
    }
91
92
    /**
93
     * Returns the name of the extension.
94
     *
95
     * @return string The extension name
96
     */
97
    public function getName()
98
    {
99
        return 'localized_money';
100
    }
101
}
102