|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Copyright 2016 Alexandru Guzinschi <[email protected]>. |
|
4
|
|
|
* |
|
5
|
|
|
* This software may be modified and distributed under the terms |
|
6
|
|
|
* of the MIT license. See the LICENSE file for details. |
|
7
|
|
|
*/ |
|
8
|
|
|
namespace Gentle\Embeddable; |
|
9
|
|
|
|
|
10
|
|
|
use Gentle\Embeddable\Time\Hour; |
|
11
|
|
|
use Gentle\Embeddable\Time\Minute; |
|
12
|
|
|
use Gentle\Embeddable\Time\Second; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @author Alexandru Guzinschi <[email protected]> |
|
16
|
|
|
*/ |
|
17
|
|
|
final class Time extends Embeddable |
|
18
|
|
|
{ |
|
19
|
|
|
/** @var Hour */ |
|
20
|
|
|
private $hour; |
|
21
|
|
|
|
|
22
|
|
|
/** @var Minute */ |
|
23
|
|
|
private $minutes; |
|
24
|
|
|
|
|
25
|
|
|
/** @var Second */ |
|
26
|
|
|
private $seconds; |
|
27
|
|
|
|
|
28
|
|
|
/** @var \DateTime */ |
|
29
|
|
|
private $dateTime; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @static |
|
33
|
|
|
* @access public |
|
34
|
|
|
* @param \DateTime $dateTime |
|
35
|
|
|
* @return Time |
|
36
|
|
|
* |
|
37
|
|
|
* @throws \OutOfRangeException |
|
38
|
|
|
* @throws \InvalidArgumentException |
|
39
|
|
|
* @throws \LengthException |
|
40
|
|
|
*/ |
|
41
|
6 |
|
public static function from(\DateTime $dateTime) |
|
42
|
|
|
{ |
|
43
|
6 |
|
return new self( |
|
44
|
6 |
|
new Hour($dateTime->format('H')), |
|
45
|
6 |
|
new Minute($dateTime->format('i')), |
|
46
|
6 |
|
new Second($dateTime->format('s')) |
|
47
|
|
|
); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @param Hour $hour |
|
52
|
|
|
* @param Minute $minutes |
|
53
|
|
|
* @param Second $seconds |
|
54
|
|
|
* |
|
55
|
|
|
* @throws \OutOfRangeException |
|
56
|
|
|
*/ |
|
57
|
30 |
|
public function __construct(Hour $hour, Minute $minutes, Second $seconds) |
|
58
|
|
|
{ |
|
59
|
30 |
|
$this->dateTime = \DateTime::createFromFormat( |
|
60
|
30 |
|
'H:i:s', |
|
61
|
30 |
|
sprintf('%s:%s:%s', $hour, $minutes, $seconds), |
|
62
|
30 |
|
new \DateTimeZone(date_default_timezone_get()) |
|
63
|
|
|
); |
|
64
|
30 |
|
$this->hour = $hour; |
|
65
|
30 |
|
$this->minutes = $minutes; |
|
66
|
30 |
|
$this->seconds = $seconds; |
|
67
|
30 |
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @access public |
|
71
|
|
|
* @param \DateTimeZone $timezone |
|
72
|
|
|
* @return Time |
|
73
|
|
|
* |
|
74
|
|
|
* @see \DateTimeZone::listIdentifiers() |
|
75
|
|
|
* |
|
76
|
|
|
* @throws \OutOfRangeException |
|
77
|
|
|
* @throws \InvalidArgumentException |
|
78
|
|
|
*/ |
|
79
|
6 |
|
public function withTimeZone(\DateTimeZone $timezone) |
|
80
|
|
|
{ |
|
81
|
6 |
|
date_default_timezone_set($timezone->getName()); |
|
82
|
|
|
|
|
83
|
6 |
|
return new self( |
|
84
|
6 |
|
new Hour((string)$this->getHour()), |
|
85
|
6 |
|
new Minute((string)$this->getMinutes()), |
|
86
|
6 |
|
new Second((string)$this->getSeconds()) |
|
87
|
|
|
); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* {@inheritdoc} |
|
92
|
|
|
*/ |
|
93
|
6 |
|
public function equals(Embeddable $object) |
|
94
|
|
|
{ |
|
95
|
|
|
/* @var Time $object */ |
|
96
|
6 |
|
return get_class($object) === 'Gentle\Embeddable\Time' && |
|
97
|
6 |
|
(string)$this->hour === (string)$object->getHour() && |
|
98
|
6 |
|
(string)$this->minutes === (string)$object->getMinutes() && |
|
99
|
6 |
|
(string)$this->seconds === (string)$object->getSeconds() |
|
100
|
|
|
; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Returned format is `H-i-s`. |
|
105
|
|
|
* |
|
106
|
|
|
* {@inheritdoc} |
|
107
|
|
|
*/ |
|
108
|
6 |
|
public function __toString() |
|
109
|
|
|
{ |
|
110
|
6 |
|
return sprintf('%s:%s:%s', $this->getHour(), $this->getMinutes(), $this->getSeconds()); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* @access public |
|
115
|
|
|
* @return Hour |
|
116
|
|
|
*/ |
|
117
|
18 |
|
public function getHour() |
|
118
|
|
|
{ |
|
119
|
18 |
|
return $this->hour; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* @access public |
|
124
|
|
|
* @return Minute |
|
125
|
|
|
*/ |
|
126
|
18 |
|
public function getMinutes() |
|
127
|
|
|
{ |
|
128
|
18 |
|
return $this->minutes; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* @access public |
|
133
|
|
|
* @return Second |
|
134
|
|
|
*/ |
|
135
|
18 |
|
public function getSeconds() |
|
136
|
|
|
{ |
|
137
|
18 |
|
return $this->seconds; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* @access public |
|
142
|
|
|
* @return \DateTime |
|
143
|
|
|
*/ |
|
144
|
12 |
|
public function asDateTime() |
|
145
|
|
|
{ |
|
146
|
12 |
|
return $this->dateTime; |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
|