Passed
Push — master ( d8f082...943bc9 )
by Thomas
02:04
created

DBTimezone::FullAlias()   A

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
    public function __construct($name = null, $options = [])
14
    {
15
        // The mysql.time_zone_name table has a Name column defined with 64 characters.
16
        // It makes sense to use VARCHAR(64) for storing this information; that matches the way the names are stored in the system.
17
        parent::__construct($name, 64, $options);
18
    }
19
20
    public function scaffoldFormField($title = null, $params = null)
21
    {
22
        $field = TimezoneDropdown::create($this->name, $title);
23
        $field->setHasEmptyDefault(true);
24
        return $field;
25
    }
26
27
    public function FullAlias()
28
    {
29
        $field = TimezoneDropdown::create('dummy');
30
31
        // Use config value or some default values
32
        // See more https://www.timeanddate.com/time/zones/
33
        $aliases = $field->hasMethod('normalizedAliases') ? $field->normalizedAliases() : [
34
            "Gulf Standard Time (GST)" => "Asia/Dubai",
35
            "Central European Time (CET)" => "Europe/Brussels",
36
            "Atlantic Standard Time (AST)" => "America/Blanc-Sablon",
37
            "Eastern Standard Time (EST)" => "America/Panama",
38
            "Central Standard Time (CST)" => "America/Regina",
39
            "Mountain Standard Time (MST)" => "America/Phoenix",
40
            "Pacific Standard Time (PST)" => "America/Los_Angeles",
41
        ];
42
        $v = $this->value;
43
        $flip = array_flip($aliases);
44
        return $flip[$v] ?? $v;
45
    }
46
47
    public function ShortAlias()
48
    {
49
        $alias = $this->FullAlias();
50
        $parts = explode("(", $alias);
51
        if (isset($parts[1])) {
52
            return trim($parts[1], ')');
53
        }
54
        return $parts[0];
55
    }
56
}
57