Completed
Push — main ( 06246d...cd1cb2 )
by Abdullah
25s queued 14s
created

TimeZoneService::getLabel()   A

Complexity

Conditions 3
Paths 7

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 6
Bugs 4 Features 0
Metric Value
cc 3
eloc 9
c 6
b 4
f 0
nc 7
nop 1
dl 0
loc 13
rs 9.9666
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', '.');
0 ignored issues
show
Unused Code introduced by
The assignment to $time_diff_symbol is dead and can be removed.
Loading history...
139
        $time_diff_symbol = Config::get('Timezone.TIME_DIFF_SYMBOL', '.');
140
        $str_time_diff = $time->format('p');
141
        return str_replace(':', $time_diff_symbol, $str_time_diff);
142
    }
143
144
    /**
145
    * @param $time
146
    * @return string
147
    */
148
    private function getZone($time): string
149
    {
150
        return str_replace('_', ' ', $time->format('e'));
151
    }
152
153
    /**
154
     * @param array $timezones
155
     * @param bool $isLabel
156
     * @param bool $isValue
157
     * @return array
158
    */
159
    private function getTimeZoneList(array $timezones, string $type = 'list'): array
160
    {
161
        $label = Config::get('Timezone.LABEL_FIELD_NAME', 'label');
162
        $value = Config::get('Timezone.VALUE_FIELD_NAME', 'value');
163
164
        $data = [];
165
166
        foreach ($timezones as $timezone) {
167
168
            switch ($type) {
169
                case 'label':
170
                    $data[] = $this->getLabel($timezone);
171
                    break;
172
173
                case 'value':
174
                    $data[] = $timezone;
175
                    break;
176
177
                default:
178
                    $zone = [
179
                        "{$label}" => $this->getLabel($timezone),
180
                        "{$value}" => $timezone,
181
                    ];
182
                    $data[] = $zone;
183
                    break;
184
            }
185
        }
186
        return $data;
187
    }
188
}
189