Skip to content

Commit 062a9d0

Browse files
committed
Add unknown and invalid codes
1 parent b5cff21 commit 062a9d0

File tree

2 files changed

+51
-12
lines changed

2 files changed

+51
-12
lines changed

src/Timezone.php

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -118,18 +118,36 @@ class Timezone
118118
];
119119

120120
/*
121-
* Codes Europe
121+
* Codes Pacific
122122
*/
123123
public const COUNTRY_CODE_PG = 'PG';
124124

125125
/*
126-
* Countries Europe
126+
* Countries Pacific
127127
*/
128128
public const COUNTRY_NAME_PG = [
129129
self::DE_DE => 'Papua-Neuguinea',
130130
self::EN_GB => 'Papua New Guinea',
131131
];
132132

133+
/*
134+
* Codes Unknown/Invalid
135+
*/
136+
public const COUNTRY_CODE_UK = 'UK';
137+
public const COUNTRY_CODE_IV = 'IV';
138+
139+
/*
140+
* Countries Unknown/Invalid
141+
*/
142+
public const COUNTRY_NAME_UK = [
143+
self::DE_DE => 'Unbekannt',
144+
self::EN_GB => 'Unknown',
145+
];
146+
public const COUNTRY_NAME_IV = [
147+
self::DE_DE => 'Ungültig',
148+
self::EN_GB => 'Invalid',
149+
];
150+
133151
private const COUNTRY_NAMES = [
134152
/*
135153
* Africa
@@ -164,6 +182,12 @@ class Timezone
164182
* Pacific
165183
*/
166184
self::COUNTRY_CODE_PG => self::COUNTRY_NAME_PG,
185+
186+
/*
187+
* Unknown/Invalid
188+
*/
189+
self::COUNTRY_CODE_UK => self::COUNTRY_NAME_UK,
190+
self::COUNTRY_CODE_IV => self::COUNTRY_NAME_IV,
167191
];
168192

169193
/**
@@ -176,21 +200,31 @@ public function __construct(protected string $timezone)
176200
/**
177201
* Returns the country of given timezone.
178202
*
179-
* @return string|null
203+
* @return string
180204
*/
181-
public function getCountryCode(): ?string
205+
public function getCountryCode(): string
182206
{
207+
if ($this->timezone === '') {
208+
return self::COUNTRY_CODE_IV;
209+
}
210+
183211
try {
184212
$dateTimeZone = new DateTimeZone($this->timezone);
185213
$location = $dateTimeZone->getLocation();
186214

187215
if ($location === false) {
188-
return null;
216+
return self::COUNTRY_CODE_IV;
189217
}
190218

191-
return $location['country_code'];
219+
$countryCode = $location['country_code'];
220+
221+
if ($countryCode === '??') {
222+
return self::COUNTRY_CODE_UK;
223+
}
224+
225+
return $countryCode;
192226
} catch (Exception) {
193-
return null;
227+
return self::COUNTRY_CODE_IV;
194228
}
195229
}
196230

@@ -200,16 +234,11 @@ public function getCountryCode(): ?string
200234
* @param string $language
201235
* @return string|null
202236
* @throws ArrayKeyNotFoundException
203-
* @throws CaseInvalidException
204237
*/
205238
public function getCountryName(string $language = self::EN_GB): ?string
206239
{
207240
$countryCode = $this->getCountryCode();
208241

209-
if (is_null($countryCode)) {
210-
throw new CaseInvalidException($this->timezone, ['Valid timezone']);
211-
}
212-
213242
if (!array_key_exists($countryCode, self::COUNTRY_NAMES)) {
214243
throw new ArrayKeyNotFoundException($countryCode);
215244
}

tests/Unit/TimezoneTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,16 @@ public function dataProvider(): array
114114
*/
115115
[++$number, 'getCountryCode', null, 'Pacific/Port_Moresby', Timezone::COUNTRY_CODE_PG],
116116
[++$number, 'getCountryName', null, 'Pacific/Port_Moresby', Timezone::COUNTRY_NAME_PG[Timezone::EN_GB]],
117+
118+
/**
119+
* getCountry/getCountryName: Unknown/Invalid
120+
*/
121+
[++$number, 'getCountryCode', null, '', Timezone::COUNTRY_CODE_IV],
122+
[++$number, 'getCountryName', null, '', Timezone::COUNTRY_NAME_IV[Timezone::EN_GB]],
123+
[++$number, 'getCountryCode', null, 'Europe/Dresden', Timezone::COUNTRY_CODE_IV],
124+
[++$number, 'getCountryName', null, 'Europe/Dresden', Timezone::COUNTRY_NAME_IV[Timezone::EN_GB]],
125+
[++$number, 'getCountryCode', null, 'Asia/Ashkhabad', Timezone::COUNTRY_CODE_UK],
126+
[++$number, 'getCountryName', null, 'Asia/Ashkhabad', Timezone::COUNTRY_NAME_UK[Timezone::EN_GB]],
117127
];
118128
}
119129
}

0 commit comments

Comments
 (0)