Passed
Push — 6.0 ( 4cc368...44c261 )
by Olivier
01:44
created

Currency::localized()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * CODE GENERATED; DO NOT EDIT.
5
 *
6
 * {@see \ICanBoogie\CLDR\Generator\Command\GenerateCurrency}
7
 */
8
9
namespace ICanBoogie\CLDR;
10
11
use ICanBoogie\CLDR\Supplemental\Fraction;
12
13
/**
14
 * Representation of a currency.
15
 *
16
 * @link https://www.unicode.org/reports/tr35/tr35-72/tr35-numbers.html#Currencies
17
 *
18
 * @implements Localizable<Currency, LocalizedCurrency>
19
 */
20
final class Currency implements Localizable
21
{
22
    /**
23
     * @link https://github.com/unicode-org/cldr-json/blob/45.0.0/cldr-json/cldr-numbers-modern/main/en-001/currencies.json
24
     */
25
    public const CODES =
26
        [
27
            'ADP',
28
            'AED',
29
            'AFA',
30
            'AFN',
31
            'ALK',
32
            'ALL',
33
            'AMD',
34
            'ANG',
35
            'AOA',
36
            'AOK',
37
            'AON',
38
            'AOR',
39
            'ARA',
40
            'ARL',
41
            'ARM',
42
            'ARP',
43
            'ARS',
44
            'ATS',
45
            'AUD',
46
            'AWG',
47
            'AZM',
48
            'AZN',
49
            'BAD',
50
            'BAM',
51
            'BAN',
52
            'BBD',
53
            'BDT',
54
            'BEC',
55
            'BEF',
56
            'BEL',
57
            'BGL',
58
            'BGM',
59
            'BGN',
60
            'BGO',
61
            'BHD',
62
            'BIF',
63
            'BMD',
64
            'BND',
65
            'BOB',
66
            'BOL',
67
            'BOP',
68
            'BOV',
69
            'BRB',
70
            'BRC',
71
            'BRE',
72
            'BRL',
73
            'BRN',
74
            'BRR',
75
            'BRZ',
76
            'BSD',
77
            'BTN',
78
            'BUK',
79
            'BWP',
80
            'BYB',
81
            'BYN',
82
            'BYR',
83
            'BZD',
84
            'CAD',
85
            'CDF',
86
            'CHE',
87
            'CHF',
88
            'CHW',
89
            'CLE',
90
            'CLF',
91
            'CLP',
92
            'CNH',
93
            'CNX',
94
            'CNY',
95
            'COP',
96
            'COU',
97
            'CRC',
98
            'CSD',
99
            'CSK',
100
            'CUC',
101
            'CUP',
102
            'CVE',
103
            'CYP',
104
            'CZK',
105
            'DDM',
106
            'DEM',
107
            'DJF',
108
            'DKK',
109
            'DOP',
110
            'DZD',
111
            'ECS',
112
            'ECV',
113
            'EEK',
114
            'EGP',
115
            'ERN',
116
            'ESA',
117
            'ESB',
118
            'ESP',
119
            'ETB',
120
            'EUR',
121
            'FIM',
122
            'FJD',
123
            'FKP',
124
            'FRF',
125
            'GBP',
126
            'GEK',
127
            'GEL',
128
            'GHC',
129
            'GHS',
130
            'GIP',
131
            'GMD',
132
            'GNF',
133
            'GNS',
134
            'GQE',
135
            'GRD',
136
            'GTQ',
137
            'GWE',
138
            'GWP',
139
            'GYD',
140
            'HKD',
141
            'HNL',
142
            'HRD',
143
            'HRK',
144
            'HTG',
145
            'HUF',
146
            'IDR',
147
            'IEP',
148
            'ILP',
149
            'ILR',
150
            'ILS',
151
            'INR',
152
            'IQD',
153
            'IRR',
154
            'ISJ',
155
            'ISK',
156
            'ITL',
157
            'JMD',
158
            'JOD',
159
            'JPY',
160
            'KES',
161
            'KGS',
162
            'KHR',
163
            'KMF',
164
            'KPW',
165
            'KRH',
166
            'KRO',
167
            'KRW',
168
            'KWD',
169
            'KYD',
170
            'KZT',
171
            'LAK',
172
            'LBP',
173
            'LKR',
174
            'LRD',
175
            'LSL',
176
            'LTL',
177
            'LTT',
178
            'LUC',
179
            'LUF',
180
            'LUL',
181
            'LVL',
182
            'LVR',
183
            'LYD',
184
            'MAD',
185
            'MAF',
186
            'MCF',
187
            'MDC',
188
            'MDL',
189
            'MGA',
190
            'MGF',
191
            'MKD',
192
            'MKN',
193
            'MLF',
194
            'MMK',
195
            'MNT',
196
            'MOP',
197
            'MRO',
198
            'MRU',
199
            'MTL',
200
            'MTP',
201
            'MUR',
202
            'MVP',
203
            'MVR',
204
            'MWK',
205
            'MXN',
206
            'MXP',
207
            'MXV',
208
            'MYR',
209
            'MZE',
210
            'MZM',
211
            'MZN',
212
            'NAD',
213
            'NGN',
214
            'NIC',
215
            'NIO',
216
            'NLG',
217
            'NOK',
218
            'NPR',
219
            'NZD',
220
            'OMR',
221
            'PAB',
222
            'PEI',
223
            'PEN',
224
            'PES',
225
            'PGK',
226
            'PHP',
227
            'PKR',
228
            'PLN',
229
            'PLZ',
230
            'PTE',
231
            'PYG',
232
            'QAR',
233
            'RHD',
234
            'ROL',
235
            'RON',
236
            'RSD',
237
            'RUB',
238
            'RUR',
239
            'RWF',
240
            'SAR',
241
            'SBD',
242
            'SCR',
243
            'SDD',
244
            'SDG',
245
            'SDP',
246
            'SEK',
247
            'SGD',
248
            'SHP',
249
            'SIT',
250
            'SKK',
251
            'SLE',
252
            'SLL',
253
            'SOS',
254
            'SRD',
255
            'SRG',
256
            'SSP',
257
            'STD',
258
            'STN',
259
            'SUR',
260
            'SVC',
261
            'SYP',
262
            'SZL',
263
            'THB',
264
            'TJR',
265
            'TJS',
266
            'TMM',
267
            'TMT',
268
            'TND',
269
            'TOP',
270
            'TPE',
271
            'TRL',
272
            'TRY',
273
            'TTD',
274
            'TWD',
275
            'TZS',
276
            'UAH',
277
            'UAK',
278
            'UGS',
279
            'UGX',
280
            'USD',
281
            'USN',
282
            'USS',
283
            'UYI',
284
            'UYP',
285
            'UYU',
286
            'UYW',
287
            'UZS',
288
            'VEB',
289
            'VED',
290
            'VEF',
291
            'VES',
292
            'VND',
293
            'VNN',
294
            'VUV',
295
            'WST',
296
            'XAF',
297
            'XAG',
298
            'XAU',
299
            'XBA',
300
            'XBB',
301
            'XBC',
302
            'XBD',
303
            'XCD',
304
            'XCG',
305
            'XDR',
306
            'XEU',
307
            'XFO',
308
            'XFU',
309
            'XOF',
310
            'XPD',
311
            'XPF',
312
            'XPT',
313
            'XRE',
314
            'XSU',
315
            'XTS',
316
            'XUA',
317
            'XXX',
318
            'YDD',
319
            'YER',
320
            'YUD',
321
            'YUM',
322
            'YUN',
323
            'YUR',
324
            'ZAL',
325
            'ZAR',
326
            'ZMK',
327
            'ZMW',
328
            'ZRN',
329
            'ZRZ',
330
            'ZWD',
331
            'ZWL',
332
            'ZWR',
333
        ];
334
335
    /**
336
     * @link https://github.com/unicode-org/cldr-json/blob/45.0.0/cldr-json/cldr-core/supplemental/currencyData.json
337
     */
338
    private const FRACTIONS =
339
        [
340
            'ADP' => [
341
                '_rounding' => '0',
342
                '_digits' => '0',
343
            ],
344
            'AFN' => [
345
                '_rounding' => '0',
346
                '_digits' => '0',
347
            ],
348
            'ALL' => [
349
                '_rounding' => '0',
350
                '_digits' => '0',
351
            ],
352
            'AMD' => [
353
                '_rounding' => '0',
354
                '_digits' => '2',
355
                '_cashRounding' => '0',
356
                '_cashDigits' => '0',
357
            ],
358
            'BHD' => [
359
                '_rounding' => '0',
360
                '_digits' => '3',
361
            ],
362
            'BIF' => [
363
                '_rounding' => '0',
364
                '_digits' => '0',
365
            ],
366
            'BYN' => [
367
                '_rounding' => '0',
368
                '_digits' => '2',
369
            ],
370
            'BYR' => [
371
                '_rounding' => '0',
372
                '_digits' => '0',
373
            ],
374
            'CAD' => [
375
                '_rounding' => '0',
376
                '_digits' => '2',
377
                '_cashRounding' => '5',
378
            ],
379
            'CHF' => [
380
                '_rounding' => '0',
381
                '_digits' => '2',
382
                '_cashRounding' => '5',
383
            ],
384
            'CLF' => [
385
                '_rounding' => '0',
386
                '_digits' => '4',
387
            ],
388
            'CLP' => [
389
                '_rounding' => '0',
390
                '_digits' => '0',
391
            ],
392
            'COP' => [
393
                '_rounding' => '0',
394
                '_digits' => '2',
395
                '_cashRounding' => '0',
396
                '_cashDigits' => '0',
397
            ],
398
            'CRC' => [
399
                '_rounding' => '0',
400
                '_digits' => '2',
401
                '_cashRounding' => '0',
402
                '_cashDigits' => '0',
403
            ],
404
            'CZK' => [
405
                '_rounding' => '0',
406
                '_digits' => '2',
407
                '_cashRounding' => '0',
408
                '_cashDigits' => '0',
409
            ],
410
            'DEFAULT' => [
411
                '_rounding' => '0',
412
                '_digits' => '2',
413
            ],
414
            'DJF' => [
415
                '_rounding' => '0',
416
                '_digits' => '0',
417
            ],
418
            'DKK' => [
419
                '_rounding' => '0',
420
                '_digits' => '2',
421
                '_cashRounding' => '50',
422
            ],
423
            'ESP' => [
424
                '_rounding' => '0',
425
                '_digits' => '0',
426
            ],
427
            'GNF' => [
428
                '_rounding' => '0',
429
                '_digits' => '0',
430
            ],
431
            'GYD' => [
432
                '_rounding' => '0',
433
                '_digits' => '2',
434
                '_cashRounding' => '0',
435
                '_cashDigits' => '0',
436
            ],
437
            'HUF' => [
438
                '_rounding' => '0',
439
                '_digits' => '2',
440
                '_cashRounding' => '0',
441
                '_cashDigits' => '0',
442
            ],
443
            'IDR' => [
444
                '_rounding' => '0',
445
                '_digits' => '2',
446
                '_cashRounding' => '0',
447
                '_cashDigits' => '0',
448
            ],
449
            'IQD' => [
450
                '_rounding' => '0',
451
                '_digits' => '0',
452
            ],
453
            'IRR' => [
454
                '_rounding' => '0',
455
                '_digits' => '0',
456
            ],
457
            'ISK' => [
458
                '_rounding' => '0',
459
                '_digits' => '0',
460
            ],
461
            'ITL' => [
462
                '_rounding' => '0',
463
                '_digits' => '0',
464
            ],
465
            'JOD' => [
466
                '_rounding' => '0',
467
                '_digits' => '3',
468
            ],
469
            'JPY' => [
470
                '_rounding' => '0',
471
                '_digits' => '0',
472
            ],
473
            'KMF' => [
474
                '_rounding' => '0',
475
                '_digits' => '0',
476
            ],
477
            'KPW' => [
478
                '_rounding' => '0',
479
                '_digits' => '0',
480
            ],
481
            'KRW' => [
482
                '_rounding' => '0',
483
                '_digits' => '0',
484
            ],
485
            'KWD' => [
486
                '_rounding' => '0',
487
                '_digits' => '3',
488
            ],
489
            'LAK' => [
490
                '_rounding' => '0',
491
                '_digits' => '0',
492
            ],
493
            'LBP' => [
494
                '_rounding' => '0',
495
                '_digits' => '0',
496
            ],
497
            'LUF' => [
498
                '_rounding' => '0',
499
                '_digits' => '0',
500
            ],
501
            'LYD' => [
502
                '_rounding' => '0',
503
                '_digits' => '3',
504
            ],
505
            'MGA' => [
506
                '_rounding' => '0',
507
                '_digits' => '0',
508
            ],
509
            'MGF' => [
510
                '_rounding' => '0',
511
                '_digits' => '0',
512
            ],
513
            'MMK' => [
514
                '_rounding' => '0',
515
                '_digits' => '0',
516
            ],
517
            'MNT' => [
518
                '_rounding' => '0',
519
                '_digits' => '2',
520
                '_cashRounding' => '0',
521
                '_cashDigits' => '0',
522
            ],
523
            'MRO' => [
524
                '_rounding' => '0',
525
                '_digits' => '0',
526
            ],
527
            'MUR' => [
528
                '_rounding' => '0',
529
                '_digits' => '2',
530
                '_cashRounding' => '0',
531
                '_cashDigits' => '0',
532
            ],
533
            'NOK' => [
534
                '_rounding' => '0',
535
                '_digits' => '2',
536
                '_cashRounding' => '0',
537
                '_cashDigits' => '0',
538
            ],
539
            'OMR' => [
540
                '_rounding' => '0',
541
                '_digits' => '3',
542
            ],
543
            'PKR' => [
544
                '_rounding' => '0',
545
                '_digits' => '2',
546
                '_cashRounding' => '0',
547
                '_cashDigits' => '0',
548
            ],
549
            'PYG' => [
550
                '_rounding' => '0',
551
                '_digits' => '0',
552
            ],
553
            'RSD' => [
554
                '_rounding' => '0',
555
                '_digits' => '0',
556
            ],
557
            'RWF' => [
558
                '_rounding' => '0',
559
                '_digits' => '0',
560
            ],
561
            'SEK' => [
562
                '_rounding' => '0',
563
                '_digits' => '2',
564
                '_cashRounding' => '0',
565
                '_cashDigits' => '0',
566
            ],
567
            'SLE' => [
568
                '_rounding' => '0',
569
                '_digits' => '2',
570
            ],
571
            'SLL' => [
572
                '_rounding' => '0',
573
                '_digits' => '0',
574
            ],
575
            'SOS' => [
576
                '_rounding' => '0',
577
                '_digits' => '0',
578
            ],
579
            'STD' => [
580
                '_rounding' => '0',
581
                '_digits' => '0',
582
            ],
583
            'SYP' => [
584
                '_rounding' => '0',
585
                '_digits' => '0',
586
            ],
587
            'TMM' => [
588
                '_rounding' => '0',
589
                '_digits' => '0',
590
            ],
591
            'TND' => [
592
                '_rounding' => '0',
593
                '_digits' => '3',
594
            ],
595
            'TRL' => [
596
                '_rounding' => '0',
597
                '_digits' => '0',
598
            ],
599
            'TWD' => [
600
                '_rounding' => '0',
601
                '_digits' => '2',
602
                '_cashRounding' => '0',
603
                '_cashDigits' => '0',
604
            ],
605
            'TZS' => [
606
                '_rounding' => '0',
607
                '_digits' => '2',
608
                '_cashRounding' => '0',
609
                '_cashDigits' => '0',
610
            ],
611
            'UGX' => [
612
                '_rounding' => '0',
613
                '_digits' => '0',
614
            ],
615
            'UYI' => [
616
                '_rounding' => '0',
617
                '_digits' => '0',
618
            ],
619
            'UYW' => [
620
                '_rounding' => '0',
621
                '_digits' => '4',
622
            ],
623
            'UZS' => [
624
                '_rounding' => '0',
625
                '_digits' => '2',
626
                '_cashRounding' => '0',
627
                '_cashDigits' => '0',
628
            ],
629
            'VEF' => [
630
                '_rounding' => '0',
631
                '_digits' => '2',
632
                '_cashRounding' => '0',
633
                '_cashDigits' => '0',
634
            ],
635
            'VND' => [
636
                '_rounding' => '0',
637
                '_digits' => '0',
638
            ],
639
            'VUV' => [
640
                '_rounding' => '0',
641
                '_digits' => '0',
642
            ],
643
            'XAF' => [
644
                '_rounding' => '0',
645
                '_digits' => '0',
646
            ],
647
            'XOF' => [
648
                '_rounding' => '0',
649
                '_digits' => '0',
650
            ],
651
            'XPF' => [
652
                '_rounding' => '0',
653
                '_digits' => '0',
654
            ],
655
            'YER' => [
656
                '_rounding' => '0',
657
                '_digits' => '0',
658
            ],
659
            'ZMK' => [
660
                '_rounding' => '0',
661
                '_digits' => '0',
662
            ],
663
            'ZWD' => [
664
                '_rounding' => '0',
665
                '_digits' => '0',
666
            ],
667
        ];
668
669
    private const FRACTIONS_FALLBACK = 'DEFAULT';
670
671
    /**
672
     * Whether a currency code is defined.
673
     *
674
     * @param string $code
675
     *     A currency code; for example, EUR.
676
     */
677
    public static function is_defined(string $code): bool
678
    {
679
        return in_array($code, self::CODES);
680
    }
681
682
    /**
683
     * @param string $code
684
     *     A currency code; for example, EUR.
685
     *
686
     * @throws CurrencyNotDefined
687
     */
688
    public static function assert_is_defined(string $code): void
689
    {
690
        self::is_defined($code)
691
            or throw new CurrencyNotDefined($code);
692
    }
693
694
    /**
695
     * Returns a {@see CurrencyCode} of the specified code.
696
     *
697
     * @param string $code
698
     *     A currency code; for example, EUR.
699
     *
700
     * @throws CurrencyNotDefined
701
     */
702
    public static function of(string $code): self
703
    {
704
        static $instances;
705
706
        self::assert_is_defined($code);
707
708
        return $instances[$code] ??= new self($code, self::fraction_for($code));
709
    }
710
711
    /**
712
     * Returns the {@see Fraction} for the specified currency code.
713
     *
714
     * @param string $code
715
     * *     A currency code; for example, EUR.
716
     */
717
    private static function fraction_for(string $code): Fraction
718
    {
719
        static $default_fraction;
720
721
        $data = self::FRACTIONS[$code] ?? null;
722
723
        if (!$data) {
724
            return $default_fraction ??= self::fraction_for(self::FRACTIONS_FALLBACK);
725
        }
726
727
        return Fraction::from($data);
728
    }
729
730
    /**
731
     * @param string $code
732
     *     A currency code; for example, EUR.
733
    */
734
    private function __construct(
735
        public readonly string $code,
736
        public readonly Fraction $fraction,
737
    ) {
738
    }
739
740
    /**
741
     * Returns the {@see $code} of the currency.
742
     */
743
    public function __toString(): string
744
    {
745
        return $this->code;
746
    }
747
748
    public function __serialize(): array
749
    {
750
        return [ 'code' => $this->code ];
751
    }
752
753
    /**
754
     * @param array{ code: string } $data
755
     */
756
    public function __unserialize(array $data): void
757
    {
758
        $this->code = $data['code'];
759
        $this->fraction = self::fraction_for($this->code);
760
    }
761
762
    /**
763
     * Returns a localized currency.
764
     */
765
    public function localized(Locale $locale): LocalizedCurrency
766
    {
767
        return new LocalizedCurrency($this, $locale);
768
    }
769
}
770