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 | 71 | public function __construct(LoggerInterface $logger = null) |
|
69 | |||
70 | /** |
||
71 | * @param $dateFormat |
||
72 | * @return $this |
||
73 | */ |
||
74 | 23 | public function addDateFormat($dateFormat) |
|
80 | |||
81 | /** |
||
82 | * @param array $dateFormats |
||
83 | * @return $this |
||
84 | */ |
||
85 | 3 | public function setDateFormats(array $dateFormats) |
|
86 | { |
||
87 | 3 | $this->dateFormats = $dateFormats; |
|
88 | |||
89 | 3 | return $this; |
|
90 | } |
||
91 | |||
92 | /** |
||
93 | * @return string |
||
94 | */ |
||
95 | 11 | 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 | 13 | public function guessDateFormat($date) |
|
107 | { |
||
108 | 13 | foreach ($this->dateFormats as $format) { |
|
109 | 13 | $test = \DateTime::createFromFormat($format, $date); |
|
110 | 13 | if ($test instanceof \DateTime) { |
|
111 | 11 | $this->lastGuessedFormat = $format; |
|
112 | |||
113 | 11 | return $format; |
|
114 | } |
||
115 | 9 | } |
|
116 | |||
117 | 2 | return false; |
|
118 | } |
||
119 | |||
120 | /** |
||
121 | * Creates a DateTime instance for the given string. Default format is RFC2822 |
||
122 | * @param string $string |
||
123 | * @return \DateTime |
||
124 | */ |
||
125 | 11 | public function convertToDateTime($string) |
|
126 | { |
||
127 | 11 | $string = trim($string); |
|
128 | 11 | foreach ([$this->getLastGuessedFormat(), $this->guessDateFormat($string) ] as $format) { |
|
129 | 11 | $date = $this->newDate($format, $string); |
|
130 | 11 | if ($date instanceof \DateTime) { |
|
131 | 10 | $date->setTimezone($this->getTimezone()); |
|
132 | |||
133 | 10 | return $date; |
|
134 | } |
||
135 | 8 | } |
|
136 | |||
137 | 1 | return $this->stringToDateTime($string); |
|
138 | } |
||
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 | 2 | public function stringToDateTime($string) |
|
147 | { |
||
148 | 2 | $this->logger->notice("unsupported date format, use strtotime() to build the DateTime instance : {$string}"); |
|
149 | |||
150 | 2 | if (false === strtotime($string)) { |
|
151 | 1 | throw new \InvalidArgumentException('Impossible to convert date : '.$string); |
|
152 | } |
||
153 | 1 | $date = new \DateTime($string, $this->getFeedTimezone()); |
|
154 | 1 | $date->setTimezone($this->getTimezone()); |
|
155 | |||
156 | 1 | return $date; |
|
157 | } |
||
158 | |||
159 | /** |
||
160 | * @return \DateTimeZone |
||
161 | */ |
||
162 | 12 | 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 | 12 | public function getServerTimezone() |
|
189 | { |
||
190 | 12 | return $this->serverTimezone; |
|
191 | } |
||
192 | |||
193 | /** |
||
194 | * @param \DateTimeZone $timezone |
||
195 | */ |
||
196 | 71 | public function setServerTimezone(\DateTimeZone $timezone) |
|
200 | |||
201 | /** |
||
202 | * @return \DateTimeZone |
||
203 | */ |
||
204 | 12 | public function getTimezone() |
|
205 | { |
||
206 | 12 | return $this->getServerTimezone(); |
|
207 | } |
||
208 | |||
209 | /** |
||
210 | * @param \DateTimeZone $timezone |
||
211 | */ |
||
212 | 71 | public function setTimezone(\DateTimeZone $timezone) |
|
216 | |||
217 | |||
218 | /** |
||
219 | * @param $format |
||
220 | * @param $string |
||
221 | * @return \DateTime |
||
222 | */ |
||
223 | 11 | protected function newDate($format, $string) |
|
231 | } |
||
232 |