Skip to content

Commit 1aeeed5

Browse files
authored
Release/3.1.0 (#22)
1 parent f304a69 commit 1aeeed5

6 files changed

Lines changed: 107 additions & 38 deletions

File tree

src/Alpha2Code.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,11 @@ public function getName(): string
260260
return $this->name;
261261
}
262262

263+
public function toString(): string
264+
{
265+
return $this->value;
266+
}
267+
263268
/**
264269
* Converts this Alpha-2 code to its corresponding Alpha-3 code.
265270
*

src/Alpha3Code.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,11 @@ public function getName(): string
260260
return $this->name;
261261
}
262262

263+
public function toString(): string
264+
{
265+
return $this->value;
266+
}
267+
263268
/**
264269
* Converts this Alpha-3 code to its corresponding Alpha-2 code.
265270
*

src/AlphaCode.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,10 @@ interface AlphaCode
1616
* @return string The name of the enum case representing the alpha code.
1717
*/
1818
public function getName(): string;
19+
20+
/**
21+
* Gets the alpha code value (e.g. 'BR' for Alpha-2, 'BRA' for Alpha-3).
22+
* @return string The alpha code value.
23+
*/
24+
public function toString(): string;
1925
}

src/Timezones.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,14 +90,14 @@ public function contains(string $iana): bool
9090
* Finds a Timezone by its IANA identifier.
9191
*
9292
* @param string $iana The IANA timezone identifier to search for (e.g. America/Sao_Paulo).
93-
* @return Timezone|null The matching Timezone, or null if not found in this country.
93+
* @return Timezone The matching Timezone, or UTC if not found in this country.
9494
*/
95-
public function findByIdentifier(string $iana): ?Timezone
95+
public function findByIdentifier(string $iana): Timezone
9696
{
9797
return array_find(
9898
$this->items,
9999
static fn(Timezone $timezone): bool => $timezone->value === $iana
100-
);
100+
) ?? Timezone::utc();
101101
}
102102

103103
/**

tests/CountryTest.php

Lines changed: 83 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,26 @@ public function testCountryAlpha3ConvertsToAlpha2Correctly(): void
122122
self::assertSame(Alpha3Code::SWITZERLAND, $country->alpha3);
123123
}
124124

125+
#[DataProvider('alphaCodeToStringDataProvider')]
126+
public function testCountryAlphaCodeToStringReturnsValue(
127+
Alpha2Code $alpha2,
128+
string $expectedAlpha2String,
129+
string $expectedAlpha3String
130+
): void {
131+
/** @Given a Country created from an Alpha-2 code */
132+
$country = Country::from(alphaCode: $alpha2);
133+
134+
/** @When calling toString on each alpha code */
135+
$alpha2String = $country->alpha2->toString();
136+
$alpha3String = $country->alpha3->toString();
137+
138+
/** @Then the Alpha-2 toString should return the two-letter code */
139+
self::assertSame($expectedAlpha2String, $alpha2String);
140+
141+
/** @And the Alpha-3 toString should return the three-letter code */
142+
self::assertSame($expectedAlpha3String, $alpha3String);
143+
}
144+
125145
public function testCountryFromStringWithAlpha2Code(): void
126146
{
127147
/** @Given a valid two-letter alpha code string */
@@ -219,23 +239,20 @@ public function testCountryTimezonesFindByIdentifierReturnsTimezone(): void
219239
/** @When searching for a known timezone identifier */
220240
$timezone = $country->timezones->findByIdentifier(iana: 'America/New_York');
221241

222-
/** @Then a Timezone object should be returned */
223-
self::assertInstanceOf(Timezone::class, $timezone);
224-
225-
/** @And its value should match the searched identifier */
242+
/** @Then the returned Timezone value should match the searched identifier */
226243
self::assertSame('America/New_York', $timezone->value);
227244
}
228245

229-
public function testCountryTimezonesFindByIdentifierReturnsNullWhenNotFound(): void
246+
public function testCountryTimezonesFindByIdentifierReturnsUtcWhenNotFound(): void
230247
{
231248
/** @Given a Country created from Alpha-2 code DE (Germany) */
232249
$country = Country::from(alphaCode: Alpha2Code::GERMANY);
233250

234251
/** @When searching for a timezone that does not belong to Germany */
235252
$timezone = $country->timezones->findByIdentifier(iana: 'Asia/Tokyo');
236253

237-
/** @Then null should be returned */
238-
self::assertNull($timezone);
254+
/** @Then the fallback UTC timezone should be returned */
255+
self::assertSame('UTC', $timezone->value);
239256
}
240257

241258
public function testCountryTimezonesCountMatchesAllSize(): void
@@ -299,7 +316,7 @@ public function testCountryWithMultipleTimezonesPreservesAll(): void
299316

