Completed
Pull Request — master (#40)
by Chad
04:02
created

DateTimeZoneUtil   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 68
c 0
b 0
f 0
wmc 7
lcom 0
cbo 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A fromString() 0 12 3
A fromOffset() 0 4 1
A getLongName() 0 11 3
1
<?php
2
namespace SubjectivePHP\DateTime\Utilities;
3
4
use SubjectivePHP\DateTime\Constants\TimezonesInterface;
5
6
/**
7
 * Static utility class for working with DateTimeZone objects.
8
 */
9
abstract class DateTimeZoneUtil implements TimezonesInterface
10
{
11
    /**
12
     * @var array
13
     */
14
    const OUTLIERS = [
15
        'WIB' => self::TIMEZONE_ASIA_JAKARTA,  //Western Indonesian Time
16
        'FET' => self::TIMEZONE_EUROPE_HELSINKI, //Further-eastern European Time
17
        'AEST' => self::TIMEZONE_AUSTRALIA_TASMANIA, //Australia Eastern Standard Time
18
        'AWST' => self::TIMEZONE_AUSTRALIA_WEST, //Australia Western Standard Time
19
        'WITA' => self::TIMEZONE_ASIA_MAKASSAR,
20
        'AEDT' => self::TIMEZONE_AUSTRALIA_SYDNEY, //Australia Eastern Daylight Time
21
        'ACDT' => self::TIMEZONE_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(string $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
    final public static function fromOffset(int $gmtOffset, bool $isDaylightSavings)
54
    {
55
        return self::fromString(timezone_name_from_abbr('', $gmtOffset, (int)$isDaylightSavings));
56
    }
57
58
    /**
59
     * Returns the long name of the given \DateTimeZone.
60
     *
61
     * @param \DateTimeZone $timezone The timezone object from which the long name should be obtained.
62
     *
63
     * @return string
64
     */
65
    final public static function getLongName(\DateTimeZone $timezone)
66
    {
67
        $nameFromTimeZone = $timezone->getName();
68
        if (strlen($nameFromTimeZone) > 6) {
69
            return $nameFromTimeZone;
70
        }
71
72
        $nameFromAbbr = timezone_name_from_abbr($nameFromTimeZone);
73
74
        return $nameFromAbbr === false ? $nameFromTimeZone : $nameFromAbbr;
75
    }
76
}
77