1 | <?php |
||
17 | final class Date extends Embeddable |
||
18 | { |
||
19 | /** @var Year */ |
||
20 | private $year; |
||
21 | |||
22 | /** @var Month */ |
||
23 | private $month; |
||
24 | |||
25 | /** @var Day */ |
||
26 | private $day; |
||
27 | |||
28 | /** @var \DateTime */ |
||
29 | private $dateTime; |
||
30 | |||
31 | /** |
||
32 | * @static |
||
33 | * @access public |
||
34 | * @param \DateTime $birthday |
||
35 | * @return Date |
||
36 | * |
||
37 | * @throws \InvalidArgumentException |
||
38 | * @throws \OutOfRangeException |
||
39 | * @throws \DomainException |
||
40 | * @throws \LengthException |
||
41 | */ |
||
42 | 6 | public static function fromDateTime(\DateTime $birthday) |
|
43 | { |
||
44 | 6 | $date = clone $birthday; |
|
45 | |||
46 | 6 | return new self( |
|
47 | 6 | new Year($date->format('Y')), |
|
48 | 6 | new Month($date->format('m')), |
|
49 | 6 | new Day($date->format('d')) |
|
50 | ); |
||
51 | } |
||
52 | |||
53 | /** |
||
54 | * @static |
||
55 | * @access public |
||
56 | * @param string $date |
||
57 | * @param string $format |
||
58 | * @return Date |
||
59 | * |
||
60 | * @throws \InvalidArgumentException |
||
61 | * @throws \OutOfRangeException |
||
62 | * @throws \DomainException |
||
63 | * @throws \LengthException |
||
64 | */ |
||
65 | 1 | public static function fromString($date, $format = \DateTime::ATOM) |
|
76 | |||
77 | /** |
||
78 | * @param Year $year |
||
79 | * @param Month $month |
||
80 | * @param Day $day |
||
81 | * |
||
82 | * @throws \OutOfRangeException |
||
83 | * @throws \DomainException |
||
84 | */ |
||
85 | 23 | public function __construct(Year $year, Month $month, Day $day) |
|
86 | { |
||
87 | 23 | if ((int)sprintf('%s', $day) > ($daysNo = $this->getDaysInMonth($month, $year))) { |
|
88 | 2 | throw new \OutOfRangeException( |
|
89 | 2 | sprintf('Month %s of %s has only %d days.', $month, $year, $daysNo) |
|
90 | ); |
||
91 | } |
||
92 | |||
93 | 21 | $this->dateTime = \DateTime::createFromFormat('Y-m-d', sprintf('%s-%s-%s', $year, $month, $day)); |
|
94 | |||
95 | // @codeCoverageIgnoreStart |
||
96 | $err = \DateTime::getLastErrors(); |
||
97 | if ($err['error_count'] > 0) { |
||
98 | throw new \DomainException(sprintf('%s', implode(', ', $err['errors']))); |
||
99 | } |
||
100 | // @codeCoverageIgnoreStop |
||
101 | |||
102 | $this->year = $year; |
||
103 | $this->month = $month; |
||
104 | $this->day = $day; |
||
105 | } |
||
106 | |||
107 | /** |
||
108 | * {@inheritdoc} |
||
109 | */ |
||
110 | public function equals(Embeddable $object) |
||
119 | |||
120 | /** |
||
121 | * Returned format is `Y-m-d`. |
||
122 | * |
||
123 | * {@inheritdoc} |
||
124 | */ |
||
125 | public function __toString() |
||
129 | |||
130 | /** |
||
131 | * @access public |
||
132 | * @return Year |
||
133 | */ |
||
134 | public function getYear() |
||
138 | |||
139 | /** |
||
140 | * @access public |
||
141 | * @return Month |
||
142 | */ |
||
143 | public function getMonth() |
||
147 | |||
148 | /** |
||
149 | * @access public |
||
150 | * @return Day |
||
151 | */ |
||
152 | public function getDay() |
||
156 | |||
157 | /** |
||
158 | * @access public |
||
159 | * @return \DateTime |
||
160 | */ |
||
161 | public function asDateTime() |
||
165 | |||
166 | /** |
||
167 | * Get number of days in specified month. |
||
168 | * |
||
169 | * @access private |
||
170 | * @param Month $month |
||
171 | * @param Year $year |
||
172 | * @return int |
||
173 | */ |
||
174 | private function getDaysInMonth(Month $month, Year $year) |
||
178 | } |
||
179 |