1 | <?php |
||
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 | 11 | public function __construct(LoggerInterface $logger = null) |
|
69 | |||
70 | /** |
||
71 | * @param $dateFormat |
||
72 | * @return $this |
||
73 | */ |
||
74 | 1 | public function addDateFormat($dateFormat) |
|
80 | |||
81 | /** |
||
82 | * @param array $dateFormats |
||
83 | * @return $this |
||
84 | */ |
||
85 | public function setDateFormats(array $dateFormats) |
||
91 | |||
92 | /** |
||
93 | * @return string |
||
94 | */ |
||
95 | 1 | public function getLastGuessedFormat() |
|
99 | |||
100 | /** |
||
101 | * Tries to guess the date's format from the list |
||
102 | * @param string $date |
||
103 | * @return string|false date Format |
||
104 | * @throws InvalidArgumentException |
||
105 | */ |
||
106 | 1 | public function guessDateFormat($date) |
|
119 | |||
120 | /** |
||
121 | * Creates a DateTime instance for the given string. Default format is RFC2822 |
||
122 | * @param string $string |
||
123 | * @return \DateTime |
||
124 | */ |
||
125 | 1 | public function convertToDateTime($string) |
|
139 | |||
140 | /** |
||
141 | * Creates a DateTime instance for the given string if the format was not catch from the list |
||
142 | * @param string $string |
||
143 | * @return \DateTime |
||
144 | * @throws InvalidArgumentException |
||
145 | */ |
||
146 | public function stringToDateTime($string) |
||
158 | |||
159 | /** |
||
160 | * @return \DateTimeZone |
||
161 | */ |
||
162 | 1 | public function getFeedTimezone() |
|
166 | |||
167 | /** |
||
168 | * Specifies the feed's timezone. Do this it the timezone is missing |
||
169 | * |
||
170 | * @param \DateTimeZone $timezone |
||
171 | */ |
||
172 | public function setFeedTimezone(\DateTimeZone $timezone) |
||
176 | |||
177 | /** |
||
178 | * Resets feedTimezone to null. |
||
179 | */ |
||
180 | public function resetFeedTimezone() |
||
184 | |||
185 | /** |
||
186 | * @return \DateTimeZone |
||
187 | */ |
||
188 | public function getServerTimezone() |
||
192 | |||
193 | /** |
||
194 | * @param \DateTimeZone $timezone |
||
195 | */ |
||
196 | 11 | public function setServerTimezone(\DateTimeZone $timezone) |
|
200 | |||
201 | /** |
||
202 | * @return \DateTimeZone |
||
203 | */ |
||
204 | public function getTimezone() |
||
208 | |||
209 | /** |
||
210 | * @param \DateTimeZone $timezone |
||
211 | */ |
||
212 | 11 | public function setTimezone(\DateTimeZone $timezone) |
|
216 | } |
||
217 |