Skip to content

Commit

Permalink
Merge pull request #2 from juanparati/5.x
Browse files Browse the repository at this point in the history
5.x
  • Loading branch information
juanparati authored Apr 8, 2024
2 parents 722a634 + d43e8b5 commit 3c9da06
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 17 deletions.
58 changes: 43 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ This library provides the following ISOs and codes:
- [ISO 4217](https://en.wikipedia.org/wiki/ISO_4217) (Currency codes)
- [ISO 639-1](https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes) (Language codes)
- [International dialing codes](https://en.wikipedia.org/wiki/List_of_country_calling_codes)
- [Olson Timezones](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones)
- [Unicode flags](https://en.wikipedia.org/wiki/Regional_indicator_symbol)
- [European Union Members](https://european-union.europa.eu/principles-countries-history/country-profiles_en)

Expand Down Expand Up @@ -64,24 +65,32 @@ It returns something like this:
[
...
"AL"=> [
"alpha2" => "AL",
"alpha3" => "ALB",
"numeric" => "008",
"tld" => ".al",
"alpha2" => "ES",
"alpha3" => "ESP",
"numeric" => "724",
"tld" => ".es",
"currencies" => [
'ALL'
"EUR",
],
"languages" => [
"SQ",
"languages" => [
"ES",
"CA",
"GL",
"EU",
],
"continents" => [
"EU",
"EU",
],
"name" => "Albania",
"capital" => "Tirana",
"flag" => "🇦🇱",
"phone_code" => "355",
"eu_member" => false
"capital" => "Madrid",
"flag" => "🇪🇸",
"phone_code" => "34",
"eu_member" => true,
"name" => "Spain",
"timezones" => [
"Europe/Madrid",
"Africa/Ceuta",
"Atlantic/Canary",
]
]
...
];
Expand Down Expand Up @@ -218,6 +227,9 @@ It returns something like:
"languages" => [ …1],
"continents" => [ …1],
"name" => "Andorra",
"timezones" => [
"Europe/Andorra"
]
],
...
],
Expand Down Expand Up @@ -342,7 +354,12 @@ returns the following:
"capital" => "Lisboa",
"flag" => "🇵🇹",
"phone_code" => "351",
"eu_member" => true
"eu_member" => true,
"timezones" => [
"Europe/Lisbon",
"Atlantic/Azores",
"Atlantic/Madeira",
],
]

instead of:
Expand All @@ -365,7 +382,12 @@ instead of:
"capital" => "Lisboa",
"flag" => "🇵🇹",
"phone_code" => "351",
"eu_member" => true
"eu_member" => true,
"timezones" => [
"Europe/Lisbon",
"Atlantic/Azores",
"Atlantic/Madeira",
],
]

The node resolutions works with the others models like "currencies", "languages", etc.
Expand Down Expand Up @@ -396,6 +418,12 @@ $iso = new ISOCodes(new ISOCodes(defaultResolutions: [
]);
```

## Main language and timezone

- The more spoken language is displayed first in the list.
- The country capital timezone is displayed first in the list.


## Custom dataset and locales

It's possible to register custom datasets and locales during the ISOCodes instantiation.
Expand Down
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 3c9da06

Please sign in to comment.