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) |
|
69 | |||
70 | /** |
||
71 | * @param $dateFormat |
||
72 | * @return DateTimeBuilder |
||
73 | */ |
||
74 | 23 | 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 | 12 | 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 | public function guessDateFormat(string $date) : ? string |
||
106 | 14 | { |
|
107 | foreach ($this->dateFormats as $format) { |
||
108 | 14 | $test = \DateTime::createFromFormat($format, $date); |
|
109 | 14 | if ($test instanceof \DateTime) { |
|
110 | 14 | $this->lastGuessedFormat = $format; |
|
111 | 12 | ||
112 | return $format; |
||
113 | 12 | } |
|
114 | } |
||
115 | 9 | ||
116 | return null; |
||
117 | 2 | } |
|
118 | |||
119 | /** |
||
120 | * Creates a DateTime instance for the given string. Default format is RFC2822 |
||
121 | * @param string $string |
||
122 | * @return \DateTime |
||
123 | */ |
||
124 | public function convertToDateTime(string $string) : \DateTime |
||
125 | 12 | { |
|
126 | $string = trim($string); |
||
127 | 12 | foreach ([$this->getLastGuessedFormat(), $this->guessDateFormat($string) ] as $format) { |
|
128 | 12 | $date = $this->newDate((string) $format, $string); |
|
129 | 12 | if ($date instanceof \DateTime) { |
|
130 | 12 | $date->setTimezone($this->getTimezone()); |
|
131 | 11 | ||
132 | return $date; |
||
133 | 11 | } |
|
134 | } |
||
135 | 8 | ||
136 | return $this->stringToDateTime($string); |
||
137 | 1 | } |
|
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 | public function stringToDateTime(string $string) : \DateTime |
||
157 | |||
158 | /** |
||
159 | * @return \DateTimeZone |
||
160 | */ |
||
161 | 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 |
||
172 | { |
||
173 | $this->feedTimezone = $timezone; |
||
174 | } |
||
175 | |||
176 | /** |
||
177 | * Resets feedTimezone to null. |
||
178 | */ |
||
179 | public function resetFeedTimezone() : void |
||
180 | { |
||
181 | $this->feedTimezone = null; |
||
182 | } |
||
183 | |||
184 | /** |
||
185 | * @return \DateTimeZone |
||
186 | */ |
||
187 | public function getServerTimezone() : ? \DateTimeZone |
||
191 | |||
192 | /** |
||
193 | * @param \DateTimeZone $timezone |
||
194 | */ |
||
195 | public function setServerTimezone(\DateTimeZone $timezone) : void |
||
196 | 72 | { |
|
197 | $this->serverTimezone = $timezone; |
||
198 | 72 | } |
|
199 | 72 | ||
200 | /** |
||
201 | * @return \DateTimeZone |
||
202 | */ |
||
203 | public function getTimezone() : ? \DateTimeZone |
||
207 | |||
208 | /** |
||
209 | * @param \DateTimeZone $timezone |
||
210 | */ |
||
211 | public function setTimezone(\DateTimeZone $timezone) : void |
||
212 | 72 | { |
|
213 | $this->setServerTimezone($timezone); |
||
214 | 72 | } |
|
215 | 72 | ||
216 | |||
217 | /** |
||
218 | * @param $format |
||
219 | * @param $string |
||
220 | * @return \DateTime |
||
221 | */ |
||
222 | protected function newDate(string $format, string $string) |
||
230 | } |
||
231 |