Skip to content

Commit

Permalink
Added timezones.
Browse files Browse the repository at this point in the history
  • Loading branch information
juanparati committed Apr 8, 2024
1 parent e4e381c commit 49abc59
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 2 deletions.
74 changes: 74 additions & 0 deletions src/Data/Countries/CountryCodes.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Juanparati\ISOCodes\Data\Countries;

use Illuminate\Support\Arr;
use Juanparati\ISOCodes\Data\ISODataBase;

/**
Expand Down Expand Up @@ -3243,4 +3244,77 @@ class CountryCodes extends ISODataBase
'eu_member' => false,
],
];


/**
* Contains the capital timezones (or compatible) for countries that has more than 1 timezone.
* In case of autonomous regions it contains the most compatible timezone.
*
* @var array|string[]
*/
protected array $mainTimezones = [
'AR' => 'America/Argentina/Buenos_Aires',
'AU' => 'Australia/Sydney',
'BV' => 'Africa/Johannesburg',
'CA' => 'America/Toronto',
'CD' => 'Africa/Kinshasa',
'CL' => 'America/Santiago',
'CN' => 'Asia/Shanghai',
'EC' => 'America/Guayaquil',
'ES' => 'Europe/Madrid',
'FM' => 'Pacific/Chuuk',
'GL' => 'America/Godthab',
'HM' => 'Australia/Perth',
'ID' => 'Asia/Jakarta',
'KI' => 'Pacific/Tarawa',
'KZ' => 'Asia/Almaty',
'MH' => 'Pacific/Majuro',
'MN' => 'Asia/Ulaanbaatar',
'MX' => 'America/Mexico_City',
'MY' => 'Asia/Kuala_Lumpur',
'NZ' => 'Pacific/Auckland',
'PF' => 'Pacific/Tahiti',
'PG' => 'Pacific/Port_Moresby',
'PS' => 'Asia/Gaza',
'PT' => 'Europe/Lisbon',
'RU' => 'Europe/Moscow',
'TF' => 'Indian/Kerguelen',
'UA' => 'Europe/Kiev',
'UM' => 'Pacific/Midway',
'US' => 'America/New_York',
'UZ' => 'Asia/Samarkand',
];


/**
* Retrieve all the elements from the database.
*
* @return array
*/
public function all(): array
{
// Populate timezones.
// PHP already has the different timezones, so it's not required to create a complete list of timezones.
return Arr::map(
$this->db,
function (array $data, string $alpha2) {
$zones = \DateTimeZone::listIdentifiers(\DateTimeZone::PER_COUNTRY, $alpha2);

if ($this->mainTimezones[$alpha2] ?? null) {

if (count($zones)) {
$zones = array_filter($zones, fn($zone) => $zone !== $this->mainTimezones[$alpha2]);
array_unshift($zones, $this->mainTimezones[$alpha2]);
} else {
$zones = [$this->mainTimezones[$alpha2]];
}
}

$data['timezones'] = $zones;

return $data;
}
);

}
}
35 changes: 33 additions & 2 deletions tests/test/CountriesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
namespace Juanparati\ISOCodes\Tests\test;

use Illuminate\Support\Collection;
use Juanparati\ISOCodes\Data\Countries\CountryCodes;
use Juanparati\ISOCodes\Enums\NodeResolution;
use Juanparati\ISOCodes\ISOCodes;
use Juanparati\ISOCodes\Models\CountryModel;
use Juanparati\ISOCodes\Models\ModelRecord;
use PHPUnit\Framework\TestCase;

Expand All @@ -18,6 +18,11 @@ class CountriesTest extends TestCase
'alpha3' => 'ESP',
'numeric' => '724',
'tld' => '.es',
'timezones' => [
'Europe/Madrid',
'Africa/Ceuta',
'Atlantic/Canary'
]
];

protected array $testNodeCode = [
Expand Down Expand Up @@ -78,6 +83,7 @@ public function testAllCountries() {
$this->assertArrayHasKey('currencies', $country);
$this->assertArrayHasKey('continents', $country);
$this->assertArrayHasKey('name', $country);
$this->assertArrayHasKey('timezones', $country);
}
}

Expand Down Expand Up @@ -202,11 +208,36 @@ public function testCurrencyAsNumber() {
}


/**
* Test continent search.
*
* @return void
*/
public function testContinentSearch() {
$this->assertCount(2, (new ISOCodes())->countries()->whereContinent(['AS', 'EU'], true));
}


/**
* Test that main timezones are correct.
*
* @return void
*/
public function testMainTimezone() {
$iso = new ISOCodes();
$countryCodes = new CountryCodes();

$reflectedClass = new \ReflectionClass($countryCodes);
$reflection = $reflectedClass->getProperty('mainTimezones');
$mainTimezones = $reflection->getValue($countryCodes);

foreach ($mainTimezones as $alpha2 => $timezone) {
$timezones = $iso->countries()->findByAlpha2($alpha2)->timezones;
$this->assertEquals($timezone, $timezones[0]);
}
}


/**
* Assert expected country.
*
Expand All @@ -218,4 +249,4 @@ protected function assertCountry(array $expected, \ArrayAccess|ModelRecord $coun
$this->assertEquals($country[$key], $value);
}

}
}

0 comments on commit 49abc59

Please sign in to comment.