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

TimezoneDropdown::normalizedAliases()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 10
nc 4
nop 0
dl 0
loc 16
rs 9.9332
c 1
b 0
f 0
1
<?php
2
3
namespace LeKoala\GeoTools\Fields;
4
5
use SilverStripe\Forms\DropdownField;
6
use SilverStripe\ORM\ArrayLib;
7
use SilverStripe\ORM\DataObjectInterface;
8
9
/**
10
 * A timezone dropdown
11
 * @link https://www.php.net/manual/en/timezones.others.php
12
 * @link https://www.timeanddate.com/time/zones/
13
 */
14
class TimezoneDropdown extends DropdownField
15
{
16
    // in yml, it needs to be name/value like so
17
    // - name: "Gulf Standard Time (GST)"
18
    //   value: "Asia/Dubai"
19
    private static $aliases = [
20
        // "Gulf Standard Time (GST)" => "Asia/Dubai",
21
        // "Central European Time (CET)" => "Europe/Brussels",
22
        // "Atlantic Standard Time (AST)" => "America/Blanc-Sablon",
23
        // "Eastern Standard Time (EST)" => "America/Panama",
24
        // "Central Standard Time (CST)" => "America/Regina",
25
        // "Mountain Standard Time (MST)" => "America/Phoenix",
26
        // "Pacific Standard Time (PST)" => "America/Los_Angeles",
27
    ];
28
29
    /**
30
     * @param string $name The field name
31
     * @param string $title The field title
32
     * @param array|ArrayAccess $source A map of the dropdown items
0 ignored issues
show
Bug introduced by
The type LeKoala\GeoTools\Fields\ArrayAccess was not found. Did you mean ArrayAccess? If so, make sure to prefix the type with \.
Loading history...
33
     * @param mixed $value The current value
34
     */
35
    public function __construct($name, $title = null, $source = array(), $value = null)
36
    {
37
        if (empty($source)) {
38
            $source = ArrayLib::valuekey(timezone_identifiers_list());
0 ignored issues
show
Bug introduced by
Are you sure the usage of timezone_identifiers_list() is correct as it seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
39
            $aliases = $this->config()->aliases ?? [];
40
            if (!empty($aliases)) {
41
                $normalizedAliases = [];
42
                foreach ($aliases as $k => $v) {
43
                    // Add a prefix to avoid clashes with regular value
44
                    if (is_int($k)) {
45
                        $normalizedAliases[$v['name']] = '_' . $v['value'];
46
                    } else {
47
                        $normalizedAliases[$k] = '_' . $v;
48
                    }
49
                }
50
51
                $source = array_flip($normalizedAliases) + $source;
52
            }
53
        }
54
        parent::__construct($name, $title, $source, $value);
55
    }
56
57
    public function normalizedAliases()
58
    {
59
        $aliases = $this->config()->aliases ?? [];
60
        if (!empty($aliases)) {
61
            $normalizedAliases = [];
62
            foreach ($aliases as $k => $v) {
63
                if (is_int($k)) {
64
                    $normalizedAliases[$v['name']] = $v['value'];
65
                } else {
66
                    $normalizedAliases[$k] = $v;
67
                }
68
            }
69
70
            return $normalizedAliases;
71
        }
72
        return [];
73
    }
74
75
    public function dataValue()
76
    {
77
        return ltrim(parent::dataValue(), '_');
78
    }
79
}
80