1 | <?php declare(strict_types=1); |
||
16 | class DateTimeBuilder |
||
17 | { |
||
18 | /** |
||
19 | * Supported date formats |
||
20 | * @var array |
||
21 | */ |
||
22 | protected $dateFormats = [ |
||
23 | \DateTime::RFC2822, |
||
24 | \DateTime::ATOM, |
||
25 | \DateTime::RFC3339, |
||
26 | \DateTime::RSS, |
||
27 | \DateTime::W3C, |
||
28 | 'Y-m-d\TH:i:s.uP', |
||
29 | 'Y-m-d\TH:i:s', |
||
30 | 'Y-m-d', |
||
31 | 'd/m/Y', |
||
32 | 'D, d M Y H:i O', |
||
33 | 'D, d M Y H:i:s O', |
||
34 | 'D M d Y H:i:s e', |
||
35 | '*, m#d#Y - H:i', |
||
36 | ]; |
||
37 | |||
38 | /** |
||
39 | * @var \DateTimeZone |
||
40 | */ |
||
41 | protected $feedTimezone; |
||
42 | |||
43 | /** |
||
44 | * @var \DateTimeZone |
||
45 | */ |
||
46 | protected $serverTimezone; |
||
47 | |||
48 | /** |
||
49 | * @var LoggerInterface |
||
50 | */ |
||
51 | protected $logger; |
||
52 | |||
53 | /** |
||
54 | * @var string |
||
55 | */ |
||
56 | protected $lastGuessedFormat = \DateTime::RFC2822; |
||
57 | |||
58 | /** |
||
59 | * @param \Psr\Log\LoggerInterface $logger |
||
60 | */ |
||
61 | 72 | public function __construct(LoggerInterface $logger = null) |
|
62 | { |
||
63 | 72 | if (is_null($logger)) { |
|
64 | 62 | $logger = new NullLogger; |
|
65 | } |
||
66 | 72 | $this->logger = $logger; |
|
67 | 72 | $this->setTimezone(new \DateTimeZone(date_default_timezone_get())); |
|
68 | 72 | } |
|
69 | |||
70 | /** |
||
71 | * @param $dateFormat |
||
72 | * @return DateTimeBuilder |
||
73 | */ |
||
74 | 24 | public function addDateFormat(string $dateFormat) : DateTimeBuilder |
|
80 | |||
81 | /** |
||
82 | * @param array $dateFormats |
||
83 | * @return $this |
||
84 | */ |
||
85 | 3 | public function setDateFormats(array $dateFormats) : DateTimeBuilder |
|
86 | { |
||
87 | 3 | $this->dateFormats = $dateFormats; |
|
88 | |||
89 | 3 | return $this; |
|
90 | } |
||
91 | |||
92 | /** |
||
93 | * @return string |
||
94 | */ |
||
95 | 11 | public function getLastGuessedFormat() : string |
|
99 | |||
100 | /** |
||
101 | * Tries to guess the date's format from the list |
||
102 | * @param string $date |
||
103 | * @return string|null date Format |
||
104 | */ |
||
105 | 13 | public function guessDateFormat(string $date) : ? string |
|
118 | |||
119 | /** |
||
120 | * Creates a DateTime instance for the given string. Default format is RFC2822 |
||
121 | * @param string $string |
||
122 | * @return \DateTime |
||
123 | */ |
||
124 | 11 | public function convertToDateTime(string $string) : \DateTime |
|
138 | |||
139 | /** |
||
140 | * Creates a DateTime instance for the given string if the format was not catch from the list |
||
141 | * @param string $string |
||
142 | * @return \DateTime |
||
143 | * @throws InvalidArgumentException |
||
144 | */ |
||
145 | 2 | public function stringToDateTime(string $string) : \DateTime |
|
157 | |||
158 | /** |
||
159 | * @return \DateTimeZone |
||
160 | */ |
||
161 | 12 | public function getFeedTimezone() : ? \DateTimeZone |
|
165 | |||
166 | /** |
||
167 | * Specifies the feed's timezone. Do this it the timezone is missing |
||
168 | * |
||
169 | * @param \DateTimeZone $timezone |
||
170 | */ |
||
171 | public function setFeedTimezone(\DateTimeZone $timezone) : void |
||
175 | |||
176 | /** |
||
177 | * Resets feedTimezone to null. |
||
178 | */ |
||
179 | public function resetFeedTimezone() : void |
||
183 | |||
184 | /** |
||
185 | * @return \DateTimeZone |
||
186 | */ |
||
187 | 12 | public function getServerTimezone() : ? \DateTimeZone |
|
191 | |||
192 | /** |
||
193 | * @param \DateTimeZone $timezone |
||
194 | */ |
||
195 | 72 | public function setServerTimezone(\DateTimeZone $timezone) : void |
|
199 | |||
200 | /** |
||
201 | * @return \DateTimeZone |
||
202 | */ |
||
203 | 12 | public function getTimezone() : ? \DateTimeZone |
|
207 | |||
208 | /** |
||
209 | * @param \DateTimeZone $timezone |
||
210 | */ |
||
211 | 72 | public function setTimezone(\DateTimeZone $timezone) : void |
|
215 | |||
216 | |||
217 | /** |
||
218 | * @param $format |
||
219 | * @param $string |
||
220 | * @return \DateTime |
||
221 | */ |
||
222 | 11 | protected function newDate(string $format, string $string) |
|
230 | } |
||
231 |