Skip to content

Commit

Permalink
feat(grouping): country grouping
Browse files Browse the repository at this point in the history
Adding in grouping by country
  • Loading branch information
awjudd committed Oct 26, 2023
1 parent b70ebb9 commit 5db593c
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ Thumbs.db
.phpunit.result.cache
report.junit.xml
coverage/
build/
build/
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
47 changes: 35 additions & 12 deletions src/TimezoneList.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -35,67 +36,89 @@ 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];
}

/**
* @throws TimezoneListReadonlyException
*/
public function offsetSet(mixed $offset, mixed $value)
public function offsetSet(mixed $offset, mixed $value): void
{
throw new TimezoneListReadonlyException($offset);
}

/**
* @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;
}
}
}
21 changes: 20 additions & 1 deletion tests/TimezoneListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,4 +154,23 @@ public function timezone_list_can_retrieve_check_if_an_offset_exists_and_it_does
// Assert
$this->assertTrue($result);
}
}

/**
* @test
*/
public function timezone_list_can_group_by_regions()
{
// Arrange
$subject = TimezoneList::groupByRegion();

// Act
$result = $subject['America'];

// Assert
$this->assertInstanceOf(
TimezoneList::class,
$result
);
}

}

0 comments on commit 5db593c

Please sign in to comment.