|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ValueObjects\DateTime; |
|
4
|
|
|
|
|
5
|
|
|
use ValueObjects\Util\Util; |
|
6
|
|
|
use ValueObjects\ValueObjectInterface; |
|
7
|
|
|
|
|
8
|
|
|
class DateTimeWithTimeZone implements ValueObjectInterface |
|
9
|
|
|
{ |
|
10
|
|
|
/** @var DateTime */ |
|
11
|
|
|
protected $dateTime; |
|
12
|
|
|
|
|
13
|
|
|
/** @var TimeZone */ |
|
14
|
|
|
protected $timeZone; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Returns a new DateTime object from native values |
|
18
|
|
|
* |
|
19
|
|
|
* @param int $year |
|
|
|
|
|
|
20
|
|
|
* @param string $month |
|
|
|
|
|
|
21
|
|
|
* @param int $day |
|
|
|
|
|
|
22
|
|
|
* @param int $hour |
|
|
|
|
|
|
23
|
|
|
* @param int $minute |
|
|
|
|
|
|
24
|
|
|
* @param int $second |
|
|
|
|
|
|
25
|
|
|
* @param string $timezone |
|
|
|
|
|
|
26
|
|
|
* |
|
27
|
|
|
* @return DateTimeWithTimeZone |
|
28
|
|
|
*/ |
|
29
|
1 |
|
public static function fromNative() |
|
30
|
|
|
{ |
|
31
|
1 |
|
$args = func_get_args(); |
|
32
|
|
|
|
|
33
|
1 |
|
$datetime = DateTime::fromNative($args[0], $args[1], $args[2], $args[3], $args[4], $args[5]); |
|
34
|
1 |
|
$timezone = TimeZone::fromNative($args[6]); |
|
35
|
|
|
|
|
36
|
1 |
|
return new static($datetime, $timezone); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Returns a new DateTime from a native PHP \DateTime |
|
41
|
|
|
* |
|
42
|
|
|
* @param \DateTime $nativeDatetime |
|
43
|
|
|
* |
|
44
|
|
|
* @return DateTimeWithTimeZone |
|
45
|
|
|
*/ |
|
46
|
1 |
|
public static function fromNativeDateTime(\DateTime $nativeDatetime) |
|
47
|
|
|
{ |
|
48
|
1 |
|
$datetime = DateTime::fromNativeDateTime($nativeDatetime); |
|
49
|
1 |
|
$timezone = TimeZone::fromNativeDateTimeZone($nativeDatetime->getTimezone()); |
|
50
|
|
|
|
|
51
|
1 |
|
return new static($datetime, $timezone); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Returns a DateTimeWithTimeZone object using current DateTime and default TimeZone |
|
56
|
|
|
* |
|
57
|
|
|
* @return DateTimeWithTimeZone |
|
58
|
|
|
*/ |
|
59
|
1 |
|
public static function now() |
|
60
|
|
|
{ |
|
61
|
1 |
|
return new static(DateTime::now(), TimeZone::fromDefault()); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Returns a new DateTimeWithTimeZone object |
|
66
|
|
|
* |
|
67
|
|
|
* @param DateTime $datetime |
|
68
|
|
|
* @param TimeZone $timezone |
|
69
|
|
|
*/ |
|
70
|
9 |
|
public function __construct(DateTime $datetime, TimeZone $timezone = null) |
|
71
|
|
|
{ |
|
72
|
9 |
|
$this->dateTime = $datetime; |
|
73
|
9 |
|
$this->timeZone = $timezone; |
|
74
|
9 |
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Tells whether two DateTimeWithTimeZone are equal by comparing their values |
|
78
|
|
|
* |
|
79
|
|
|
* @param ValueObjectInterface $dateTimeWithTimeZone |
|
80
|
|
|
* |
|
81
|
|
|
* @return bool |
|
82
|
|
|
*/ |
|
83
|
4 |
|
public function sameValueAs(ValueObjectInterface $dateTimeWithTimeZone) |
|
84
|
|
|
{ |
|
85
|
4 |
|
if (false === Util::classEquals($this, $dateTimeWithTimeZone)) { |
|
86
|
1 |
|
return false; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
4 |
|
return $this->getDateTime()->sameValueAs($dateTimeWithTimeZone->getDateTime()) && |
|
|
|
|
|
|
90
|
4 |
|
$this->getTimeZone()->sameValueAs($dateTimeWithTimeZone->getTimeZone()); |
|
|
|
|
|
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Tells whether two DateTimeWithTimeZone represents the same timestamp |
|
95
|
|
|
* |
|
96
|
|
|
* @param ValueObjectInterface $dateTimeWithTimeZone |
|
97
|
|
|
* |
|
98
|
|
|
* @return bool |
|
99
|
|
|
*/ |
|
100
|
1 |
|
public function sameTimestampAs(ValueObjectInterface $dateTimeWithTimeZone) |
|
101
|
|
|
{ |
|
102
|
1 |
|
if (false === Util::classEquals($this, $dateTimeWithTimeZone)) { |
|
103
|
1 |
|
return false; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
1 |
|
return $this->toNativeDateTime() == $dateTimeWithTimeZone->toNativeDateTime(); |
|
|
|
|
|
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Returns datetime from current DateTimeWithTimeZone |
|
111
|
|
|
* |
|
112
|
|
|
* @return DateTime |
|
113
|
|
|
*/ |
|
114
|
8 |
|
public function getDateTime() |
|
115
|
|
|
{ |
|
116
|
8 |
|
return clone $this->dateTime; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Returns timezone from current DateTimeWithTimeZone |
|
121
|
|
|
* |
|
122
|
|
|
* @return TimeZone |
|
123
|
|
|
*/ |
|
124
|
8 |
|
public function getTimeZone() |
|
125
|
|
|
{ |
|
126
|
8 |
|
return clone $this->timeZone; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Returns a native PHP \DateTime version of the current DateTimeWithTimeZone |
|
131
|
|
|
* |
|
132
|
|
|
* @return \DateTime |
|
133
|
|
|
*/ |
|
134
|
2 |
|
public function toNativeDateTime() |
|
135
|
|
|
{ |
|
136
|
2 |
|
$date = $this->getDateTime()->getDate(); |
|
137
|
2 |
|
$time = $this->getDateTime()->getTime(); |
|
138
|
2 |
|
$year = $date->getYear()->toNative(); |
|
139
|
2 |
|
$month = $date->getMonth()->getNumericValue(); |
|
140
|
2 |
|
$day = $date->getDay()->toNative(); |
|
141
|
2 |
|
$hour = $time->getHour()->toNative(); |
|
142
|
2 |
|
$minute = $time->getMinute()->toNative(); |
|
143
|
2 |
|
$second = $time->getSecond()->toNative(); |
|
144
|
2 |
|
$timezone = $this->getTimeZone()->toNativeDateTimeZone(); |
|
145
|
|
|
|
|
146
|
2 |
|
$dateTime = new \DateTime(); |
|
147
|
2 |
|
$dateTime->setTimezone($timezone); |
|
148
|
2 |
|
$dateTime->setDate($year, $month, $day); |
|
149
|
2 |
|
$dateTime->setTime($hour, $minute, $second); |
|
150
|
|
|
|
|
151
|
2 |
|
return $dateTime; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
function jsonSerialize() |
|
|
|
|
|
|
155
|
|
|
{ |
|
156
|
|
|
return $this->toNativeDateTime(); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* Returns DateTime as string in format "Y-n-j G:i:s e" |
|
161
|
|
|
* |
|
162
|
|
|
* @return string |
|
163
|
|
|
*/ |
|
164
|
2 |
|
public function __toString() |
|
165
|
|
|
{ |
|
166
|
2 |
|
return \sprintf('%s %s', $this->getDateTime(), $this->getTimeZone()); |
|
167
|
|
|
} |
|
168
|
|
|
} |
|
169
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.