TimeZoneService::list()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 2 Features 0
Metric Value
cc 2
eloc 5
c 4
b 2
f 0
nc 2
nop 0
dl 0
loc 7
rs 10
1
<?php
2
3
namespace Joy2362\PhpTimezone;
4
5
use DateTime;
6
use Exception;
7
use DateTimeZone;
8
use Illuminate\Support\Facades\Config;
9
10
class TimeZoneService
11
{
12
    /**
13
     * @var array
14
     */
15
    private array $regions = [
16
        'Africa' => DateTimeZone::AFRICA,
17
        'America' => DateTimeZone::AMERICA,
18
        'Antarctica' => DateTimeZone::ANTARCTICA,
19
        'Asia' => DateTimeZone::ASIA,
20
        'Atlantic' => DateTimeZone::ATLANTIC,
21
        'Australia' => DateTimeZone::AUSTRALIA,
22
        'Europe' => DateTimeZone::EUROPE,
23
        'Indian' => DateTimeZone::INDIAN,
24
        'Pacific' => DateTimeZone::PACIFIC,
25
    ];
26
27
    /**
28
     * @var array|string[]
29
     */
30
    private array $supportedTimeZone = ['GMT', 'UTC'];
31
32
    /**
33
     * @return array
34
     */
35
    public function getRegions(): array
36
    {
37
        return array_keys($this->regions);
38
    }
39
40
    /**
41
     * @return array
42
     */
43
    public function getSupportedTimeZone(): array
44
    {
45
        return $this->supportedTimeZone;
46
    }
47
48
    /**
49
     * @return array
50
     */
51
    public function list(): array
52
    {
53
        $list = [];
54
        foreach ($this->regions as $region) {
55
            $list = array_merge($list, $this->getTimeZoneList(DateTimeZone::listIdentifiers($region) ?? []));
56
        }
57
        return $list;
58
    }
59
60
    /**
61
     * @return array
62
     */
63
    public function listWithoutLabel(): array
64
    {
65
        $list = [];
66
        foreach ($this->regions as $region) {
67
            $list = array_merge($list, $this->getTimeZoneList(DateTimeZone::listIdentifiers($region) ?? [], 'value'));
68
        }
69
        return $list;
70
    }
71
72
    /**
73
     * @return array
74
     */
75
    public function listWithoutValue(): array
76
    {
77
        $list = [];
78
        foreach ($this->regions as $region) {
79
            $list = array_merge($list, $this->getTimeZoneList(DateTimeZone::listIdentifiers($region) ?? [], 'label'));
80
        }
81
        return $list;
82
    }
83
84
    public function listByRegion($region): array
85
    {
86
        if (!array_key_exists($region, $this->regions)) {
87
            return [];
88
        }
89
90
        return $this->getTimeZoneList(DateTimeZone::listIdentifiers($this->regions[$region]) ?? []);
91
    }
92
93
    /**
94
     * @param $label
95
     * @return string
96
     */
97
    public function getValueFromLabel($label): string
98
    {
99
        $str_zone = explode(') ', $label);
100
        return str_replace(' ', '_', $str_zone[1]);
101
    }
102
103
    /**
104
     * @param $value
105
     * @return string
106
     */
107
    public function getLabelFromValue($value): string
108
    {
109
        return $this->getLabel($value);
110
    }
111
112
    /**
113
     * @param $timezone
114
     * @return string
115
     */
116
    private function getLabel($timezone): string
117
    {
118
        try {
119
            $time = new DateTime('', new DateTimeZone($timezone));
120
            $time_diff = $this->getTimeDiff($time);
121
            $zone = $this->getZone($time);
122
123
            $defaultTimeZone = Config::get('Timezone.DEFAULT_TIME_ZONE', 'GMT');
124
125
            $defaultTimeZone = in_array($defaultTimeZone, $this->supportedTimeZone) ? $defaultTimeZone : $this->supportedTimeZone[0];
126
            return "({$defaultTimeZone} {$time_diff}) {$zone}";
127
        } catch (Exception $ex) {
128
            return '';
129
        }
130
    }
131
132
    /**
133
     * @param $time
134
     * @return string
135
     */
136
    private function getTimeDiff($time): string
137
    {
138
        $time_diff_symbol = Config::get('Timezone.TIME_DIFF_SYMBOL', '.');
139
        $str_time_diff = $time->format('p');
140
        return str_replace(':', $time_diff_symbol, $str_time_diff);
141
    }
142
143
    /**
144
    * @param $time
145
    * @return string
146
    */
147
    private function getZone($time): string
148
    {
149
        return str_replace('_', ' ', $time->format('e'));
150
    }
151
152
    /**
153
     * @param array $timezones
154
     * @param bool $isLabel
155
     * @param bool $isValue
156
     * @return array
157
    */
158
    private function getTimeZoneList(array $timezones, string $type = 'list'): array
159
    {
160
        $label = Config::get('Timezone.LABEL_FIELD_NAME', 'label');
161
        $value = Config::get('Timezone.VALUE_FIELD_NAME', 'value');
162
163
        $data = [];
164
165
        foreach ($timezones as $timezone) {
166
167
            switch ($type) {
168
                case 'label':
169
                    $data[] = $this->getLabel($timezone);
170
                    break;
171
172
                case 'value':
173
                    $data[] = $timezone;
174
                    break;
175
176
                default:
177
                    $zone = [
178
                        "{$label}" => $this->getLabel($timezone),
179
                        "{$value}" => $timezone,
180
                    ];
181
                    $data[] = $zone;
182
                    break;
183
            }
184
        }
185
        return $data;
186
    }
187
}
188