CurrencyCodeUtil::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 1
c 0
b 0
f 0
ccs 0
cts 0
cp 0
rs 10
cc 1
nc 1
nop 0
crap 2
1
<?php
2
/*
3
 * This file is part of the Exchange Rate package, an RunOpenCode project.
4
 *
5
 * (c) 2017 RunOpenCode
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace RunOpenCode\ExchangeRate\Utils;
11
12
use RunOpenCode\ExchangeRate\Exception\UnknownCurrencyCodeException;
13
14
/**
15
 * Class CurrencyCodeUtil
16
 *
17
 * Currency code utilities.
18
 *
19
 * @package RunOpenCode\ExchangeRate\Utils
20
 */
21
final class CurrencyCodeUtil
22
{
23
    private function __construct() { /* noop */ }
24
25
    /**
26
     * List of currency codes.
27
     *
28
     * List of currency codes. List contains some of already obsolete currency codes as well.
29
     *
30
     * @see https://gist.github.com/Min2liz/5728013
31
     * @see https://en.wikipedia.org/wiki/ISO_4217
32
     *
33
     * @var array
34
     */
35
    private static $codes = array(
36
        'ALL' => 'Albania Lek',
37
        'AFN' => 'Afghanistan Afghani',
38
        'ARS' => 'Argentina Peso',
39
        'AWG' => 'Aruba Guilder',
40
        'AUD' => 'Australia Dollar',
41
        'AZN' => 'Azerbaijan New Manat',
42
        'BSD' => 'Bahamas Dollar',
43
        'BBD' => 'Barbados Dollar',
44
        'BDT' => 'Bangladeshi taka',
45
        'BYR' => 'Belarus Ruble',
46
        'BZD' => 'Belize Dollar',
47
        'BMD' => 'Bermuda Dollar',
48
        'BOB' => 'Bolivia Boliviano',
49
        'BAM' => 'Bosnia and Herzegovina Convertible Marka',
50
        'BWP' => 'Botswana Pula',
51
        'BGN' => 'Bulgaria Lev',
52
        'BRL' => 'Brazil Real',
53
        'BND' => 'Brunei Darussalam Dollar',
54
        'KHR' => 'Cambodia Riel',
55
        'CAD' => 'Canada Dollar',
56
        'KYD' => 'Cayman Islands Dollar',
57
        'CLP' => 'Chile Peso',
58
        'CNY' => 'China Yuan Renminbi',
59
        'COP' => 'Colombia Peso',
60
        'CRC' => 'Costa Rica Colon',
61
        'HRK' => 'Croatia Kuna',
62
        'CUP' => 'Cuba Peso',
63
        'CZK' => 'Czech Republic Koruna',
64
        'DKK' => 'Denmark Krone',
65
        'DOP' => 'Dominican Republic Peso',
66
        'XCD' => 'East Caribbean Dollar',
67
        'EGP' => 'Egypt Pound',
68
        'SVC' => 'El Salvador Colon',
69
        'EEK' => 'Estonia Kroon',
70
        'EUR' => 'Euro Member Countries',
71
        'FKP' => 'Falkland Islands (Malvinas) Pound',
72
        'FJD' => 'Fiji Dollar',
73
        'GHC' => 'Ghana Cedis',
74
        'GIP' => 'Gibraltar Pound',
75
        'GTQ' => 'Guatemala Quetzal',
76
        'GGP' => 'Guernsey Pound',
77
        'GYD' => 'Guyana Dollar',
78
        'HNL' => 'Honduras Lempira',
79
        'HKD' => 'Hong Kong Dollar',
80
        'HUF' => 'Hungary Forint',
81
        'ISK' => 'Iceland Krona',
82
        'INR' => 'India Rupee',
83
        'IDR' => 'Indonesia Rupiah',
84
        'IRR' => 'Iran Rial',
85
        'IMP' => 'Isle of Man Pound',
86
        'ILS' => 'Israel Shekel',
87
        'JMD' => 'Jamaica Dollar',
88
        'JPY' => 'Japan Yen',
89
        'JEP' => 'Jersey Pound',
90
        'KZT' => 'Kazakhstan Tenge',
91
        'KPW' => 'Korea (North) Won',
92
        'KRW' => 'Korea (South) Won',
93
        'KGS' => 'Kyrgyzstan Som',
94
        'LAK' => 'Laos Kip',
95
        'LVL' => 'Latvia Lat',
96
        'LBP' => 'Lebanon Pound',
97
        'LRD' => 'Liberia Dollar',
98
        'LTL' => 'Lithuania Litas',
99
        'MKD' => 'Macedonia Denar',
100
        'MYR' => 'Malaysia Ringgit',
101
        'MUR' => 'Mauritius Rupee',
102
        'MXN' => 'Mexico Peso',
103
        'MNT' => 'Mongolia Tughrik',
104
        'MZN' => 'Mozambique Metical',
105
        'NAD' => 'Namibia Dollar',
106
        'NPR' => 'Nepal Rupee',
107
        'ANG' => 'Netherlands Antilles Guilder',
108
        'NZD' => 'New Zealand Dollar',
109
        'NIO' => 'Nicaragua Cordoba',
110
        'NGN' => 'Nigeria Naira',
111
        'NOK' => 'Norway Krone',
112
        'OMR' => 'Oman Rial',
113
        'PKR' => 'Pakistan Rupee',
114
        'PAB' => 'Panama Balboa',
115
        'PYG' => 'Paraguay Guarani',
116
        'PEN' => 'Peru Nuevo Sol',
117
        'PHP' => 'Philippines Peso',
118
        'PLN' => 'Poland Zloty',
119
        'QAR' => 'Qatar Riyal',
120
        'RON' => 'Romania New Leu',
121
        'RUB' => 'Russia Ruble',
122
        'SHP' => 'Saint Helena Pound',
123
        'SAR' => 'Saudi Arabia Riyal',
124
        'RSD' => 'Serbia Dinar',
125
        'SCR' => 'Seychelles Rupee',
126
        'SGD' => 'Singapore Dollar',
127
        'SBD' => 'Solomon Islands Dollar',
128
        'SOS' => 'Somalia Shilling',
129
        'ZAR' => 'South Africa Rand',
130
        'LKR' => 'Sri Lanka Rupee',
131
        'SEK' => 'Sweden Krona',
132
        'CHF' => 'Switzerland Franc',
133
        'SRD' => 'Suriname Dollar',
134
        'SYP' => 'Syria Pound',
135
        'TWD' => 'Taiwan New Dollar',
136
        'THB' => 'Thailand Baht',
137
        'TTD' => 'Trinidad and Tobago Dollar',
138
        'TRY' => 'Turkey Lira',
139
        'TRL' => 'Turkey Lira',
140
        'TVD' => 'Tuvalu Dollar',
141
        'UAH' => 'Ukraine Hryvna',
142
        'GBP' => 'United Kingdom Pound',
143
        'UGX' => 'Uganda Shilling',
144
        'USD' => 'United States Dollar',
145
        'UYU' => 'Uruguay Peso',
146
        'UZS' => 'Uzbekistan Som',
147
        'VEF' => 'Venezuela Bolivar',
148
        'VND' => 'Viet Nam Dong',
149
        'YER' => 'Yemen Rial',
150
        'ZWD' => 'Zimbabwe Dollar',
151
        'KWD' => 'Kuwait Dinar',
152
        'ATS' => 'Austrian Schilling',
153
        'BEF' => 'Belgian Franc',
154
        'FIM' => 'Finnish Markka',
155
        'FRF' => 'French Franc',
156
        'DEM' => 'German Mark',
157
        'GRD' => 'Greece Drachma',
158
        'IEP' => 'Ireland Pound',
159
        'ITL' => 'Italy Lira',
160
        'LUF' => 'Luxembourg Franc',
161
        'PTE' => 'Portugal Escudo',
162
        'ESP' => 'Spain Peseta',
163
        'XDR' => 'IMF Special Drawing Rights',
164
        'SKK' => 'Slovak Koruna',
165
        'BYN' => 'Belarusian ruble',
166
    );
167
168
    /**
169
     * Check if currency code exists.
170
     *
171
     * @param string $currencyCode ISO 4271 currency code.
172
     * @return bool TRUE if exists.
173 53
     */
174
    public static function exists($currencyCode)
175 53
    {
176
        $currencyCode = trim(strtoupper($currencyCode));
177 53
178
        return array_key_exists($currencyCode, self::$codes);
179
    }
180
181
    /**
182
     * Clean currency code
183
     *
184
     * @param string $currencyCode Currency code to clean up.
185
     * @return string Cleaned ISO 4271 currency code.
186
     * @throws UnknownCurrencyCodeException If currency code does not exists.
187 51
     */
188
    public static function clean($currencyCode)
189 51
    {
190
        $clean = trim(strtoupper($currencyCode));
191 51
192 1
        if (!self::exists($clean)) {
193
            throw new UnknownCurrencyCodeException(sprintf('Unknown currency code "%s".', $currencyCode));
194
        }
195 50
196
        return $clean;
197
    }
198
}
199