Completed
Push — master ( 123713...d951bb )
by Joachim
02:27
created

DateTimeImmutable   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 22.58%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 0
dl 0
loc 86
ccs 7
cts 31
cp 0.2258
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
A instance() 0 8 2
A createFromFormat() 0 9 2
A createFromMutable() 0 5 1
A createFromTimestamp() 0 10 3
A createFromJson() 0 12 2
A timeZone() 0 4 1
1
<?php
2
namespace Loevgaard\DandomainDateTime;
3
4
use DateTime;
5
use DateTimeInterface;
6
use DateTimeZone;
7
use Exception;
8
use InvalidArgumentException;
9
10
/**
11
 * This class represents a date time in Dandomain where the time zone is always Europe/Copenhagen
12
 */
13
class DateTimeImmutable extends \DateTimeImmutable
14
{
15 2
    public function __construct($time = 'now')
16
    {
17 2
        if (strpos($time, '@') !== false) {
18 1
            throw new InvalidArgumentException('Use DateTimeImmutable::createFromTimestamp instead');
19
        }
20
21 1
        parent::__construct($time, static::timeZone());
22 1
    }
23
24
    /**
25
     * @throws Exception
26
     */
27
    public static function instance(DateTimeInterface $dt) : DateTimeImmutable
28
    {
29
        if ($dt instanceof static) {
30
            return clone $dt;
31
        }
32
33
        return new static($dt->format('Y-m-d H:i:s.u'));
34
    }
35
36
    /**
37
     * @throws Exception
38
     */
39
    public static function createFromFormat($format, $time, $timezone = null) : DateTimeImmutable
40
    {
41
        if ($timezone !== null) {
42
            throw new InvalidArgumentException('Do not pass time zone as an argument');
43
        }
44
45
        $dt = parent::createFromFormat($format, $time, static::timeZone());
46
        return static::instance($dt);
47
    }
48
49
    /**
50
     * @param DateTime $dateTime
51
     * @return DateTimeImmutable
52
     */
53
    public static function createFromMutable($dateTime) : DateTimeImmutable
54
    {
55
        $dt = static::createFromMutable($dateTime);
56
        return $dt->setTimezone(static::timeZone());
57
    }
58
59
    /**
60
     * @throws Exception
61
     */
62
    public static function createFromTimestamp($timestamp) : DateTimeImmutable
63
    {
64
        if (is_string($timestamp) && ($pos = strpos($timestamp, '.')) !== false) {
65
            $timestamp = substr($timestamp, 0, $pos);
66
        }
67
68
        $dateTime = new DateTime('@'.$timestamp);
69
        $dateTime->setTimezone(static::timeZone());
70
        return static::instance($dateTime);
71
    }
72
73
    /**
74
     * @throws Exception
75
     */
76
    public static function createFromJson(string $json) : DateTimeImmutable
77
    {
78
        preg_match('/(\d+)\+/', $json, $matches);
79
        if (!isset($matches[1])) {
80
            throw new InvalidArgumentException('$json is not a valid JSON date. Input: ' . $json);
81
        }
82
83
        // remove the last three digits since the json date is given in milliseconds
84
        $timestamp = substr($matches[1], 0, -3);
85
86
        return static::createFromTimestamp($timestamp);
87
    }
88
89
    /**
90
     * Returns the default Dandomain time zone
91
     *
92
     * @return DateTimeZone
93
     */
94 1
    protected static function timeZone() : DateTimeZone
95
    {
96 1
        return new DateTimeZone('Europe/Copenhagen');
97
    }
98
}
99