Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
9 / 9 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
CurrencyFormatter | |
100.00% |
9 / 9 |
|
100.00% |
2 / 2 |
6 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
4 | |||
convertToDisplayFormat | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 |
1 | <?php |
2 | |
3 | /** |
4 | * Currency formatter |
5 | * |
6 | * PHP version 8 |
7 | * |
8 | * Copyright (C) Villanova University 2010. |
9 | * |
10 | * This program is free software; you can redistribute it and/or modify |
11 | * it under the terms of the GNU General Public License version 2, |
12 | * as published by the Free Software Foundation. |
13 | * |
14 | * This program is distributed in the hope that it will be useful, |
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17 | * GNU General Public License for more details. |
18 | * |
19 | * You should have received a copy of the GNU General Public License |
20 | * along with this program; if not, write to the Free Software |
21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
22 | * |
23 | * @category VuFind |
24 | * @package Service |
25 | * @author Demian Katz <demian.katz@villanova.edu> |
26 | * @author Ere Maijala <ere.maijala@helsinki.fi> |
27 | * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License |
28 | * @link https://vufind.org/wiki/development Wiki |
29 | */ |
30 | |
31 | namespace VuFind\Service; |
32 | |
33 | use NumberFormatter; |
34 | |
35 | /** |
36 | * Currency formatter |
37 | * |
38 | * @category VuFind |
39 | * @package Service |
40 | * @author Demian Katz <demian.katz@villanova.edu> |
41 | * @author Ere Maijala <ere.maijala@helsinki.fi> |
42 | * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License |
43 | * @link https://vufind.org/wiki/development Wiki |
44 | */ |
45 | class CurrencyFormatter |
46 | { |
47 | /** |
48 | * Default currency format (ISO 4217) to use. |
49 | * |
50 | * @var string |
51 | */ |
52 | protected $defaultCurrency; |
53 | |
54 | /** |
55 | * Number formatter. |
56 | * |
57 | * @var NumberFormatter |
58 | */ |
59 | protected $formatter; |
60 | |
61 | /** |
62 | * Constructor |
63 | * |
64 | * @param string $defaultCurrency Default currency format (ISO 4217) to use (null |
65 | * for default from system locale) |
66 | * @param string $locale Locale to use for number formatting (null for |
67 | * default system locale) |
68 | */ |
69 | public function __construct($defaultCurrency = null, $locale = null) |
70 | { |
71 | // Initialize number formatter: |
72 | $locale ??= setlocale(LC_MONETARY, 0); |
73 | $this->formatter = new NumberFormatter($locale, NumberFormatter::CURRENCY); |
74 | |
75 | // Initialize default currency: |
76 | if (null === $defaultCurrency) { |
77 | $defaultCurrency = trim($this->formatter->getTextAttribute(NumberFormatter::CURRENCY_CODE) ?: ''); |
78 | } |
79 | $this->defaultCurrency = empty($defaultCurrency) ? 'USD' : $defaultCurrency; |
80 | } |
81 | |
82 | /** |
83 | * Convert currency from float to display format |
84 | * |
85 | * @param float $number The number to format |
86 | * @param string $currency Currency format (ISO 4217) to use (null for default) |
87 | * |
88 | * @return string |
89 | */ |
90 | public function convertToDisplayFormat($number, $currency = null) |
91 | { |
92 | return $this->formatter->formatCurrency( |
93 | (float)$number, |
94 | $currency ?: $this->defaultCurrency |
95 | ); |
96 | } |
97 | } |