|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ValueObjects\DateTime; |
|
4
|
|
|
|
|
5
|
|
|
use ValueObjects\Util\Util; |
|
6
|
|
|
use ValueObjects\ValueObjectInterface; |
|
7
|
|
|
|
|
8
|
|
|
class DateTime implements ValueObjectInterface |
|
9
|
|
|
{ |
|
10
|
|
|
/** @var Date */ |
|
11
|
|
|
protected $date; |
|
12
|
|
|
|
|
13
|
|
|
/** @var Time */ |
|
14
|
|
|
protected $time; |
|
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
|
|
|
* @return DateTime |
|
26
|
|
|
*/ |
|
27
|
2 |
|
public static function fromNative() |
|
28
|
|
|
{ |
|
29
|
2 |
|
$args = func_get_args(); |
|
30
|
|
|
|
|
31
|
2 |
|
$date = Date::fromNative($args[0], $args[1], $args[2]); |
|
32
|
2 |
|
$time = Time::fromNative($args[3], $args[4], $args[5]); |
|
33
|
|
|
|
|
34
|
2 |
|
return new static($date, $time); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Returns a new DateTime from a native PHP \DateTime |
|
39
|
|
|
* |
|
40
|
|
|
* @param \DateTime $date_time |
|
41
|
|
|
* @return DateTime |
|
42
|
|
|
*/ |
|
43
|
2 |
|
public static function fromNativeDateTime(\DateTime $date_time) |
|
44
|
|
|
{ |
|
45
|
2 |
|
$date = Date::fromNativeDateTime($date_time); |
|
46
|
2 |
|
$time = Time::fromNativeDateTime($date_time); |
|
47
|
|
|
|
|
48
|
2 |
|
return new static($date, $time); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Returns current DateTime |
|
53
|
|
|
* |
|
54
|
|
|
* @return DateTime |
|
55
|
|
|
*/ |
|
56
|
2 |
|
public static function now() |
|
57
|
|
|
{ |
|
58
|
2 |
|
$dateTime = new static(Date::now(), Time::now()); |
|
59
|
|
|
|
|
60
|
2 |
|
return $dateTime; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Returns a new DateTime object |
|
65
|
|
|
* |
|
66
|
|
|
* @param Date $date |
|
67
|
|
|
* @param Time $time |
|
68
|
|
|
*/ |
|
69
|
18 |
|
public function __construct(Date $date, Time $time = null) |
|
70
|
|
|
{ |
|
71
|
18 |
|
$this->date = $date; |
|
72
|
|
|
|
|
73
|
18 |
|
if (null === $time) { |
|
74
|
1 |
|
$time = Time::zero(); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
18 |
|
$this->time = $time; |
|
78
|
18 |
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Tells whether two DateTime are equal by comparing their values |
|
82
|
|
|
* |
|
83
|
|
|
* @param ValueObjectInterface $date_time |
|
84
|
|
|
* @return bool |
|
85
|
|
|
*/ |
|
86
|
8 |
|
public function sameValueAs(ValueObjectInterface $date_time) |
|
87
|
|
|
{ |
|
88
|
8 |
|
if (false === Util::classEquals($this, $date_time)) { |
|
89
|
1 |
|
return false; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
8 |
|
return $this->getDate()->sameValueAs($date_time->getDate()) && $this->getTime()->sameValueAs($date_time->getTime()); |
|
|
|
|
|
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Returns date from current DateTime |
|
97
|
|
|
* |
|
98
|
|
|
* @return Date |
|
99
|
|
|
*/ |
|
100
|
15 |
|
public function getDate() |
|
101
|
|
|
{ |
|
102
|
15 |
|
return clone $this->date; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Returns time from current DateTime |
|
107
|
|
|
* |
|
108
|
|
|
* @return Time |
|
109
|
|
|
*/ |
|
110
|
16 |
|
public function getTime() |
|
111
|
|
|
{ |
|
112
|
16 |
|
return clone $this->time; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Returns a native PHP \DateTime version of the current DateTime. |
|
117
|
|
|
* |
|
118
|
|
|
* @return \DateTime |
|
119
|
|
|
*/ |
|
120
|
1 |
|
public function toNativeDateTime() |
|
121
|
|
|
{ |
|
122
|
1 |
|
$year = $this->getDate()->getYear()->toNative(); |
|
123
|
1 |
|
$month = $this->getDate()->getMonth()->getNumericValue(); |
|
124
|
1 |
|
$day = $this->getDate()->getDay()->toNative(); |
|
125
|
1 |
|
$hour = $this->getTime()->getHour()->toNative(); |
|
126
|
1 |
|
$minute = $this->getTime()->getMinute()->toNative(); |
|
127
|
1 |
|
$second = $this->getTime()->getSecond()->toNative(); |
|
128
|
|
|
|
|
129
|
1 |
|
$dateTime = new \DateTime(); |
|
130
|
1 |
|
$dateTime->setDate($year, $month, $day); |
|
131
|
1 |
|
$dateTime->setTime($hour, $minute, $second); |
|
132
|
|
|
|
|
133
|
1 |
|
return $dateTime; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Returns DateTime as string in format "Y-n-j G:i:s" |
|
138
|
|
|
* |
|
139
|
|
|
* @return string |
|
140
|
|
|
*/ |
|
141
|
4 |
|
public function __toString() |
|
142
|
|
|
{ |
|
143
|
4 |
|
return \sprintf('%s %s', $this->getDate(), $this->getTime()); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
function jsonSerialize() |
|
|
|
|
|
|
147
|
|
|
{ |
|
148
|
|
|
return $this->toNativeDateTime(); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
|
|
152
|
|
|
} |
|
153
|
|
|
|
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.