CountryCollection::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 0
nc 1
nop 0
dl 0
loc 2
rs 10
c 1
b 0
f 0
1
<?php
2
/**
3
 * @package Localization
4
 * @subpackage Countries
5
 */
6
7
declare(strict_types=1);
8
9
namespace AppLocalize\Localization\Countries;
10
11
use AppLocalize\Localization_Country_AT;
12
use AppLocalize\Localization_Country_CA;
13
use AppLocalize\Localization_Country_DE;
14
use AppLocalize\Localization_Country_ES;
15
use AppLocalize\Localization_Country_FR;
16
use AppLocalize\Localization_Country_IT;
17
use AppLocalize\Localization_Country_MX;
18
use AppLocalize\Localization_Country_PL;
19
use AppLocalize\Localization_Country_RO;
20
use AppLocalize\Localization_Country_UK;
21
use AppLocalize\Localization_Country_US;
22
use AppLocalize\Localization_Country_ZZ;
23
use AppUtils\Collections\BaseStringPrimaryCollection;
24
use AppUtils\Collections\CollectionException;
25
26
/**
27
 * Country collection that gives access to all available
28
 * countries and their data.
29
 *
30
 * @package Localization
31
 * @subpackage Countries
32
 *
33
 * @method BaseCountry getByID(string $id)
34
 * @method BaseCountry getDefault()
35
 * @method BaseCountry[] getAll()
36
 */
37
class CountryCollection extends BaseStringPrimaryCollection
38
{
39
    private static ?CountryCollection $instance = null;
40
41
    private function __construct()
42
    {
43
    }
44
45
    public static function getInstance(): CountryCollection
46
    {
47
        if (self::$instance === null) {
48
            self::$instance = new self();
49
        }
50
51
        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...tries\CountryCollection. Consider adding an additional type-check to rule them out.
Loading history...
52
    }
53
54
    /**
55
     * @param string $iso Two-letter ISO code of the country, e.g. `de`. Case-insensitive.
56
     * @return CountryInterface
57
     * @throws CollectionException
58
     */
59
    public function getByISO(string $iso) : CountryInterface
60
    {
61
        return $this->getByID(strtolower($iso));
62
    }
63
64
    /**
65
     * Checks whether the target ISO code is known.
66
     * @param string $iso Two-letter ISO code of the country, e.g. `de`. Case-insensitive.
67
     * @return bool
68
     */
69
    public function isoExists(string $iso) : bool
70
    {
71
        return $this->idExists(strtolower($iso));
72
    }
73
74
    public function getDefaultID(): string
75
    {
76
        return Localization_Country_US::ISO_CODE;
77
    }
78
79
    protected function registerItems(): void
80
    {
81
        $this->registerItem(new Localization_Country_AT());
82
        $this->registerItem(new Localization_Country_CA());
83
        $this->registerItem(new Localization_Country_DE());
84
        $this->registerItem(new Localization_Country_ES());
85
        $this->registerItem(new Localization_Country_FR());
86
        $this->registerItem(new Localization_Country_IT());
87
        $this->registerItem(new Localization_Country_MX());
88
        $this->registerItem(new Localization_Country_PL());
89
        $this->registerItem(new Localization_Country_RO());
90
        $this->registerItem(new Localization_Country_UK());
91
        $this->registerItem(new Localization_Country_US());
92
        $this->registerItem(new Localization_Country_ZZ());
93
    }
94
95
    private ?CannedCountries $canned = null;
96
97
    public function choose() : CannedCountries
98
    {
99
        if(!isset($this->canned)) {
100
            $this->canned = new CannedCountries();
101
        }
102
103
        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\Countries\CannedCountries. Consider adding an additional type-check to rule them out.
Loading history...
104
    }
105
}
106