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 | '*, d M* Y H:i:s e', |
||
40 | '*, d M Y', |
||
41 | ]; |
||
42 | |||
43 | /** |
||
44 | * @var \DateTimeZone |
||
45 | */ |
||
46 | protected $feedTimezone; |
||
47 | |||
48 | /** |
||
49 | * @var \DateTimeZone |
||
50 | */ |
||
51 | protected $serverTimezone; |
||
52 | |||
53 | /** |
||
54 | * @var LoggerInterface |
||
55 | */ |
||
56 | protected $logger; |
||
57 | |||
58 | /** |
||
59 | * @var string |
||
60 | */ |
||
61 | protected $lastGuessedFormat = \DateTime::RFC2822; |
||
62 | |||
63 | /** |
||
64 | * @param \Psr\Log\LoggerInterface $logger |
||
65 | */ |
||
66 | 78 | public function __construct(LoggerInterface $logger = null) |
|
74 | |||
75 | /** |
||
76 | * @param $dateFormat |
||
77 | * @return DateTimeBuilder |
||
78 | */ |
||
79 | 22 | public function addDateFormat(string $dateFormat) : DateTimeBuilderInterface |
|
85 | |||
86 | /** |
||
87 | * @param array $dateFormats |
||
88 | * @return $this |
||
89 | */ |
||
90 | 2 | public function setDateFormats(array $dateFormats) : DateTimeBuilderInterface |
|
96 | |||
97 | /** |
||
98 | * @return string |
||
99 | */ |
||
100 | 15 | public function getLastGuessedFormat() : string |
|
104 | |||
105 | /** |
||
106 | * Tries to guess the date's format from the list |
||
107 | * @param string $date |
||
108 | * @return string|null date Format |
||
109 | */ |
||
110 | 17 | public function guessDateFormat(string $date) : ? string |
|
123 | |||
124 | /** |
||
125 | * Creates a DateTime instance for the given string. Default format is RFC2822 |
||
126 | * @param string $string |
||
127 | * @return \DateTime |
||
128 | */ |
||
129 | 15 | public function convertToDateTime(string $string) : \DateTime |
|
143 | |||
144 | /** |
||
145 | * Creates a DateTime instance for the given string if the format was not catch from the list |
||
146 | * @param string $string |
||
147 | * @return \DateTime |
||
148 | * @throws InvalidArgumentException |
||
149 | */ |
||
150 | 2 | public function stringToDateTime(string $string) : \DateTime |
|
162 | |||
163 | /** |
||
164 | * @return \DateTimeZone |
||
165 | */ |
||
166 | 16 | public function getFeedTimezone() : ? \DateTimeZone |
|
170 | |||
171 | /** |
||
172 | * Specifies the feed's timezone. Do this it the timezone is missing |
||
173 | * |
||
174 | * @param \DateTimeZone $timezone |
||
175 | */ |
||
176 | public function setFeedTimezone(\DateTimeZone $timezone) : void |
||
180 | |||
181 | /** |
||
182 | * Resets feedTimezone to null. |
||
183 | */ |
||
184 | public function resetFeedTimezone() : void |
||
188 | |||
189 | /** |
||
190 | * @return \DateTimeZone |
||
191 | */ |
||
192 | 16 | public function getServerTimezone() : ? \DateTimeZone |
|
196 | |||
197 | /** |
||
198 | * @param \DateTimeZone $timezone |
||
199 | */ |
||
200 | 78 | public function setServerTimezone(\DateTimeZone $timezone) : void |
|
204 | |||
205 | /** |
||
206 | * @return \DateTimeZone |
||
207 | */ |
||
208 | 16 | public function getTimezone() : ? \DateTimeZone |
|
212 | |||
213 | /** |
||
214 | * @param \DateTimeZone $timezone |
||
215 | */ |
||
216 | 78 | public function setTimezone(\DateTimeZone $timezone) : void |
|
220 | |||
221 | /** |
||
222 | * @param $format |
||
223 | * @param $string |
||
224 | * @return \DateTime |
||
225 | */ |
||
226 | 15 | protected function newDate(string $format, string $string) |
|
234 | } |
||
235 |