300317
/** @And each timezone should be findable by its identifier */
301318
foreach ($country->timezones->all() as $timezone) {
302-
self::assertNotNull($country->timezones->findByIdentifier(iana: $timezone->value));
319+
self::assertSame($timezone->value, $country->timezones->findByIdentifier(iana: $timezone->value)->value);
303320
}
304321
}
305322

@@ -316,7 +333,7 @@ public function testCountryTimezonesCreatedFromSameCodeAreConsistent(): void
316333
self::assertSame($first->timezones->count(), $second->timezones->count());
317334
}
318335

319-
public function testCountryWhenInvalidTimezoneIdentifier(): void
336+
public function testCountryWhenInvalidTimezone(): void
320337
{
321338
/** @Given a non-empty string that is not a valid IANA timezone */
322339
$invalidIdentifier = 'Invalid/Timezone';
@@ -370,48 +387,48 @@ public function testCountryWhenInvalidAlphaCodeImplementation(): void
370387
public static function alphaCodeObjectsDataProvider(): array
371388
{
372389
return [
373-
'Alpha2 with custom name' => [
374-
'alphaCode' => Alpha2Code::UNITED_STATES_OF_AMERICA,
375-
'name' => 'United States',
376-
'expectedName' => 'United States',
377-
'expectedAlpha2' => Alpha2Code::UNITED_STATES_OF_AMERICA,
378-
'expectedAlpha3' => Alpha3Code::UNITED_STATES_OF_AMERICA,
379-
],
380-
'Alpha3 with custom name' => [
381-
'alphaCode' => Alpha3Code::UNITED_STATES_OF_AMERICA,
382-
'name' => 'United States',
383-
'expectedName' => 'United States',
384-
'expectedAlpha2' => Alpha2Code::UNITED_STATES_OF_AMERICA,
385-
'expectedAlpha3' => Alpha3Code::UNITED_STATES_OF_AMERICA,
386-
],
387390
'Alpha2 with null name' => [
388391
'alphaCode' => Alpha2Code::BRAZIL,
389392
'name' => null,
390393
'expectedName' => 'Brazil',
391394
'expectedAlpha2' => Alpha2Code::BRAZIL,
392-
'expectedAlpha3' => Alpha3Code::BRAZIL,
395+
'expectedAlpha3' => Alpha3Code::BRAZIL
393396
],
394397
'Alpha3 with null name' => [
395398
'alphaCode' => Alpha3Code::BRAZIL,
396399
'name' => null,
397400
'expectedName' => 'Brazil',
398401
'expectedAlpha2' => Alpha2Code::BRAZIL,
399-
'expectedAlpha3' => Alpha3Code::BRAZIL,
402+
'expectedAlpha3' => Alpha3Code::BRAZIL
403+
],
404+
'Alpha2 with custom name' => [
405+
'alphaCode' => Alpha2Code::UNITED_STATES_OF_AMERICA,
406+
'name' => 'United States',
407+
'expectedName' => 'United States',
408+
'expectedAlpha2' => Alpha2Code::UNITED_STATES_OF_AMERICA,
409+
'expectedAlpha3' => Alpha3Code::UNITED_STATES_OF_AMERICA
410+
],
411+
'Alpha3 with custom name' => [
412+
'alphaCode' => Alpha3Code::UNITED_STATES_OF_AMERICA,
413+
'name' => 'United States',
414+
'expectedName' => 'United States',
415+
'expectedAlpha2' => Alpha2Code::UNITED_STATES_OF_AMERICA,
416+
'expectedAlpha3' => Alpha3Code::UNITED_STATES_OF_AMERICA
400417
],
401418
'Alpha2 GB with full name' => [
402419
'alphaCode' => Alpha2Code::UNITED_KINGDOM_OF_GREAT_BRITAIN_AND_NORTHERN_IRELAND,
403420
'name' => 'United Kingdom of Great Britain and Northern Ireland',
404421
'expectedName' => 'United Kingdom of Great Britain and Northern Ireland',
405422
'expectedAlpha2' => Alpha2Code::UNITED_KINGDOM_OF_GREAT_BRITAIN_AND_NORTHERN_IRELAND,
406-
'expectedAlpha3' => Alpha3Code::UNITED_KINGDOM_OF_GREAT_BRITAIN_AND_NORTHERN_IRELAND,
423+
'expectedAlpha3' => Alpha3Code::UNITED_KINGDOM_OF_GREAT_BRITAIN_AND_NORTHERN_IRELAND
407424
],
408425
'Alpha3 GBR with null name' => [
409426
'alphaCode' => Alpha3Code::UNITED_KINGDOM_OF_GREAT_BRITAIN_AND_NORTHERN_IRELAND,
410427
'name' => null,
411428
'expectedName' => 'United Kingdom of Great Britain and Northern Ireland',
412429
'expectedAlpha2' => Alpha2Code::UNITED_KINGDOM_OF_GREAT_BRITAIN_AND_NORTHERN_IRELAND,
413-
'expectedAlpha3' => Alpha3Code::UNITED_KINGDOM_OF_GREAT_BRITAIN_AND_NORTHERN_IRELAND,
414-
],
430+
'expectedAlpha3' => Alpha3Code::UNITED_KINGDOM_OF_GREAT_BRITAIN_AND_NORTHERN_IRELAND
431+
]
415432
];
416433
}
417434

