Completed
Pull Request — master (#44)
by Chad
01:01
created

DateTimeZoneUtil::getLongName()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11

Duplication

Lines 11
Ratio 100 %

Importance

Changes 0
Metric Value
dl 11
loc 11
rs 9.9
c 0
b 0
f 0
cc 3
nc 3
nop 1
1
<?php
2
namespace SubjectivePHP\DateTime\Utilities;
3
4
use DateTimeZone;
5
use SubjectivePHP\DateTime\Constants\TimezonesInterface;
6
7
/**
8
 * Static utility class for working with DateTimeZone objects.
9
 */
10
abstract class DateTimeZoneUtil implements TimezonesInterface
11
{
12
    /**
13
     * @var array
14
     */
15
    const OUTLIERS = [
16
        'WIB' => self::TIMEZONE_ASIA_JAKARTA,  //Western Indonesian Time
17
        'FET' => self::TIMEZONE_EUROPE_HELSINKI, //Further-eastern European Time
18
        'AEST' => self::TIMEZONE_AUSTRALIA_TASMANIA, //Australia Eastern Standard Time
19
        'AWST' => self::TIMEZONE_AUSTRALIA_WEST, //Australia Western Standard Time
20
        'WITA' => self::TIMEZONE_ASIA_MAKASSAR,
21
        'AEDT' => self::TIMEZONE_AUSTRALIA_SYDNEY, //Australia Eastern Daylight Time
22
        'ACDT' => self::TIMEZONE_AUSTRALIA_ADELAIDE, //Australia Central Daylight Time
23
    ];
24
25
    /**
26
     * Returns a DateTimeZone instance for the give nameOrAbbreviation.
27
     *
28
     * @param string       $nameOrAbbreviation The timezone nameOrAbbreviation.
29
     * @param DateTimeZone $default            The default timezone to return if none can be created.
30
     *
31
     * @return DateTimeZone|null
32
     */
33 View Code Duplication
    final public static function fromString(string $nameOrAbbreviation, DateTimeZone $default = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
34
    {
35
        try {
36
            return new DateTimeZone($nameOrAbbreviation);
37
        } catch (\Exception $e) {
38
            if (array_key_exists($nameOrAbbreviation, self::OUTLIERS)) {
39
                return new DateTimeZone(self::OUTLIERS[$nameOrAbbreviation]);
40
            }
41
42
            return $default;
43
        }
44
    }
45
46
    /**
47
     * Returns a DateTimeZone object based on gmt offset.
48
     *
49
     * @param int  $gmtOffset         Offset from GMT in seconds.
50
     * @param bool $isDaylightSavings Daylight saving time indicator.
51
     *
52
     * @return DateTimeZone
53
     */
54
    final public static function fromOffset(int $gmtOffset, bool $isDaylightSavings) : DateTimeZone
55
    {
56
        return self::fromString(timezone_name_from_abbr('', $gmtOffset, (int)$isDaylightSavings));
57
    }
58
59
    /**
60
     * Returns the long name of the given DateTimeZone.
61
     *
62
     * @param DateTimeZone $timezone The timezone object from which the long name should be obtained.
63
     *
64
     * @return string
65
     */
66 View Code Duplication
    final public static function getLongName(DateTimeZone $timezone) : string
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
67
    {
68
        $nameFromTimeZone = $timezone->getName();
69
        if (strlen($nameFromTimeZone) > 6) {
70
            return $nameFromTimeZone;
71
        }
72
73
        $nameFromAbbr = timezone_name_from_abbr($nameFromTimeZone);
74
75
        return $nameFromAbbr === false ? $nameFromTimeZone : $nameFromAbbr;
76
    }
77
}
78