Completed
Push — master ( 42a2ad...714685 )
by Chad
12s queued 10s
created

DateTimeZone   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 5
Bugs 0 Features 0
Metric Value
wmc 9
c 5
b 0
f 0
lcom 1
cbo 0
dl 0
loc 81
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A fromString() 0 12 3
A fromOffset() 0 12 3
A getLongName() 0 11 3
1
<?php
2
namespace SubjectivePHP\Util;
3
4
/**
5
 * Static utility class for working with DateTimeZone objects.
6
 */
7
abstract class DateTimeZone
8
{
9
    /**
10
     * Array of edge case timezones
11
     *
12
     * @var array
13
     */
14
    private static $outliers = [
15
        'WIB' => 'Asia/Jakarta',  //Western Indonesian Time
16
        'FET' => 'Europe/Helsinki', //Further-eastern European Time
17
        'AEST' => 'Australia/Tasmania', //Australia Eastern Standard Time
18
        'AWST' => 'Australia/West', //Australia Western Standard Time
19
        'WITA' => 'Asia/Makassar',
20
        'AEDT' => 'Australia/Sydney', //Australia Eastern Daylight Time
21
        'ACDT' => 'Australia/Adelaide', //Australia Central Daylight Time
22
    ];
23
24
    /**
25
     * Returns a \DateTimeZone instance for the give nameOrAbbreviation.
26
     *
27
     * @param string        $nameOrAbbreviation The timezone nameOrAbbreviation.
28
     * @param \DateTimeZone $default            The default timezone to return if none can be created.
29
     *
30
     * @return \DateTimeZone
31
     */
32
    final public static function fromString($nameOrAbbreviation, \DateTimeZone $default = null)
33
    {
34
        try {
35
            return new \DateTimeZone($nameOrAbbreviation);
36
        } catch (\Exception $e) {
37
            if (array_key_exists($nameOrAbbreviation, self::$outliers)) {
38
                return new \DateTimeZone(self::$outliers[$nameOrAbbreviation]);
39
            }
40
41
            return $default;
42
        }
43
    }
44
45
    /**
46
     * Returns a \DateTimeZone object based on gmt offset.
47
     *
48
     * @param integer $gmtOffset         Offset from GMT in seconds.
49
     * @param boolean $isDaylightSavings Daylight saving time indicator.
50
     *
51
     * @return \DateTimeZone
52
     *
53
     * @throws \InvalidArgumentException Thrown if $gmtOffset is not an integer.
54
     * @throws \InvalidArgumentException Thrown if $isDaylightSavings is not a boolean.
55
     */
56
    final public static function fromOffset($gmtOffset, $isDaylightSavings)
57
    {
58
        if (!is_int($gmtOffset)) {
59
            throw new \InvalidArgumentException('$gmtOffset must be an integer');
60
        }
61
62
        if (!is_bool($isDaylightSavings)) {
63
            throw new \InvalidArgumentException('$isDaylightSavings must be a boolean');
64
        }
65
66
        return self::fromString(timezone_name_from_abbr('', $gmtOffset, (int)$isDaylightSavings));
67
    }
68
69
    /**
70
     * Returns the long name of the given \DateTimeZone.
71
     *
72
     * @param \DateTimeZone $timezone The timezone object from which the long name should be obtained.
73
     *
74
     * @return string
75
     */
76
    final public static function getLongName(\DateTimeZone $timezone)
77
    {
78
        $nameFromTimeZone = $timezone->getName();
79
        if (strlen($nameFromTimeZone) > 6) {
80
            return $nameFromTimeZone;
81
        }
82
83
        $nameFromAbbr = timezone_name_from_abbr($nameFromTimeZone);
84
85
        return $nameFromAbbr === false ? $nameFromTimeZone : $nameFromAbbr;
86
    }
87
}
88