Skip to content

Commit daa0bb7

Browse files
committed
Add Country::getName functionality
1 parent a2769b8 commit daa0bb7

File tree

5 files changed

+218
-6
lines changed

5 files changed

+218
-6
lines changed
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@
1414
namespace Ixnode\PhpTimezone\Constants;
1515

1616
/**
17-
* Class Country
17+
* Class CountryAll
1818
*
1919
* @author Björn Hempel <bjoern@hempel.li>
2020
* @version 0.1.0 (2023-07-01)
2121
* @since 0.1.0 (2023-07-01) First version.
2222
*/
23-
class Country
23+
class CountryAll
2424
{
2525
public const COUNTRY_NAMES = [
2626
/*

src/Country.php

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the ixnode/php-timezone project.
5+
*
6+
* (c) Björn Hempel <https://www.hempel.li/>
7+
*
8+
* For the full copyright and license information, please view the LICENSE.md
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Ixnode\PhpTimezone;
15+
16+
use Ixnode\PhpException\ArrayType\ArrayKeyNotFoundException;
17+
use Ixnode\PhpTimezone\Constants\CountryAll;
18+
use Ixnode\PhpTimezone\Constants\CountryUnknown;
19+
use Ixnode\PhpTimezone\Constants\Language;
20+
use Ixnode\PhpTimezone\Tests\Unit\CountryTest;
21+
22+
/**
23+
* Class Country
24+
*
25+
* @author Björn Hempel <bjoern@hempel.li>
26+
* @version 0.1.0 (2023-07-01)
27+
* @since 0.1.0 (2023-07-01) First version.
28+
* @link CountryTest
29+
*/
30+
class Country
31+
{
32+
/**
33+
* @param string $country
34+
*/
35+
public function __construct(protected string $country)
36+
{
37+
}
38+
39+
/**
40+
* Returns the language string of given values.
41+
*
42+
* @param array<string, string> $values
43+
* @param string $language
44+
* @return string
45+
* @throws ArrayKeyNotFoundException
46+
*/
47+
private function getLanguage(array $values, string $language): string
48+
{
49+
if (!array_key_exists($language, $values)) {
50+
throw new ArrayKeyNotFoundException($language);
51+
}
52+
53+
return $values[$language];
54+
}
55+
56+
/**
57+
* Returns the name of country.
58+
*
59+
* @param string $language
60+
* @return string
61+
* @throws ArrayKeyNotFoundException
62+
*/
63+
public function getName(string $language = Language::EN_GB): string
64+
{
65+
if ($this->country === '') {
66+
return $this->getLanguage(CountryUnknown::COUNTRY_NAME_IV, $language);
67+
}
68+
69+
if (!array_key_exists($this->country, CountryAll::COUNTRY_NAMES)) {
70+
return $this->getLanguage(CountryUnknown::COUNTRY_NAME_UK, $language);
71+
}
72+
73+
return $this->getLanguage(CountryAll::COUNTRY_NAMES[$this->country], $language);
74+
}
75+
}

src/Timezone.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
use DateTimeZone;
1717
use Exception;
1818
use Ixnode\PhpException\ArrayType\ArrayKeyNotFoundException;
19-
use Ixnode\PhpTimezone\Constants\Country;
19+
use Ixnode\PhpTimezone\Constants\CountryAll;
2020
use Ixnode\PhpTimezone\Constants\CountryUnknown;
2121
use Ixnode\PhpTimezone\Constants\Language;
2222
use Ixnode\PhpTimezone\Tests\Unit\TimezoneTest;
@@ -80,11 +80,11 @@ public function getCountryName(string $language = Language::EN_GB): ?string
8080
{
8181
$countryCode = $this->getCountryCode();
8282

83-
if (!array_key_exists($countryCode, Country::COUNTRY_NAMES)) {
83+
if (!array_key_exists($countryCode, CountryAll::COUNTRY_NAMES)) {
8484
throw new ArrayKeyNotFoundException($countryCode);
8585
}
8686

87-
$countryName = Country::COUNTRY_NAMES[$countryCode];
87+
$countryName = CountryAll::COUNTRY_NAMES[$countryCode];
8888

8989
if (!array_key_exists($language, $countryName)) {
9090
throw new ArrayKeyNotFoundException($language);

tests/Unit/CountryTest.php

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the ixnode/php-timezone project.
5+
*
6+
* (c) Björn Hempel <https://www.hempel.li/>
7+
*
8+
* For the full copyright and license information, please view the LICENSE.md
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Ixnode\PhpTimezone\Tests\Unit;
15+
16+
use Ixnode\PhpTimezone\Constants\CountryAfrica;
17+
use Ixnode\PhpTimezone\Constants\CountryAmerica;
18+
use Ixnode\PhpTimezone\Constants\CountryAsia;
19+
use Ixnode\PhpTimezone\Constants\CountryAustralia;
20+
use Ixnode\PhpTimezone\Constants\CountryEurope;
21+
use Ixnode\PhpTimezone\Constants\CountryPacific;
22+
use Ixnode\PhpTimezone\Constants\CountryUnknown;
23+
use Ixnode\PhpTimezone\Constants\Language;
24+
use Ixnode\PhpTimezone\Country;
25+
use PHPUnit\Framework\TestCase;
26+
27+
/**
28+
* Class CountryTest
29+
*
30+
* @author Björn Hempel <bjoern@hempel.li>
31+
* @version 0.1.0 (2023-06-30)
32+
* @since 0.1.0 (2023-06-30) First version.
33+
* @link Country
34+
*/
35+
final class CountryTest extends TestCase
36+
{
37+
/**
38+
* Test wrapper.
39+
*
40+
* @dataProvider dataProvider
41+
*
42+
* @test
43+
* @testdox $number) Test Country: $method
44+
* @param int $number
45+
* @param string $method
46+
* @param string|null $parameter
47+
* @param string $given
48+
* @param string $expected
49+
*/
50+
public function wrapper(int $number, string $method, string|null $parameter, string $given, string $expected): void
51+
{
52+
/* Arrange */
53+
54+
/* Act */
55+
$timezone = new Country($given);
56+
$callback = [$timezone, $method];
57+
58+
/* Assert */
59+
$this->assertIsNumeric($number); // To avoid phpmd warning.
60+
$this->assertContains($method, get_class_methods(Country::class));
61+
$this->assertIsCallable($callback);
62+
63+
$result = match (true) {
64+
is_null($parameter) => $timezone->{$method}(),
65+
default => $timezone->{$method}($parameter),
66+
};
67+
68+
$this->assertSame($expected, $result);
69+
}
70+
71+
/**
72+
* Data provider.
73+
*
74+
* @return array<int, array<int, string|int|null>>
75+
*/
76+
public function dataProvider(): array
77+
{
78+
$number = 0;
79+
80+
return [
81+
82+
/**
83+
* getName: Africa
84+
*/
85+
[++$number, 'getName', null, CountryAfrica::COUNTRY_CODE_CI, CountryAfrica::COUNTRY_NAME_CI[Language::EN_GB]],
86+
[++$number, 'getName', null, CountryAfrica::COUNTRY_CODE_GH, CountryAfrica::COUNTRY_NAME_GH[Language::EN_GB]],
87+
88+
/**
89+
* getName: Africa
90+
*/
91+
[++$number, 'getName', null, CountryAmerica::COUNTRY_CODE_AR, CountryAmerica::COUNTRY_NAME_AR[Language::EN_GB]],
92+
93+
/**
94+
* getName: Asia
95+
*/
96+
[++$number, 'getName', null, CountryAsia::COUNTRY_CODE_AE, CountryAsia::COUNTRY_NAME_AE[Language::EN_GB]],
97+
[++$number, 'getName', null, CountryAsia::COUNTRY_CODE_KZ, CountryAsia::COUNTRY_NAME_KZ[Language::EN_GB]],
98+
[++$number, 'getName', null, CountryAsia::COUNTRY_CODE_RU, CountryAsia::COUNTRY_NAME_RU[Language::EN_GB]],
99+
100+
/**
101+
* getName: Australia
102+
*/
103+
[++$number, 'getName', null, CountryAustralia::COUNTRY_CODE_AU, CountryAustralia::COUNTRY_NAME_AU[Language::EN_GB]],
104+
105+
/**
106+
* getName: Europe
107+
*/
108+
[++$number, 'getName', null, CountryEurope::COUNTRY_CODE_AD, CountryEurope::COUNTRY_NAME_AD[Language::EN_GB]],
109+
[++$number, 'getName', null, CountryEurope::COUNTRY_CODE_BE, CountryEurope::COUNTRY_NAME_BE[Language::EN_GB]],
110+
[++$number, 'getName', null, CountryEurope::COUNTRY_CODE_CH, CountryEurope::COUNTRY_NAME_CH[Language::EN_GB]],
111+
[++$number, 'getName', null, CountryEurope::COUNTRY_CODE_CZ, CountryEurope::COUNTRY_NAME_CZ[Language::EN_GB]],
112+
[++$number, 'getName', null, CountryEurope::COUNTRY_CODE_DE, CountryEurope::COUNTRY_NAME_DE[Language::EN_GB]],
113+
[++$number, 'getName', Language::EN_GB, CountryEurope::COUNTRY_CODE_DE, CountryEurope::COUNTRY_NAME_DE[Language::EN_GB]],
114+
[++$number, 'getName', Language::DE_DE, CountryEurope::COUNTRY_CODE_DE, CountryEurope::COUNTRY_NAME_DE[Language::DE_DE]],
115+
[++$number, 'getName', null, CountryEurope::COUNTRY_CODE_DK, CountryEurope::COUNTRY_NAME_DK[Language::EN_GB]],
116+
[++$number, 'getName', null, CountryEurope::COUNTRY_CODE_ES, CountryEurope::COUNTRY_NAME_ES[Language::EN_GB]],
117+
[++$number, 'getName', null, CountryEurope::COUNTRY_CODE_FR, CountryEurope::COUNTRY_NAME_FR[Language::EN_GB]],
118+
[++$number, 'getName', null, CountryEurope::COUNTRY_CODE_IT, CountryEurope::COUNTRY_NAME_IT[Language::EN_GB]],
119+
[++$number, 'getName', null, CountryEurope::COUNTRY_CODE_LU, CountryEurope::COUNTRY_NAME_LU[Language::EN_GB]],
120+
[++$number, 'getName', null, CountryEurope::COUNTRY_CODE_NL, CountryEurope::COUNTRY_NAME_NL[Language::EN_GB]],
121+
[++$number, 'getName', null, CountryEurope::COUNTRY_CODE_PL, CountryEurope::COUNTRY_NAME_PL[Language::EN_GB]],
122+
[++$number, 'getName', null, CountryEurope::COUNTRY_CODE_RS, CountryEurope::COUNTRY_NAME_RS[Language::EN_GB]],
123+
[++$number, 'getName', null, CountryEurope::COUNTRY_CODE_SK, CountryEurope::COUNTRY_NAME_SK[Language::EN_GB]],
124+
125+
/**
126+
* getName: Pacific
127+
*/
128+
[++$number, 'getName', null, CountryPacific::COUNTRY_CODE_PG, CountryPacific::COUNTRY_NAME_PG[Language::EN_GB]],
129+
130+
/**
131+
* getName: Unknown/Invalid
132+
*/
133+
[++$number, 'getName', null, '', CountryUnknown::COUNTRY_NAME_IV[Language::EN_GB]],
134+
[++$number, 'getName', null, 'XX', CountryUnknown::COUNTRY_NAME_UK[Language::EN_GB]],
135+
];
136+
}
137+
}

tests/Unit/TimezoneTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ final class TimezoneTest extends TestCase
4040
* @dataProvider dataProvider
4141
*
4242
* @test
43-
* @testdox $number) Test SizeByte: $method
43+
* @testdox $number) Test Timezone: $method
4444
* @param int $number
4545
* @param string $method
4646
* @param string|null $parameter

0 commit comments

Comments
 (0)