@@ -423,43 +440,74 @@ public static function alphaCodeStringsDataProvider(): array
423440
'name' => 'United States',
424441
'expectedName' => 'United States',
425442
'expectedAlpha2' => Alpha2Code::UNITED_STATES_OF_AMERICA,
426-
'expectedAlpha3' => Alpha3Code::UNITED_STATES_OF_AMERICA,
443+
'expectedAlpha3' => Alpha3Code::UNITED_STATES_OF_AMERICA
427444
],
428445
'Alpha3 string USA' => [
429446
'alphaCode' => 'USA',
430447
'name' => 'United States',
431448
'expectedName' => 'United States',
432449
'expectedAlpha2' => Alpha2Code::UNITED_STATES_OF_AMERICA,
433-
'expectedAlpha3' => Alpha3Code::UNITED_STATES_OF_AMERICA,
450+
'expectedAlpha3' => Alpha3Code::UNITED_STATES_OF_AMERICA
434451
],
435452
'Alpha2 string with null name' => [
436453
'alphaCode' => 'BR',
437454
'name' => null,
438455
'expectedName' => 'Brazil',
439456
'expectedAlpha2' => Alpha2Code::BRAZIL,
440-
'expectedAlpha3' => Alpha3Code::BRAZIL,
457+
'expectedAlpha3' => Alpha3Code::BRAZIL
441458
],
442459
'Alpha3 string with null name' => [
443460
'alphaCode' => 'BRA',
444461
'name' => null,
445462
'expectedName' => 'Brazil',
446463
'expectedAlpha2' => Alpha2Code::BRAZIL,
447-
'expectedAlpha3' => Alpha3Code::BRAZIL,
464+
'expectedAlpha3' => Alpha3Code::BRAZIL
448465
],
449466
'Alpha2 string with custom name' => [
450467
'alphaCode' => 'BR',
451468
'name' => 'Brasil',
452469
'expectedName' => 'Brasil',
453470
'expectedAlpha2' => Alpha2Code::BRAZIL,
454-
'expectedAlpha3' => Alpha3Code::BRAZIL,
471+
'expectedAlpha3' => Alpha3Code::BRAZIL
455472
],
456473
'Alpha3 string with custom name' => [
457474
'alphaCode' => 'BRA',
458475
'name' => 'Brasil',
459476
'expectedName' => 'Brasil',
460477
'expectedAlpha2' => Alpha2Code::BRAZIL,
461-
'expectedAlpha3' => Alpha3Code::BRAZIL,
478+
'expectedAlpha3' => Alpha3Code::BRAZIL
479+
]
480+
];
481+
}
482+
483+
public static function alphaCodeToStringDataProvider(): array
484+
{
485+
return [
486+
'Japan' => [
487+
'alpha2' => Alpha2Code::JAPAN,
488+
'expectedAlpha2String' => 'JP',
489+
'expectedAlpha3String' => 'JPN'
490+
],
491+
'Brazil' => [
492+
'alpha2' => Alpha2Code::BRAZIL,
493+
'expectedAlpha2String' => 'BR',
494+
'expectedAlpha3String' => 'BRA'
495+
],
496+
'Germany' => [
497+
'alpha2' => Alpha2Code::GERMANY,
498+
'expectedAlpha2String' => 'DE',
499+
'expectedAlpha3String' => 'DEU'
500+
],
501+
'Switzerland' => [
502+
'alpha2' => Alpha2Code::SWITZERLAND,
503+
'expectedAlpha2String' => 'CH',
504+
'expectedAlpha3String' => 'CHE'
462505
],
506+
'United States' => [
507+
'alpha2' => Alpha2Code::UNITED_STATES_OF_AMERICA,
508+
'expectedAlpha2String' => 'US',
509+
'expectedAlpha3String' => 'USA'
510+
]
463511
];
464512
}
465513

@@ -469,7 +517,7 @@ public static function invalidAlphaCodeStringsDataProvider(): array
469517
'Single character' => ['alphaCode' => 'X'],
470518
'Two characters' => ['alphaCode' => 'XY'],
471519
'Three characters' => ['alphaCode' => 'XYZ'],
472-
'Four characters' => ['alphaCode' => 'XYZ1'],
520+
'Four characters' => ['alphaCode' => 'XYZ1']
473521
];
474522
}
475523
}

tests/Models/AlphaCodeXpto.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,9 @@ public function getName(): string
1414
{
1515
return $this->value;
1616
}
17+
18+
public function toString(): string
19+
{
20+
return $this->value;
21+
}
1722
}

0 commit comments

Comments
 (0)