1
|
|
|
<?php namespace Propaganistas\LaravelIntl; |
2
|
|
|
|
3
|
|
|
use CommerceGuys\Intl\Currency\CurrencyRepository; |
4
|
|
|
use CommerceGuys\Intl\Formatter\NumberFormatter; |
5
|
|
|
use CommerceGuys\Intl\NumberFormat\NumberFormatRepository; |
6
|
|
|
use Propaganistas\LaravelIntl\Base\Intl; |
7
|
|
|
|
8
|
|
|
class Currency extends Intl |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var \CommerceGuys\Intl\Currency\CurrencyRepository |
12
|
|
|
*/ |
13
|
|
|
protected $data; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var \CommerceGuys\Intl\NumberFormat\NumberFormatRepository |
17
|
|
|
*/ |
18
|
|
|
protected $formatData; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Currency constructor. |
22
|
|
|
* |
23
|
|
|
* @param \CommerceGuys\Intl\Currency\CurrencyRepository $data |
24
|
|
|
* @param \CommerceGuys\Intl\NumberFormat\NumberFormatRepository $formatData |
25
|
|
|
*/ |
26
|
36 |
|
public function __construct(CurrencyRepository $data, NumberFormatRepository $formatData) |
27
|
|
|
{ |
28
|
36 |
|
$this->data = $data; |
29
|
36 |
|
$this->formatData = $formatData; |
30
|
36 |
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Get the localized name for the specified currency. |
34
|
|
|
* |
35
|
|
|
* @param string $currencyCode |
36
|
|
|
* @return string |
37
|
|
|
*/ |
38
|
9 |
|
public function name($currencyCode) |
39
|
|
|
{ |
40
|
9 |
|
return $this->get($currencyCode)->getName(); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Get the symbol for the specified currency. |
45
|
|
|
* |
46
|
|
|
* @param string $currencyCode |
47
|
|
|
* @return string |
48
|
|
|
*/ |
49
|
3 |
|
public function symbol($currencyCode) |
50
|
|
|
{ |
51
|
3 |
|
return $this->get($currencyCode)->getSymbol(); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Format an value for the given currency. |
56
|
|
|
* |
57
|
|
|
* @param int|float $value |
58
|
|
|
* @param string $currencyCode |
59
|
|
|
* @return string |
60
|
|
|
*/ |
61
|
12 |
View Code Duplication |
public function format($value, $currencyCode) |
|
|
|
|
62
|
|
|
{ |
63
|
12 |
|
$currency = $this->get($currencyCode); |
64
|
12 |
|
$format = $this->formatData->get(null); |
65
|
12 |
|
$formatter = new NumberFormatter($format, NumberFormatter::CURRENCY); |
66
|
|
|
|
67
|
12 |
|
return $formatter->formatCurrency($value, $currency); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Format an value for the given currency in accounting |
72
|
|
|
* format. |
73
|
|
|
* |
74
|
|
|
* @param int|float $value |
75
|
|
|
* @param string $currencyCode |
76
|
|
|
* @return string |
77
|
|
|
*/ |
78
|
3 |
View Code Duplication |
public function formatAccounting($value, $currencyCode) |
|
|
|
|
79
|
|
|
{ |
80
|
3 |
|
$currency = $this->get($currencyCode); |
81
|
3 |
|
$format = $this->formatData->get(null); |
82
|
3 |
|
$formatter = new NumberFormatter($format, NumberFormatter::CURRENCY_ACCOUNTING); |
83
|
|
|
|
84
|
3 |
|
return $formatter->formatCurrency($value, $currency); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Parse a localized value into native PHP format. |
89
|
|
|
* |
90
|
|
|
* @param string|int|float $value |
91
|
|
|
* @param string $currencyCode |
92
|
|
|
* @return int|float |
93
|
|
|
*/ |
94
|
3 |
View Code Duplication |
public function parse($value, $currencyCode) |
|
|
|
|
95
|
|
|
{ |
96
|
3 |
|
$currency = $this->get($currencyCode); |
97
|
3 |
|
$format = $this->formatData->get(null); |
98
|
3 |
|
$formatter = new NumberFormatter($format, NumberFormatter::CURRENCY); |
99
|
|
|
|
100
|
3 |
|
return $formatter->parseCurrency($value, $currency); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Set the default locale. |
105
|
|
|
* |
106
|
|
|
* @param $locale |
107
|
|
|
* @return $this |
108
|
|
|
*/ |
109
|
12 |
|
public function setLocale($locale) |
110
|
|
|
{ |
111
|
12 |
|
parent::setLocale($locale); |
112
|
|
|
|
113
|
12 |
|
$this->formatData->setDefaultLocale($locale); |
114
|
|
|
|
115
|
12 |
|
return $this; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Set the desired locale. |
120
|
|
|
* |
121
|
|
|
* @param $locale |
122
|
|
|
* @return $this |
123
|
|
|
*/ |
124
|
3 |
|
public function setFallbackLocale($locale) |
125
|
|
|
{ |
126
|
3 |
|
parent::setFallbackLocale($locale); |
127
|
|
|
|
128
|
3 |
|
$this->formatData->setFallbackLocale($locale); |
129
|
|
|
|
130
|
3 |
|
return $this; |
131
|
|
|
} |
132
|
|
|
} |
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.