Currency::formatter()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 17

Duplication

Lines 17
Ratio 100 %

Code Coverage

Tests 10
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 17
loc 17
ccs 10
cts 10
cp 1
crap 2
rs 9.7
c 0
b 0
f 0
1
<?php
2
3
namespace Propaganistas\LaravelIntl;
4
5
use CommerceGuys\Intl\Formatter\CurrencyFormatter;
6
use CommerceGuys\Intl\Currency\CurrencyRepository;
7
use CommerceGuys\Intl\NumberFormat\NumberFormatRepository;
8
use Illuminate\Support\Arr;
9
use Propaganistas\LaravelIntl\Concerns\WithLocales;
10
use Propaganistas\LaravelIntl\Contracts\Intl;
11
12
class Currency extends Intl
13
{
14
    use WithLocales;
15
16
    /**
17
     * Loaded localized currency data.
18
     *
19
     * @var array
20
     */
21
    protected $data;
22
23
    /**
24
     * Array of localized currency formatters.
25
     *
26
     * @var array
27
     */
28
    protected $formatters;
29
30
    /**
31
     * Get a localized record by key.
32
     *
33
     * @param string $currencyCode
34
     * @return string
35
     */
36 15
    public function get($currencyCode)
37
    {
38 15
        return $this->data()->get($currencyCode)->getName();
39
    }
40
41
    /**
42
     * Alias of get().
43
     *
44
     * @param string $currencyCode
45
     * @return string
46
     */
47 12
    public function name($currencyCode)
48
    {
49 12
        return $this->get($currencyCode);
50
    }
51
52
    /**
53
     * Get the symbol of the given currency.
54
     *
55
     * @param string $currencyCode
56
     * @return string
57
     */
58 3
    public function symbol($currencyCode)
59
    {
60 3
        return $this->data()->get($currencyCode)->getSymbol();
61
    }
62
63
    /**
64
     * Format a number.
65
     *
66
     * @param string|int|float $number
67
     * @param string $currencyCode
68
     * @param array $options
69
     * @return mixed|string
70
     */
71 12
    public function format($number, $currencyCode, $options = [])
72
    {
73 12
        return $this->formatter()->format($number, $currencyCode,
74 12
            $this->mergeOptions($options)
75
        );
76
    }
77
78
    /**
79
     * Format a number.
80
     *
81
     * @param string|int|float $number
82
     * @param string $currencyCode
83
     * @param array $options
84
     * @return mixed|string
85
     */
86 3
    public function formatAccounting($number, $currencyCode, $options = [])
87
    {
88 3
        return $this->formatter()->format($number, $currencyCode,
89 3
            $this->mergeOptions($options, ['style' => 'accounting'])
90
        );
91
    }
92
93
    /**
94
     * Parse a localized currency string into a number.
95
     *
96
     * @param string $number
97
     * @param string $currencyCode
98
     * @param array $options
99
     * @return mixed|string
100
     */
101 3
    public function parse($number, $currencyCode, $options = [])
102
    {
103 3
        return $this->formatter()->parse($number, $currencyCode,
104 3
            $this->mergeOptions($options)
105
        );
106
    }
107
108
    /**
109
     * Get all localized records.
110
     *
111
     * @return array
112
     */
113 3
    public function all()
114
    {
115 3
        return $this->data()->getList();
116
    }
117
118
    /**
119
     * Get the formatter's key.
120
     *
121
     * @param string $locale
122
     * @param string $fallbackLocale
123
     * @return string
124
     */
125 33
    protected function getLocalesKey($locale, $fallbackLocale)
126
    {
127 33
        return implode('|', [
128 33
            $locale,
129 33
            $fallbackLocale,
130
        ]);
131
    }
132
133
    /**
134
     * The currency repository.
135
     *
136
     * @return \CommerceGuys\Intl\Currency\CurrencyRepository
137
     */
138 33 View Code Duplication
    protected function data()
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...
139
    {
140 33
        $key = $this->getLocalesKey(
141 33
            $locale = $this->getLocale(),
142 33
            $fallbackLocale = $this->getFallbackLocale()
143
        );
144
145 33
        if (! isset($this->data[$key])) {
146 33
            $this->data[$key] = new CurrencyRepository($locale, $fallbackLocale);
147
        }
148
149 33
        return $this->data[$key];
150
    }
151
152
    /**
153
     * The current number formatter.
154
     *
155
     * @return \CommerceGuys\Intl\Formatter\CurrencyFormatter
156
     */
157 18 View Code Duplication
    protected function formatter()
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...
158
    {
159 18
        $key = $this->getLocalesKey(
160 18
            $locale = $this->getLocale(),
161 18
            $fallbackLocale = $this->getFallbackLocale()
162
        );
163
164 18
        if (! isset($this->formatters[$key])) {
165 18
            $this->formatters[$key] = new CurrencyFormatter(
166 18
                new NumberFormatRepository($fallbackLocale),
167 18
                $this->data(),
168 18
                ['locale' => $locale]
169
            );
170
        }
171
172 18
        return $this->formatters[$key];
173
    }
174
175
    /**
176
     * Merges the options array.
177
     *
178
     * @param array $options
179
     * @param array $defaults
180
     * @return array
181
     */
182 18
    protected function mergeOptions(array $options, array $defaults = [])
183
    {
184 18
        Arr::forget($options, 'locale');
185
186 18
        return $defaults + $options;
187
    }
188
}