Date   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 170
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 170
rs 10
c 0
b 0
f 0
wmc 16

14 Methods

Rating   Name   Duplication   Size   Complexity  
A withUnixMicroTime() 0 6 1
A buildFromUnixTime() 0 7 1
A withOffset() 0 6 1
A getUnixTime() 0 3 1
A withUnixTime() 0 6 1
A withTimezone() 0 6 1
A buildFromDateTimeInterface() 0 10 1
A fromConversion() 0 10 1
A getUnixMicroTime() 0 3 1
A fromRepresentation() 0 10 1
A buildFromTimezone() 0 6 1
A buildFromMicroTime() 0 10 1
A getOffset() 0 3 2
A getTimezone() 0 3 2
1
<?php
2
3
namespace Popy\Calendar\ValueObject\DateRepresentation;
4
5
use DateTimeZone;
6
use DateTimeInterface;
7
use Popy\Calendar\Converter\Conversion;
8
use Popy\Calendar\ValueObject\TimeOffset;
9
use Popy\Calendar\ValueObject\DateRepresentationInterface;
10
11
/**
12
 * Basic implementation with static factory methods.
13
 */
14
class Date implements DateRepresentationInterface
15
{
16
    /**
17
     * Unix time.
18
     *
19
     * @var integer|null
20
     */
21
    protected $unixTime;
22
23
    /**
24
     * Unix microtime.
25
     *
26
     * @var integer|null
27
     */
28
    protected $unixMicroTime;
29
30
    /**
31
     * Timezone.
32
     *
33
     * @var DateTimeZone|null
34
     */
35
    protected $timezone;
36
37
    /**
38
     * TimeZone offset used when building this date object.
39
     *
40
     * @var TimeOffset
41
     */
42
    protected $offset;
43
44
    /**
45
     * @inheritDoc
46
     */
47
    public function getUnixTime()
48
    {
49
        return $this->unixTime;
50
    }
51
52
    /**
53
     * @inheritDoc
54
     */
55
    public function getUnixMicroTime()
56
    {
57
        return $this->unixMicroTime;
58
    }
59
60
    /**
61
     * @inheritDoc
62
     */
63
    public function getOffset()
64
    {
65
        return $this->offset ?: new TimeOffset();
66
    }
67
68
    /**
69
     * @inheritDoc
70
     */
71
    public function getTimezone()
72
    {
73
        return $this->timezone ?: new DateTimeZone(date_default_timezone_get());
74
    }
75
76
    /**
77
     * @inheritDoc
78
     */
79
    public function withUnixTime($unixTime)
80
    {
81
        $res = clone $this;
82
        $res->unixTime = $unixTime;
83
84
        return $res;
85
    }
86
87
    /**
88
     * @inheritDoc
89
     */
90
    public function withUnixMicroTime($unixMicroTime)
91
    {
92
        $res = clone $this;
93
        $res->unixMicroTime = $unixMicroTime;
94
95
        return $res;
96
    }
97
98
    /**
99
     * @inheritDoc
100
     */
101
    public function withOffset(TimeOffset $offset)
102
    {
103
        $res = clone $this;
104
        $res->offset = $offset;
105
106
        return $res;
107
    }
108
109
    /**
110
     * @inheritDoc
111
     */
112
    public function withTimezone(DateTimeZone $timezone)
113
    {
114
        $res = clone $this;
115
        $res->timezone = $timezone;
116
117
        return $res;
118
    }
119
120
121
    public static function buildFromUnixTime($time, DateTimeZone $timezone = null)
122
    {
123
        $res = new static();
124
        $res->unixTime = (int)$time;
125
        $res->timezone = $timezone;
126
127
        return $res;
128
    }
129
130
    public static function buildFromMicroTime($microtime, DateTimeZone $timezone = null)
131
    {
132
        list($micro, $time) = explode(' ', $microtime);
133
134
        $res = new static();
135
        $res->unixTime = (int)$time;
136
        $res->unixMicroTime = (int)substr($micro, 2, 6);
137
        $res->timezone = $timezone;
138
139
        return $res;
140
    }
141
142
    public static function buildFromDateTimeInterface(DateTimeInterface $datetime)
143
    {
144
        $res = new static();
145
146
        $res->unixTime = $datetime->getTimestamp();
147
        $res->unixMicroTime = (int)$datetime->format('u');
148
        $res->timezone = $datetime->getTimezone();
149
        $res->offset = TimeOffset::buildFromDateTimeInterface($datetime);
150
151
        return $res;
152
    }
153
154
    public static function buildFromTimezone(DateTimeZone $timezone = null)
155
    {
156
        $res = new static();
157
        $res->timezone = $timezone;
158
159
        return $res;
160
    }
161
162
    public static function fromRepresentation(DateRepresentationInterface $date)
163
    {
164
        $res = new static();
165
166
        $res->unixTime = $date->getUnixTime();
167
        $res->unixMicroTime = $date->getUnixMicroTime();
168
        $res->timezone = $date->getTimezone();
169
        $res->offset   = $date->getOffset();
170
171
        return $res;
172
    }
173
174
    public static function fromConversion(Conversion $conversion)
175
    {
176
        $res = new static();
177
178
        $res->unixTime = $conversion->getUnixTime();
179
        $res->unixMicroTime = $conversion->getUnixMicroTime();
180
        $res->timezone = $conversion->getTo()->getTimezone();
181
        $res->offset   = $conversion->getTo()->getOffset();
182
183
        return $res;
184
    }
185
}
186