|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Currencies |
|
4
|
|
|
* |
|
5
|
|
|
* @author Pronamic <[email protected]> |
|
6
|
|
|
* @copyright 2005-2023 Pronamic |
|
7
|
|
|
* @license GPL-3.0-or-later |
|
8
|
|
|
* @package Pronamic\WordPress\Money |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Pronamic\WordPress\Money; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Currencies |
|
15
|
|
|
* |
|
16
|
|
|
* @link https://github.com/moneyphp/money/blob/v3.1.3/resources/currency.php |
|
17
|
|
|
* |
|
18
|
|
|
* @author Remco Tolsma |
|
19
|
|
|
* @version 2.0.0 |
|
20
|
|
|
* @since 1.0.0 |
|
21
|
|
|
*/ |
|
22
|
|
|
class Currencies { |
|
23
|
|
|
/** |
|
24
|
|
|
* Map of known currencies indexed by code. |
|
25
|
|
|
* |
|
26
|
|
|
* @var array<string, Currency>|null |
|
27
|
|
|
*/ |
|
28
|
|
|
private static $currencies; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Get currencies. |
|
32
|
|
|
* |
|
33
|
|
|
* @return array<string, Currency> |
|
34
|
|
|
*/ |
|
35
|
103 |
|
public static function get_currencies() { |
|
36
|
103 |
|
if ( is_null( self::$currencies ) ) { |
|
37
|
1 |
|
self::$currencies = self::load_currencies(); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
103 |
|
return self::$currencies; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Get currency. |
|
45
|
|
|
* |
|
46
|
|
|
* @param string $alphabetic_code Alphabetic currency code. |
|
47
|
|
|
* |
|
48
|
|
|
* @return Currency |
|
49
|
|
|
*/ |
|
50
|
103 |
|
public static function get_currency( $alphabetic_code ) { |
|
51
|
103 |
|
$currencies = self::get_currencies(); |
|
52
|
|
|
|
|
53
|
103 |
|
if ( \array_key_exists( $alphabetic_code, $currencies ) ) { |
|
54
|
102 |
|
return $currencies[ $alphabetic_code ]; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
1 |
|
return new Currency( $alphabetic_code ); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Load currencies. |
|
62
|
|
|
* |
|
63
|
|
|
* @link https://github.com/moneyphp/money/blob/v3.1.3/src/Currencies/ISOCurrencies.php#L90-L102 |
|
64
|
|
|
* @return array<string, Currency> |
|
65
|
|
|
* @throws \RuntimeException Throws runtime exception if currencies could not be loaded from file. |
|
66
|
|
|
*/ |
|
67
|
1 |
|
private static function load_currencies() { |
|
68
|
1 |
|
$file = __DIR__ . '/../resources/currencies.php'; |
|
69
|
|
|
|
|
70
|
1 |
|
$currencies = []; |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Data. |
|
74
|
|
|
* |
|
75
|
|
|
* @psalm-suppress UnresolvableInclude |
|
76
|
|
|
* |
|
77
|
|
|
* @var array<int, Currency> |
|
78
|
|
|
*/ |
|
79
|
1 |
|
$data = require $file; |
|
80
|
|
|
|
|
81
|
1 |
|
foreach ( $data as $currency ) { |
|
82
|
1 |
|
$alphabetic_code = $currency->get_alphabetic_code(); |
|
83
|
|
|
|
|
84
|
1 |
|
$currencies[ $alphabetic_code ] = $currency; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
1 |
|
return $currencies; |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|