CurrencyCollection::getByISO()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
/**
3
 * @package Localization
4
 * @subpackage Currencies
5
 */
6
7
declare(strict_types=1);
8
9
namespace AppLocalize\Localization\Currencies;
10
11
use AppLocalize\Localization\Countries\CountryInterface;
12
use AppLocalize\Localization_Currency_CAD;
13
use AppLocalize\Localization_Currency_CHF;
14
use AppLocalize\Localization_Currency_EUR;
15
use AppLocalize\Localization_Currency_GBP;
16
use AppLocalize\Localization_Currency_MXN;
17
use AppLocalize\Localization_Currency_PLN;
18
use AppLocalize\Localization_Currency_RON;
19
use AppLocalize\Localization_Currency_USD;
20
use AppUtils\Collections\BaseStringPrimaryCollection;
21
use AppUtils\Collections\CollectionException;
22
23
/**
24
 * @package Localization
25
 * @subpackage Currencies
26
 *
27
 * @method CurrencyInterface getByID(string $id)
28
 * @method CurrencyInterface getDefault()
29
 * @method CurrencyInterface[] getAll()
30
 */
31
class CurrencyCollection extends BaseStringPrimaryCollection
32
{
33
    private static ?CurrencyCollection $instance = null;
34
35
    private function __construct()
36
    {
37
    }
38
39
    public static function getInstance(): CurrencyCollection
40
    {
41
        if (self::$instance === null) {
42
            self::$instance = new self();
43
        }
44
45
        return self::$instance;
0 ignored issues
show
Bug Best Practice introduced by
The expression return self::instance could return the type null which is incompatible with the type-hinted return AppLocalize\Localization...cies\CurrencyCollection. Consider adding an additional type-check to rule them out.
Loading history...
46
    }
47
48
    public function getDefaultID(): string
49
    {
50
        return Localization_Currency_USD::ISO_CODE;
51
    }
52
53
    protected function registerItems(): void
54
    {
55
        $this->registerItem(new Localization_Currency_CAD());
56
        $this->registerItem(new Localization_Currency_CHF());
57
        $this->registerItem(new Localization_Currency_EUR());
58
        $this->registerItem(new Localization_Currency_GBP());
59
        $this->registerItem(new Localization_Currency_MXN());
60
        $this->registerItem(new Localization_Currency_PLN());
61
        $this->registerItem(new Localization_Currency_RON());
62
        $this->registerItem(new Localization_Currency_USD());
63
    }
64
65
    /**
66
     * @param string $iso Three-letter ISO code of the currency, e.g. `EUR`. Case-insensitive.
67
     * @return CurrencyInterface
68
     * @throws CollectionException
69
     */
70
    public function getByISO(string $iso) : CurrencyInterface
71
    {
72
        return $this->getByID(strtoupper($iso));
73
    }
74
75
    /**
76
     * Checks whether the target ISO code is known.
77
     * @param string $iso Three-letter ISO code of the currency, e.g. `USD`. Case-insensitive.
78
     * @return bool
79
     */
80
    public function isoExists(string $iso) : bool
81
    {
82
        return $this->idExists(strtoupper($iso));
83
    }
84
85
    private ?CannedCurrencies $canned = null;
86
87
    public function choose() : CannedCurrencies
88
    {
89
        if(!isset($this->canned)) {
90
            $this->canned = new CannedCurrencies();
91
        }
92
93
        return $this->canned;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->canned could return the type null which is incompatible with the type-hinted return AppLocalize\Localization...encies\CannedCurrencies. Consider adding an additional type-check to rule them out.
Loading history...
94
    }
95
}
96