From 6811be4dcd4383a316c9c998911719c8beb53854 Mon Sep 17 00:00:00 2001 From: joy2362 Date: Fri, 28 Jun 2024 13:38:08 +0600 Subject: [PATCH] fix: update timezoneService --- src/TimeZoneService.php | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/src/TimeZoneService.php b/src/TimeZoneService.php index f92d2eb..7fa2e7d 100644 --- a/src/TimeZoneService.php +++ b/src/TimeZoneService.php @@ -3,7 +3,9 @@ namespace Joy2362\PhpTimezone; use DateTime; +use Exception; use DateTimeZone; +use Illuminate\Support\Facades\Config; class TimeZoneService { @@ -53,11 +55,13 @@ public function getSupportedTimeZone(): array public function list(): array { $list = []; - $label = config('Timezone.LABEL_FIELD_NAME') ?? 'label'; - $value = config('Timezone.VALUE_FIELD_NAME') ?? 'value'; + + $label = Config::get('Timezone.LABEL_FIELD_NAME','label'); + $value = Config::get('Timezone.VALUE_FIELD_NAME','value'); foreach ($this->regions as $region) { $timezones = DateTimeZone::listIdentifiers($region); + $data = []; foreach ($timezones as $timezone) { $data[$label] = $this->getLabel($timezone); $data[$value] = $timezone; @@ -106,8 +110,8 @@ public function listByRegion($region): array return []; } $list = []; - $label = config('Timezone.LABEL_FIELD_NAME') ?? 'label'; - $value = config('Timezone.VALUE_FIELD_NAME') ?? 'value'; + $label = Config::get('Timezone.LABEL_FIELD_NAME','label'); + $value = Config::get('Timezone.VALUE_FIELD_NAME','value'); $timezones = DateTimeZone::listIdentifiers($this->regions[$region]); foreach ($timezones as $timezone) { @@ -148,10 +152,11 @@ private function getLabel($timezone): string $time = new DateTime(null, new DateTimeZone($timezone)); $time_diff = $this->getTimeDiff($time); $zone = $this->getZone($time); - $defaultTimeZone = config('Timezone.DEFAULT_TIME_ZONE') ?? 'GMT'; + + $defaultTimeZone = Config::get('Timezone.DEFAULT_TIME_ZONE' , 'GMT'); $defaultTimeZone = in_array($defaultTimeZone, $this->supportedTimeZone) ? $defaultTimeZone : $this->supportedTimeZone[0]; return "({$defaultTimeZone} {$time_diff}) {$zone}"; - } catch (\Exception $ex) { + } catch (Exception $ex) { return ''; } } @@ -162,7 +167,7 @@ private function getLabel($timezone): string */ private function getTimeDiff($time): string { - $time_diff_symbol = config('Timezone.TIME_DIFF_SYMBOL') ?? '.'; + $time_diff_symbol = Config::get('Timezone.TIME_DIFF_SYMBOL','.'); $str_time_diff = $time->format('p'); return str_replace(':', $time_diff_symbol, $str_time_diff); } @@ -173,9 +178,6 @@ private function getTimeDiff($time): string */ private function getZone($time): string { - $str_zone = $time->format('e'); - return str_replace('_', ' ', $str_zone); + return str_replace('_', ' ', $time->format('e')); } - - -} +} \ No newline at end of file