From 950ab723258cd1e860ed953db9cc4c1456ecde0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9ia=20Bohner?= Date: Sat, 25 May 2024 21:46:41 -0300 Subject: [PATCH] Add option to pass multiple country codes or regions Co-authored-by: flashmediasolutions --- README.md | 51 ++++++++++++++++++++--------- src/Concerns/HasTimezoneOptions.php | 47 +++++++++++++++++--------- 2 files changed, 68 insertions(+), 30 deletions(-) diff --git a/README.md b/README.md index e055945..3509493 100644 --- a/README.md +++ b/README.md @@ -58,6 +58,8 @@ public static function form(Form $form): Form } ``` +##### List timezones by country + To list only the timezones for a country, you can pass the country code to `->byCountry()` method. For example, to list only United States timezones: ```php @@ -65,7 +67,16 @@ TimezoneSelect::make('timezone') ->byCountry('US') ``` -It's also possible to list the timezones for a region using `->byRegion()` method. You can specify a region with a [Region enum value](src/Enums/Region.php): +You can also pass an array with more than one country code: + +```php +TimezoneSelect::make('timezone') + ->byCountry(['US', 'AU']) +``` + +##### List timezones by region + +To list the timezones for a region use the `->byRegion()` method. You can specify a region with a [Region enum value](src/Enums/Region.php): ```php use Tapp\FilamentTimezoneField\Enums\Region; @@ -83,25 +94,35 @@ TimezoneSelect::make('timezone') ->byRegion(DateTimeZone::AUSTRALIA) ``` -Also all [Filament select field](https://filamentphp.com/docs/2.x/forms/fields#select) methods are available to use: +It's also possible to pass an array with more than one region: ```php -use Tapp\FilamentTimezoneField\Forms\Components\TimezoneSelect; +use Tapp\FilamentTimezoneField\Enums\Region; -public static function form(Form $form): Form -{ - return $form - ->schema([ - // ... - TimezoneSelect::make('timezone') - ->searchable() - ->required(), - // ... - ]); -} +TimezoneSelect::make('timezone') + ->byRegion([Region::Australia, Region::America]) ``` -Optionally hide either timezone offsets or timezone names, depending on your use case: +> [!TIP] +> All [Filament select field](https://filamentphp.com/docs/2.x/forms/fields#select) methods are available to use: +> +> ```php +> use Tapp\FilamentTimezoneField\Forms\Components\TimezoneSelect; +> +> public static function form(Form $form): Form +> { +> return $form +> ->schema([ +> // ... +> TimezoneSelect::make('timezone') +> ->searchable() +> ->required(), +> // ... +> ]); +> } +> ``` + +Optionally, hide either timezone offsets or timezone names, depending on your use case: ![Filament Timezone Display Options](https://raw.githubusercontent.com/TappNetwork/filament-timezone-field/main/docs/hide-timezone-offset.png) diff --git a/src/Concerns/HasTimezoneOptions.php b/src/Concerns/HasTimezoneOptions.php index 64db19a..a045f46 100644 --- a/src/Concerns/HasTimezoneOptions.php +++ b/src/Concerns/HasTimezoneOptions.php @@ -4,13 +4,14 @@ use DateTime; use DateTimeZone; +use Illuminate\Support\Arr; use Tapp\FilamentTimezoneField\Enums\Region; trait HasTimezoneOptions { - protected string|Closure|null $byCountry = null; + protected array|string|Closure|null $byCountry = null; - protected Region|int|Closure|null $byRegion = null; + protected array|Region|int|Closure|null $byRegion = null; public function getOptions(): array { @@ -50,43 +51,59 @@ public function getTimezones(): array return $data; } - public function byCountry(string|Closure|null $countryCode): static + public function byCountry(array|string|Closure|null $countryCode): static { $this->byCountry = $countryCode; return $this; } - public function getByCountry(): ?string + public function getByCountry(): array|string|null { return $this->evaluate($this->byCountry); } - public function byRegion(Region|int|Closure|null $region): static + public function byRegion(array|Region|int|Closure|null $region): static { $this->byRegion = $region; return $this; } - public function getByRegion(): Region|int|null + public function getByRegion(): array|Region|int|null { return $this->evaluate($this->byRegion); } - protected function listTimezonesByCountry($countryCode) + protected function listTimezonesByCountry(array|string $countryCodes) { - return DateTimeZone::listIdentifiers( - timezoneGroup: DateTimeZone::PER_COUNTRY, - countryCode: $countryCode, - ); + $countryCodes = Arr::wrap($countryCodes); + + $timezones = []; + + foreach ($countryCodes as $countryCode) { + $timezones = array_merge($timezones, DateTimeZone::listIdentifiers( + timezoneGroup: DateTimeZone::PER_COUNTRY, + countryCode: $countryCode, + )); + } + + return $timezones; } - protected function listTimezonesByRegion($region) + protected function listTimezonesByRegion(array|Region|int $regions) { - return DateTimeZone::listIdentifiers( - timezoneGroup: $region?->value ?? $region, - ); + $regions = Arr::wrap($regions); + + $timezones = []; + + foreach ($regions as $region) { + $timezones = array_merge($timezones, DateTimeZone::listIdentifiers( + timezoneGroup: $region?->value ?? $region, + )); + } + + return $timezones; } protected function listAllTimezones()