1 | <?php declare(strict_types=1); |
||
16 | class DateTimeBuilder implements DateTimeBuilderInterface |
||
17 | { |
||
18 | /** |
||
19 | * Supported date formats |
||
20 | * @var array |
||
21 | */ |
||
22 | protected $dateFormats = [ |
||
23 | \DateTime::RFC2822, |
||
24 | \DateTime::ATOM, |
||
25 | \DateTime::RFC3339, |
||
26 | \DateTime::RFC3339_EXTENDED, |
||
27 | \DateTime::RSS, |
||
28 | \DateTime::W3C, |
||
29 | 'Y-m-d\TH:i:s.uP', |
||
30 | 'Y-m-d\TH:i:s.uvP', |
||
31 | 'Y-m-d\TH:i:s', |
||
32 | 'Y-m-d', |
||
33 | 'd/m/Y', |
||
34 | 'D, d M Y H:i O', |
||
35 | 'D, d M Y H:i:s O', |
||
36 | 'D M d Y H:i:s e', |
||
37 | '*, m#d#Y - H:i', |
||
38 | 'D, d M Y H:i:s \U\T', |
||
39 | ]; |
||
40 | |||
41 | /** |
||
42 | * @var \DateTimeZone |
||
43 | */ |
||
44 | protected $feedTimezone; |
||
45 | |||
46 | /** |
||
47 | * @var \DateTimeZone |
||
48 | */ |
||
49 | protected $serverTimezone; |
||
50 | |||
51 | /** |
||
52 | * @var LoggerInterface |
||
53 | */ |
||
54 | protected $logger; |
||
55 | |||
56 | /** |
||
57 | * @var string |
||
58 | */ |
||
59 | protected $lastGuessedFormat = \DateTime::RFC2822; |
||
60 | |||
61 | /** |
||
62 | 80 | * @param \Psr\Log\LoggerInterface $logger |
|
63 | */ |
||
64 | 80 | public function __construct(LoggerInterface $logger = null) |
|
72 | |||
73 | /** |
||
74 | * @param $dateFormat |
||
75 | 24 | * @return DateTimeBuilder |
|
76 | */ |
||
77 | 24 | public function addDateFormat(string $dateFormat) : DateTimeBuilderInterface |
|
83 | |||
84 | /** |
||
85 | * @param array $dateFormats |
||
86 | 3 | * @return $this |
|
87 | */ |
||
88 | 3 | public function setDateFormats(array $dateFormats) : DateTimeBuilderInterface |
|
94 | |||
95 | /** |
||
96 | 15 | * @return string |
|
97 | */ |
||
98 | 15 | public function getLastGuessedFormat() : string |
|
102 | |||
103 | /** |
||
104 | * Tries to guess the date's format from the list |
||
105 | * @param string $date |
||
106 | 17 | * @return string|null date Format |
|
107 | */ |
||
108 | 17 | public function guessDateFormat(string $date) : ? string |
|
121 | |||
122 | /** |
||
123 | * Creates a DateTime instance for the given string. Default format is RFC2822 |
||
124 | * @param string $string |
||
125 | 15 | * @return \DateTime |
|
126 | */ |
||
127 | 15 | public function convertToDateTime(string $string) : \DateTime |
|
141 | |||
142 | /** |
||
143 | * Creates a DateTime instance for the given string if the format was not catch from the list |
||
144 | * @param string $string |
||
145 | * @return \DateTime |
||
146 | 2 | * @throws InvalidArgumentException |
|
147 | */ |
||
148 | 2 | public function stringToDateTime(string $string) : \DateTime |
|
160 | |||
161 | /** |
||
162 | 16 | * @return \DateTimeZone |
|
163 | */ |
||
164 | 16 | public function getFeedTimezone() : ? \DateTimeZone |
|
168 | |||
169 | /** |
||
170 | * Specifies the feed's timezone. Do this it the timezone is missing |
||
171 | * |
||
172 | * @param \DateTimeZone $timezone |
||
173 | */ |
||
174 | public function setFeedTimezone(\DateTimeZone $timezone) : void |
||
178 | |||
179 | /** |
||
180 | * Resets feedTimezone to null. |
||
181 | */ |
||
182 | public function resetFeedTimezone() : void |
||
186 | |||
187 | /** |
||
188 | 16 | * @return \DateTimeZone |
|
189 | */ |
||
190 | 16 | public function getServerTimezone() : ? \DateTimeZone |
|
194 | |||
195 | /** |
||
196 | 80 | * @param \DateTimeZone $timezone |
|
197 | */ |
||
198 | 80 | public function setServerTimezone(\DateTimeZone $timezone) : void |
|
202 | |||
203 | /** |
||
204 | 16 | * @return \DateTimeZone |
|
205 | */ |
||
206 | 16 | public function getTimezone() : ? \DateTimeZone |
|
210 | |||
211 | /** |
||
212 | 80 | * @param \DateTimeZone $timezone |
|
213 | */ |
||
214 | 80 | public function setTimezone(\DateTimeZone $timezone) : void |
|
218 | |||
219 | /** |
||
220 | * @param $format |
||
221 | * @param $string |
||
222 | 15 | * @return \DateTime |
|
223 | */ |
||
224 | 15 | protected function newDate(string $format, string $string) |
|
232 | } |
||
233 |