1 | <?php |
||
6 | class FlexDate |
||
7 | { |
||
8 | |||
9 | const PRECISION_YEAR = 'year'; |
||
10 | |||
11 | const PRECISION_MONTH = 'month'; |
||
12 | |||
13 | const PRECISION_DAY = 'day'; |
||
14 | |||
15 | /** |
||
16 | * @var int|null |
||
17 | */ |
||
18 | protected $year; |
||
19 | |||
20 | /** |
||
21 | * @var int|null |
||
22 | */ |
||
23 | protected $month; |
||
24 | |||
25 | /** |
||
26 | * @var int|null |
||
27 | */ |
||
28 | protected $day; |
||
29 | |||
30 | /** |
||
31 | * @param int|null $year |
||
32 | * @param int|null $month |
||
33 | * @param int|null $day |
||
34 | */ |
||
35 | 7 | public function __construct($year = null, $month = null, $day = null) |
|
36 | { |
||
37 | 7 | $this->year = $year; |
|
38 | 7 | $this->month = $month; |
|
39 | 7 | $this->day = $day; |
|
40 | 7 | } |
|
41 | |||
42 | /** |
||
43 | * @return int|null |
||
44 | */ |
||
45 | 1 | public function getYear() |
|
46 | { |
||
47 | 1 | return $this->year; |
|
48 | } |
||
49 | |||
50 | /** |
||
51 | * @param int|null $year |
||
52 | */ |
||
53 | 5 | public function setYear($year) |
|
54 | { |
||
55 | 5 | $this->year = $year; |
|
56 | 5 | } |
|
57 | |||
58 | /** |
||
59 | * @return int|null |
||
60 | */ |
||
61 | 1 | public function getMonth() |
|
62 | { |
||
63 | 1 | return $this->month; |
|
64 | } |
||
65 | |||
66 | /** |
||
67 | * @param int|null $month |
||
68 | */ |
||
69 | 5 | public function setMonth($month) |
|
70 | { |
||
71 | 5 | $this->month = $month; |
|
72 | 5 | } |
|
73 | |||
74 | /** |
||
75 | * @return int|null |
||
76 | */ |
||
77 | 1 | public function getDay() |
|
78 | { |
||
79 | 1 | return $this->day; |
|
80 | } |
||
81 | |||
82 | /** |
||
83 | * @param int|null $day |
||
84 | */ |
||
85 | 5 | public function setDay($day) |
|
86 | { |
||
87 | 5 | $this->day = $day; |
|
88 | 5 | } |
|
89 | |||
90 | /** |
||
91 | * @return bool |
||
92 | */ |
||
93 | 1 | public function hasValue() |
|
94 | { |
||
95 | 1 | return null !== $this->year || null !== $this->month || null !== $this->day; |
|
96 | } |
||
97 | |||
98 | /** |
||
99 | * @return bool |
||
100 | */ |
||
101 | 2 | public function isCompleteDate() |
|
102 | { |
||
103 | 2 | return null !== $this->year && null !== $this->month && null !== $this->day; |
|
104 | } |
||
105 | |||
106 | /** |
||
107 | * @return bool |
||
108 | */ |
||
109 | 2 | public function isValid() |
|
110 | { |
||
111 | try { |
||
112 | 2 | $this->assertValid(); |
|
113 | |||
114 | 1 | return true; |
|
115 | 1 | } catch (\Exception $e) { |
|
116 | 1 | return false; |
|
117 | } |
||
118 | } |
||
119 | |||
120 | /** |
||
121 | * @return bool |
||
122 | * @throws \Exception |
||
123 | */ |
||
124 | 2 | public function assertValid() |
|
125 | { |
||
126 | 2 | if (null !== $this->day && null === $this->month) { |
|
127 | 1 | throw new \Exception('Day set, but no month'); |
|
128 | } |
||
129 | |||
130 | 2 | if (null !== $this->month && null === $this->year) { |
|
131 | 1 | throw new \Exception('Month, but no year'); |
|
132 | } |
||
133 | |||
134 | 1 | return true; |
|
135 | } |
||
136 | |||
137 | /** |
||
138 | * @return bool |
||
139 | */ |
||
140 | 2 | public function isValidDate() |
|
141 | { |
||
142 | 2 | return checkdate($this->month, $this->day, $this->year); |
|
143 | } |
||
144 | |||
145 | /** |
||
146 | * @¶eturn \DateTime |
||
147 | */ |
||
148 | 1 | public function toDateTime() |
|
149 | { |
||
150 | 1 | $dateTime = new \DateTime(); |
|
151 | 1 | $inferredYear = $this->year !== null ? $this->year : 0; |
|
152 | 1 | $inferredMonth = $this->month !== null ? $this->month : 1; |
|
153 | 1 | $inferredDay = $this->day !== null ? $this->day : 1; |
|
154 | 1 | $dateTime->setDate($inferredYear, $inferredMonth, $inferredDay); |
|
155 | |||
156 | 1 | return $dateTime; |
|
157 | } |
||
158 | |||
159 | /** |
||
160 | * {@inheritdoc} |
||
161 | */ |
||
162 | 1 | public function __toString() |
|
163 | { |
||
164 | 1 | $string = ''; |
|
165 | 1 | if (null !== $this->year) { |
|
166 | 1 | $string .= $this->year; |
|
167 | 1 | } |
|
168 | 1 | if (null !== $this->month) { |
|
169 | 1 | $string .= '-'; |
|
170 | 1 | $string .= str_pad($this->month, 2, '0', STR_PAD_LEFT); |
|
171 | 1 | } |
|
172 | 1 | if (null !== $this->day) { |
|
173 | 1 | $string .= '-'; |
|
174 | 1 | $string .= str_pad($this->day, 2, '0', STR_PAD_LEFT); |
|
175 | 1 | } |
|
176 | |||
177 | 1 | return $string; |
|
178 | } |
||
179 | |||
180 | /** |
||
181 | * @param string $dateString |
||
182 | * |
||
183 | * @return FlexDate |
||
184 | */ |
||
185 | 1 | public static function fromString($dateString) |
|
186 | { |
||
187 | 1 | $flexDate = new FlexDate(); |
|
188 | 1 | if (empty($dateString)) { |
|
189 | 1 | return $flexDate; |
|
190 | } |
||
191 | |||
192 | 1 | $parts = explode('-', $dateString); |
|
193 | 1 | $numParts = count($parts); |
|
194 | 1 | if ($numParts > 0) { |
|
195 | 1 | $flexDate->setYear((int)$parts[0]); |
|
196 | 1 | } |
|
197 | 1 | if ($numParts > 1) { |
|
198 | 1 | $flexDate->setMonth((int)$parts[1]); |
|
199 | 1 | } |
|
200 | 1 | if ($numParts > 2) { |
|
201 | 1 | $flexDate->setDay((int)$parts[2]); |
|
202 | 1 | } |
|
203 | |||
204 | 1 | return $flexDate; |
|
205 | } |
||
206 | |||
207 | public function getPrecision() |
||
223 | } |
||
224 |