Completed
Pull Request — master (#18)
by Propa
09:42
created

Currency::formatter()   A

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 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
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\Contracts\Intl;
10
11
class Currency extends Intl
12
{
13
    /**
14
     * Loaded localized currency data.
15
     *
16
     * @var array
17
     */
18
    protected $data;
19
20
    /**
21
     * Array of localized currency formatters.
22
     *
23
     * @var array
24
     */
25
    protected $formatters;
26
27
    /**
28
     * The current locale.
29
     *
30
     * @var string $locale
31
     */
32
    protected $locale;
33
34
    /**
35
     * The current locale.
36
     *
37
     * @var string $locale
38
     */
39
    protected $fallbackLocale;
40
41
    /**
42
     * Get a localized record by key.
43
     *
44
     * @param string $currencyCode
45
     * @return string
46
     */
47 15
    public function get($currencyCode)
48
    {
49 15
        return $this->data()->get($currencyCode)->getName();
50
    }
51
52
    /**
53
     * Alias of get().
54
     *
55
     * @param string $currencyCode
56
     * @return string
57
     */
58 12
    public function name($currencyCode)
59
    {
60 12
        return $this->get($currencyCode);
61
    }
62
63
    /**
64
     * Get the symbol of the given currency.
65
     *
66
     * @param string $currencyCode
67
     * @return string
68
     */
69 3
    public function symbol($currencyCode)
70
    {
71 3
        return $this->data()->get($currencyCode)->getSymbol();
72
    }
73
74
    /**
75
     * Format a number.
76
     *
77
     * @param string|int|float $number
78
     * @param string $currencyCode
79
     * @param array $options
80
     * @return mixed|string
81
     */
82 12
    public function format($number, $currencyCode, $options = [])
83
    {
84 12
        return $this->formatter()->format($number, $currencyCode,
85 12
            $this->mergeOptions($options)
86
        );
87
    }
88
89
    /**
90
     * Format a number.
91
     *
92
     * @param string|int|float $number
93
     * @param string $currencyCode
94
     * @param array $options
95
     * @return mixed|string
96
     */
97 3
    public function formatAccounting($number, $currencyCode, $options = [])
98
    {
99 3
        return $this->formatter()->format($number, $currencyCode,
100 3
            $this->mergeOptions($options, ['style' => 'accounting'])
101
        );
102
    }
103
104
    /**
105
     * Parse a localized currency string into a number.
106
     *
107
     * @param string $number
108
     * @param string $currencyCode
109
     * @param array $options
110
     * @return mixed|string
111
     */
112 3
    public function parse($number, $currencyCode, $options = [])
113
    {
114 3
        return $this->formatter()->parse($number, $currencyCode,
115 3
            $this->mergeOptions($options)
116
        );
117
    }
118
119
    /**
120
     * Get all localized records.
121
     *
122
     * @return array
123
     */
124 3
    public function all()
125
    {
126 3
        return $this->data()->getList();
127
    }
128
129
    /**
130
     * Get the current locale.
131
     *
132
     * @return string
133
     */
134 36
    public function getLocale()
135
    {
136 36
        return $this->locale;
137
    }
138
139
    /**
140
     * Set the current locale.
141
     *
142
     * @param $locale
143
     * @return $this
144
     */
145 36
    public function setLocale($locale)
146
    {
147 36
        $this->locale = $locale;
148
149 36
        $this->load($locale, $this->getFallbackLocale());
150
151 36
        return $this;
152
    }
153
154
    /**
155
     * Get the fallback locale.
156
     *
157
     * @return string
158
     */
159 36
    public function getFallbackLocale()
160
    {
161 36
        return $this->fallbackLocale;
162
    }
163
164
    /**
165
     * Set the fallback locale.
166
     *
167
     * @param $locale
168
     * @return $this
169
     */
170 36
    public function setFallbackLocale($locale)
171
    {
172 36
        $this->fallbackLocale = $locale;
173
174 36
        $this->load($this->getLocale(), $locale);
175
176 36
        return $this;
177
    }
178
179
    /**
180
     * Load the format repository for the given locale.
181
     *
182
     * @param string $locale
183
     * @return void
184
     */
185 36
    protected function load($locale, $fallbackLocale)
186
    {
187 36
        $key = $this->getLocalesKey($locale, $fallbackLocale);
188
189 36
        if (! isset($this->data[$key])) {
190 36
            $this->data[$key] = new CurrencyRepository($locale, $fallbackLocale);
191
        }
192
193 36 View Code Duplication
        if (! isset($this->formatters[$key])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
194 36
            $this->formatters[$key] = new CurrencyFormatter(
195 36
                new NumberFormatRepository($fallbackLocale),
196 36
                $this->data[$key],
197 36
                ['locale' => $locale]
198
            );
199
        }
200 36
    }
201
202
    /**
203
     * Get the formatter's key.
204
     *
205
     * @param string|null $locale
206
     * @param string|null $fallbackLocale
207
     * @return string
208
     */
209 36 View Code Duplication
    protected function getLocalesKey($locale = null, $fallbackLocale = 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...
210
    {
211 36
        return implode('|', [
212 36
            $locale ?: $this->getLocale(),
213 36
            $fallbackLocale ?: $this->getFallbackLocale(),
214
        ]);
215
    }
216
217
    /**
218
     * The current number formatter.
219
     *
220
     * @return \CommerceGuys\Intl\Currency\CurrencyRepository
221
     */
222 21
    protected function data()
223
    {
224 21
        return $this->data[$this->getLocalesKey()];
225
    }
226
227
    /**
228
     * The current number formatter.
229
     *
230
     * @return \CommerceGuys\Intl\Formatter\CurrencyFormatter
231
     */
232 18
    protected function formatter()
233
    {
234 18
        return $this->formatters[$this->getLocalesKey()];
235
    }
236
237
    /**
238
     * Merges the options array.
239
     *
240
     * @param array $options
241
     * @param array $defaults
242
     * @return array
243
     */
244 18
    protected function mergeOptions(array $options, array $defaults = [])
245
    {
246 18
        Arr::forget($options, 'locale');
247
248 18
        return $defaults + $options;
249
    }
250
}