diff --git a/.gitignore b/.gitignore index a8c166a..fec6a2b 100644 --- a/.gitignore +++ b/.gitignore @@ -8,4 +8,4 @@ Thumbs.db .phpunit.result.cache report.junit.xml coverage/ -build/ \ No newline at end of file +build/ diff --git a/composer.json b/composer.json index a81f66f..7966bdf 100644 --- a/composer.json +++ b/composer.json @@ -17,7 +17,8 @@ } ], "require": { - "php": "^8.0|^8.1" + "php": "^8.0|^8.1|^8.2", + "ext-intl": "*" }, "require-dev": { "phpunit/phpunit": "^9.0" diff --git a/src/TimezoneList.php b/src/TimezoneList.php index cbcce45..3a243e8 100644 --- a/src/TimezoneList.php +++ b/src/TimezoneList.php @@ -7,6 +7,7 @@ use GamingEngine\Timezone\Exceptions\InvalidTimezoneException; use GamingEngine\Timezone\Exceptions\TimezoneListReadonlyException; use Iterator; +use League\ISO3166\ISO3166; class TimezoneList implements ArrayAccess, Iterator { @@ -35,25 +36,47 @@ public static function all(): TimezoneList } /** + * @return TimezoneList[] * @throws InvalidTimezoneException */ - public static function fromTimezoneGroup(int $timezoneGroup): TimezoneList + public static function groupByRegion(): array + { + $timezones = DateTimeZone::listIdentifiers(); + $countries = []; + $lists = []; + + foreach ($timezones as $timezone) { + $details = explode('/', $timezone); + $countries[$details[0]][] = new Timezone($timezone); + } + + foreach($countries as $country => $timezones) { + $lists[$country] = new TimezoneList($timezones); + } + + return $lists; + } + + /** + * @throws InvalidTimezoneException + */ + public static function fromTimezoneGroup(int $timezoneGroup, ?string $countryCode = null): TimezoneList { $timezones = []; - foreach (DateTimeZone::listIdentifiers($timezoneGroup) as $identifier) { + foreach (DateTimeZone::listIdentifiers($timezoneGroup, $countryCode) as $identifier) { $timezones[] = new Timezone($identifier); } return new self($timezones); } - public function offsetExists(mixed $offset) + public function offsetExists(mixed $offset): bool { return array_key_exists($offset, $this->timezones); } - public function offsetGet(mixed $offset) + public function offsetGet(mixed $offset): ?Timezone { return $this->timezones[$offset]; } @@ -61,7 +84,7 @@ public function offsetGet(mixed $offset) /** * @throws TimezoneListReadonlyException */ - public function offsetSet(mixed $offset, mixed $value) + public function offsetSet(mixed $offset, mixed $value): void { throw new TimezoneListReadonlyException($offset); } @@ -69,33 +92,33 @@ public function offsetSet(mixed $offset, mixed $value) /** * @throws TimezoneListReadonlyException */ - public function offsetUnset(mixed $offset) + public function offsetUnset(mixed $offset): void { throw new TimezoneListReadonlyException($offset); } - public function current() + public function current(): Timezone { return $this->timezones[$this->key]; } - public function next() + public function next(): void { $this->key++; } - public function key() + public function key(): int { return $this->key; } - public function valid() + public function valid(): bool { return array_key_exists($this->key, $this->timezones); } - public function rewind() + public function rewind(): void { $this->key = 0; } -} \ No newline at end of file +} diff --git a/tests/TimezoneListTest.php b/tests/TimezoneListTest.php index 51c37c1..95d718b 100644 --- a/tests/TimezoneListTest.php +++ b/tests/TimezoneListTest.php @@ -154,4 +154,23 @@ public function timezone_list_can_retrieve_check_if_an_offset_exists_and_it_does // Assert $this->assertTrue($result); } -} \ No newline at end of file + + /** + * @test + */ + public function timezone_list_can_group_by_regions() + { + // Arrange + $subject = TimezoneList::groupByRegion(); + + // Act + $result = $subject['America']; + + // Assert + $this->assertInstanceOf( + TimezoneList::class, + $result + ); + } + +}