DBTimezone::FullAlias()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 12
nc 2
nop 0
dl 0
loc 18
rs 9.8666
c 0
b 0
f 0
1
<?php
2
3
namespace LeKoala\GeoTools\FieldType;
4
5
use SilverStripe\ORM\FieldType\DBVarchar;
6
use LeKoala\GeoTools\Fields\TimezoneDropdown;
7
8
/**
9
 * A timezone dbfield
10
 */
11
class DBTimezone extends DBVarchar
12
{
13
    /**
14
     * @param string $name
15
     * @param array<mixed> $options
16
     */
17
    public function __construct($name = null, $options = [])
18
    {
19
        // The mysql.time_zone_name table has a Name column defined with 64 characters.
20
        // It makes sense to use VARCHAR(64) for storing this information; that matches the way the names are stored in the system.
21
        parent::__construct($name, 64, $options);
22
    }
23
24
    /**
25
     * @param string $title
26
     * @param mixed $params
27
     * @return TimezoneDropdown
28
     */
29
    public function scaffoldFormField($title = null, $params = null)
30
    {
31
        $field = TimezoneDropdown::create($this->name, $title);
32
        $field->setHasEmptyDefault(true);
33
        return $field;
34
    }
35
36
    public function FullAlias(): string
37
    {
38
        $field = TimezoneDropdown::create('dummy');
39
40
        // Use config value or some default values
41
        // See more https://www.timeanddate.com/time/zones/
42
        $aliases = $field->hasMethod('normalizedAliases') ? $field->normalizedAliases() : [
43
            "Gulf Standard Time (GST)" => "Asia/Dubai",
44
            "Central European Time (CET)" => "Europe/Brussels",
45
            "Atlantic Standard Time (AST)" => "America/Blanc-Sablon",
46
            "Eastern Standard Time (EST)" => "America/Panama",
47
            "Central Standard Time (CST)" => "America/Regina",
48
            "Mountain Standard Time (MST)" => "America/Phoenix",
49
            "Pacific Standard Time (PST)" => "America/Los_Angeles",
50
        ];
51
        $v = $this->value;
52
        $flip = array_flip($aliases);
53
        return $flip[$v] ?? $v;
54
    }
55
56
    public function ShortAlias(): string
57
    {
58
        $alias = $this->FullAlias();
59
        $parts = explode("(", $alias);
60
        if (isset($parts[1])) {
61
            return trim($parts[1], ')');
62
        }
63
        return $parts[0];
64
    }
65
}
66