-
Notifications
You must be signed in to change notification settings - Fork 135
/
Country.php
145 lines (128 loc) · 3.64 KB
/
Country.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<?php
namespace CommerceGuys\Addressing\Country;
/**
* Represents a country.
*/
final class Country
{
/**
* The two-letter country code.
*/
protected string $countryCode;
protected string $name;
/**
* The three-letter country code.
*/
protected ?string $threeLetterCode = null;
/**
* The numeric country code.
*/
protected ?string $numericCode = null;
protected ?string $currencyCode = null;
/**
* The locale (i.e. "en_US").
*/
protected string $locale;
public function __construct(array $definition)
{
foreach (['country_code', 'name', 'locale'] as $requiredProperty) {
if (empty($definition[$requiredProperty])) {
throw new \InvalidArgumentException(sprintf('Missing required property "%s".', $requiredProperty));
}
}
$this->countryCode = $definition['country_code'];
$this->name = $definition['name'];
if (isset($definition['three_letter_code'])) {
$this->threeLetterCode = $definition['three_letter_code'];
}
if (isset($definition['numeric_code'])) {
$this->numericCode = $definition['numeric_code'];
}
if (isset($definition['currency_code'])) {
$this->currencyCode = $definition['currency_code'];
}
$this->locale = $definition['locale'];
}
/**
* Gets the string representation of the Country.
*/
public function __toString(): string
{
return $this->countryCode;
}
/**
* Gets the two-letter country code.
*/
public function getCountryCode(): string
{
return $this->countryCode;
}
/**
* Gets the country name.
*
* This value is locale specific.
*/
public function getName(): string
{
return $this->name;
}
/**
* Gets the three-letter country code.
*
* Note that not every country has a three-letter code.
* CLDR lists "Canary Islands" (IC) and "Ceuta and Melilla" (EA)
* as separate countries, even though they are formally a part of Spain
* and have no three-letter or numeric ISO codes.
*/
public function getThreeLetterCode(): ?string
{
return $this->threeLetterCode;
}
/**
* Gets the numeric country code.
*
* The numeric code has three digits, and the first one can be a zero,
* hence the need to pass it around as a string.
*
* Note that not every country has a numeric code.
* CLDR lists "Canary Islands" (IC) and "Ceuta and Melilla" (EA)
* as separate countries, even though they are formally a part of Spain
* and have no three-letter or numeric ISO codes.
* "Ascension Island" (AE) also has no numeric code, even though it has a
* three-letter code.
*/
public function getNumericCode(): ?string
{
return $this->numericCode;
}
/**
* Gets the currency code.
*
* Represents the official currency used in the country, if known.
*/
public function getCurrencyCode(): ?string
{
return $this->currencyCode;
}
/**
* Gets the timezones.
*
* Note that a country can span more than one timezone.
* For example, Germany has ['Europe/Berlin', 'Europe/Busingen'].
*
* @return string[]
*/
public function getTimezones(): array
{
return \DateTimeZone::listIdentifiers(\DateTimeZone::PER_COUNTRY, $this->countryCode);
}
/**
* Gets the locale.
*
* The country name is locale specific.
*/
public function getLocale(): string
{
return $this->locale;
}
}