MoneyFormatter::formatCurrencyAsName()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Ivoba\Money\Formatter;
4
5
use Money\Currencies\ISOCurrencies;
6
use Money\Formatter\IntlMoneyFormatter;
7
use Money\Money;
8
use Symfony\Component\Intl\Intl;
9
10
class MoneyFormatter
11
{
12
    /**
13
     * @var
14
     */
15
    private $locale;
16
17
    /**
18
     * @param null|string $locale
19
     */
20 18
    public function __construct($locale = null)
21
    {
22 18
        $this->locale = $locale;
23 18
        if ($this->locale === null) {
24 6
            $this->locale = \Locale::getDefault();
25 2
        }
26 18
    }
27
28
    /**
29
     * localized format for money with the NumberFormatter class in Decimal mode
30
     * without currency symbol
31
     * like 1.234,56
32
     *
33
     * @param Money $money
34
     * @param null  $locale
35
     * @return bool|string
36
     */
37 3 View Code Duplication
    public function formatI18nDecimal(Money $money, $locale = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
38
    {
39 3
        if ($locale === null) {
40
            $locale = $this->locale;
41
        }
42
43 3
        $numberFormatter = new \NumberFormatter($locale, \NumberFormatter::DECIMAL);
44 3
        $moneyFormatter = new IntlMoneyFormatter($numberFormatter, new ISOCurrencies());
45
46 3
        return $moneyFormatter->format($money);
47
    }
48
49
    /**
50
     * localized format for money with the NumberFormatter class in Currency mode
51
     * with currency symbol
52
     * like €1.234,56
53
     *
54
     * @param Money $money
55
     * @param null  $locale
56
     * @return bool|string
57
     */
58 9 View Code Duplication
    public function formatI18n(Money $money, $locale = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
59
    {
60 9
        if ($locale === null) {
61 3
            $locale = $this->locale;
62 1
        }
63
64 9
        $numberFormatter = new \NumberFormatter($locale, \NumberFormatter::CURRENCY);
65 9
        $moneyFormatter = new IntlMoneyFormatter($numberFormatter, new ISOCurrencies());
66
67 9
        return $moneyFormatter->format($money);
68
    }
69
70
    /**
71
     * gets currency symbol
72
     *
73
     * @param Money $money
74
     * @return null|string
75
     */
76 6
    public function formatCurrencyAsSymbol(Money $money)
77
    {
78 6
        return Intl::getCurrencyBundle()->getCurrencySymbol($money->getCurrency()->getCode());
79
    }
80
81
    /**
82
     * gets currency name
83
     *
84
     * @param Money $money
85
     * @return null|string
86
     */
87 6
    public function formatCurrencyAsName(Money $money)
88
    {
89 6
        return Intl::getCurrencyBundle()->getCurrencyName($money->getCurrency()->getCode());
90
    }
91
}
92