Skip to content

Commit

Permalink
fix: list data issue
Browse files Browse the repository at this point in the history
  • Loading branch information
joy2362 committed Jun 29, 2024
1 parent dc20987 commit c8eaf21
Showing 1 changed file with 23 additions and 14 deletions.
37 changes: 23 additions & 14 deletions src/TimeZoneService.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function list(): array
{
$list = [];
foreach ($this->regions as $region) {
$list[] = $this->getTimeZoneList(DateTimeZone::listIdentifiers($region) ?? []);
$list = array_merge($list , $this->getTimeZoneList(DateTimeZone::listIdentifiers($region) ?? []));
}
return $list;
}
Expand All @@ -64,7 +64,7 @@ public function listWithoutLabel(): array
{
$list = [];
foreach ($this->regions as $region) {
$list[] = $this->getTimeZoneList(DateTimeZone::listIdentifiers($region) ?? [], false);
$list = array_merge($list , $this->getTimeZoneList(DateTimeZone::listIdentifiers($region) ?? [], 'value'));
}
return $list;
}
Expand All @@ -76,7 +76,7 @@ public function listWithoutValue(): array
{
$list = [];
foreach ($this->regions as $region) {
$list[] = $this->getTimeZoneList(DateTimeZone::listIdentifiers($region) ?? [], true, false);
$list = array_merge($list , $this->getTimeZoneList(DateTimeZone::listIdentifiers($region) ?? [], 'label'));
}
return $list;
}
Expand Down Expand Up @@ -154,24 +154,33 @@ private function getZone($time): string
* @param bool $isValue
* @return array
*/
private function getTimeZoneList(array $timezones, bool $isLabel = true, bool $isValue = true): array
private function getTimeZoneList(array $timezones, string $type = 'list'): array
{
$label = Config::get('Timezone.LABEL_FIELD_NAME', 'label');
$value = Config::get('Timezone.VALUE_FIELD_NAME', 'value');

$data = [];

foreach ($timezones as $timezone) {
if($isLabel) {
$data[$label] = $this->getLabel($timezone);
}

if($isValue) {
$data[$value] = $timezone;
}


switch ($type) {
case 'label':
$data[] = $this->getLabel($timezone);
break;

case 'value':
$data[] = $timezone;
break;

default:
$zone = [
"{$label}" => $this->getLabel($timezone),
"{$value}" => $timezone,
];
$data[] = $zone;
break;
}
}

return $data;
}
}
}

0 comments on commit c8eaf21

Please sign in to comment.