1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
|
<?php
namespace CommerceGuys\Intl\Formatter;
use CommerceGuys\Intl\Currency\Currency;
use CommerceGuys\Intl\Currency\CurrencyRepositoryInterface;
use CommerceGuys\Intl\Exception\InvalidArgumentException;
use CommerceGuys\Intl\Exception\UnknownCurrencyException;
use CommerceGuys\Intl\NumberFormat\NumberFormat;
use CommerceGuys\Intl\NumberFormat\NumberFormatRepositoryInterface;
/**
* Formats currency amounts using locale-specific patterns.
*/
class CurrencyFormatter implements CurrencyFormatterInterface
{
use FormatterTrait;
/**
* The number format repository.
*
* @var NumberFormatRepositoryInterface
*/
protected $numberFormatRepository;
/**
* The currency repository.
*
* @var CurrencyRepositoryInterface
*/
protected $currencyRepository;
/**
* The default locale.
*
* @var string
*/
protected $defaultLocale;
/**
* The loaded number formats.
*
* @var NumberFormat[]
*/
protected $numberFormats = [];
/**
* The loaded currencies.
*
* @var Currency[]
*/
protected $currencies = [];
/**
* The default options.
*
* @var array
*/
protected $defaultOptions = [
'locale' => 'en',
'use_grouping' => true,
'minimum_fraction_digits' => null,
'maximum_fraction_digits' => null,
'rounding_mode' => PHP_ROUND_HALF_UP,
'style' => 'standard',
'currency_display' => 'symbol',
];
/**
* Creates a CurrencyFormatter instance.
*
* @param NumberFormatRepositoryInterface $numberFormatRepository The number format repository.
* @param CurrencyRepositoryInterface $currencyRepository The currency repository.
* @param array $defaultOptions The default options.
*
* @throws \RuntimeException
*/
public function __construct(NumberFormatRepositoryInterface $numberFormatRepository, CurrencyRepositoryInterface $currencyRepository, array $defaultOptions = [])
{
if (!extension_loaded('bcmath')) {
throw new \RuntimeException('The bcmath extension is required by CurrencyFormatter.');
}
$this->validateOptions($defaultOptions);
$this->numberFormatRepository = $numberFormatRepository;
$this->currencyRepository = $currencyRepository;
$this->defaultOptions = array_replace($this->defaultOptions, $defaultOptions);
}
/**
* {@inheritdoc}
*/
public function format($number, $currencyCode, array $options = [])
{
if (!is_numeric($number)) {
$message = sprintf('The provided value "%s" is not a valid number or numeric string.', $number);
throw new InvalidArgumentException($message);
}
$this->validateOptions($options);
$options = array_replace($this->defaultOptions, $options);
$numberFormat = $this->getNumberFormat($options['locale']);
$currency = $this->getCurrency($currencyCode, $options['locale']);
// Use the currency defaults if the values weren't set by the caller.
if (!isset($options['minimum_fraction_digits'])) {
$options['minimum_fraction_digits'] = $currency->getFractionDigits();
}
if (!isset($options['maximum_fraction_digits'])) {
$options['maximum_fraction_digits'] = $currency->getFractionDigits();
}
$number = (string) $number;
$number = $this->formatNumber($number, $numberFormat, $options);
if ($options['currency_display'] == 'symbol') {
$number = str_replace('¤', $currency->getSymbol(), $number);
} elseif ($options['currency_display'] == 'code') {
$number = str_replace('¤', $currency->getCurrencyCode(), $number);
} else {
// No symbol should be displayed. Remove leftover whitespace.
$number = str_replace('¤', '', $number);
$number = trim($number, " \xC2\xA0");
}
return $number;
}
/**
* {@inheritdoc}
*/
public function parse($number, $currencyCode, array $options = [])
{
$this->validateOptions($options);
$options = array_replace($this->defaultOptions, $options);
$numberFormat = $this->getNumberFormat($options['locale']);
$currency = $this->getCurrency($currencyCode, $options['locale']);
$replacements = [
// Strip the currency code or symbol.
$currency->getCurrencyCode() => '',
$currency->getSymbol() => '',
];
$number = strtr($number, $replacements);
$number = $this->parseNumber($number, $numberFormat);
return $number;
}
/**
* Gets the number format for the provided locale.
*
* @param string $locale The locale.
*
* @return NumberFormat
*/
protected function getNumberFormat($locale)
{
if (!isset($this->numberFormats[$locale])) {
$this->numberFormats[$locale] = $this->numberFormatRepository->get($locale);
}
return $this->numberFormats[$locale];
}
/**
* Gets the currency for the provided currency code and locale.
*
* @param string $currencyCode The currency code.
* @param string $locale The locale.
*
* @return Currency
*/
protected function getCurrency($currencyCode, $locale)
{
if (!isset($this->currencies[$currencyCode][$locale])) {
try {
$currency = $this->currencyRepository->get($currencyCode, $locale);
} catch (UnknownCurrencyException $e) {
// The requested currency was not found. Fall back
// to a dummy object to show just the currency code.
$currency = new Currency([
'currency_code' => $currencyCode,
'name' => $currencyCode,
'numeric_code' => '000',
'locale' => $locale,
]);
}
$this->currencies[$currencyCode][$locale] = $currency;
}
return $this->currencies[$currencyCode][$locale];
}
/**
* {@inheritdoc}
*/
protected function getAvailablePatterns(NumberFormat $numberFormat)
{
return [
'standard' => $numberFormat->getCurrencyPattern(),
'accounting' => $numberFormat->getAccountingCurrencyPattern(),
];
}
/**
* Validates the provided options.
*
* Ensures the absence of unknown keys, correct data types and values.
*
* @param array $options The options.
*
* @throws \InvalidArgumentException
*/
protected function validateOptions(array $options)
{
foreach ($options as $option => $value) {
if (!array_key_exists($option, $this->defaultOptions)) {
throw new InvalidArgumentException(sprintf('Unrecognized option "%s".', $option));
}
}
if (isset($options['use_grouping']) && !is_bool($options['use_grouping'])) {
throw new InvalidArgumentException('The option "use_grouping" must be a boolean.');
}
foreach (['minimum_fraction_digits', 'maximum_fraction_digits'] as $option) {
if (array_key_exists($option, $options) && !is_numeric($options[$option])) {
throw new InvalidArgumentException(sprintf('The option "%s" must be numeric.', $option));
}
}
$roundingModes = [
PHP_ROUND_HALF_UP, PHP_ROUND_HALF_DOWN,
PHP_ROUND_HALF_EVEN, PHP_ROUND_HALF_ODD, 'none',
];
if (!empty($options['rounding_mode']) && !in_array($options['rounding_mode'], $roundingModes)) {
throw new InvalidArgumentException(sprintf('Unrecognized rounding mode "%s".', $options['rounding_mode']));
}
if (!empty($options['style']) && !in_array($options['style'], ['standard', 'accounting'])) {
throw new InvalidArgumentException(sprintf('Unrecognized style "%s".', $options['style']));
}
if (!empty($options['currency_display']) && !in_array($options['currency_display'], ['code', 'symbol', 'none'])) {
throw new InvalidArgumentException(sprintf('Unrecognized currency display "%s".', $options['currency_display']));
}
}
}
|