|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of the feed-io package. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Alexandre Debril <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace FeedIo\Rule; |
|
12
|
|
|
|
|
13
|
|
|
use Psr\Log\LoggerInterface; |
|
14
|
|
|
use Psr\Log\NullLogger; |
|
15
|
|
|
|
|
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) |
|
62
|
|
|
{ |
|
63
|
11 |
|
if (is_null($logger)) { |
|
64
|
2 |
|
$logger = new NullLogger; |
|
65
|
2 |
|
} |
|
66
|
11 |
|
$this->logger = $logger; |
|
67
|
11 |
|
$this->setTimezone(new \DateTimeZone(date_default_timezone_get())); |
|
68
|
11 |
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @param $dateFormat |
|
72
|
|
|
* @return $this |
|
73
|
|
|
*/ |
|
74
|
1 |
|
public function addDateFormat($dateFormat) |
|
75
|
|
|
{ |
|
76
|
1 |
|
$this->dateFormats[] = $dateFormat; |
|
77
|
|
|
|
|
78
|
1 |
|
return $this; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @param array $dateFormats |
|
83
|
|
|
* @return $this |
|
84
|
|
|
*/ |
|
85
|
|
|
public function setDateFormats(array $dateFormats) |
|
86
|
|
|
{ |
|
87
|
|
|
$this->dateFormats = $dateFormats; |
|
88
|
|
|
|
|
89
|
|
|
return $this; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @return string |
|
94
|
|
|
*/ |
|
95
|
1 |
|
public function getLastGuessedFormat() |
|
96
|
|
|
{ |
|
97
|
1 |
|
return $this->lastGuessedFormat; |
|
98
|
|
|
} |
|
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) |
|
107
|
|
|
{ |
|
108
|
1 |
|
foreach ($this->dateFormats as $format) { |
|
109
|
1 |
|
$test = \DateTime::createFromFormat($format, $date); |
|
110
|
1 |
|
if ($test instanceof \DateTime) { |
|
111
|
1 |
|
$this->lastGuessedFormat = $format; |
|
112
|
|
|
|
|
113
|
1 |
|
return $format; |
|
114
|
|
|
} |
|
115
|
1 |
|
} |
|
116
|
|
|
|
|
117
|
|
|
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
|
1 |
|
public function convertToDateTime($string) |
|
126
|
|
|
{ |
|
127
|
1 |
|
$string = trim($string); |
|
128
|
1 |
|
foreach ([$this->getLastGuessedFormat(), $this->guessDateFormat($string) ] as $format) { |
|
129
|
1 |
|
$date = \DateTime::createFromFormat($format, $string, $this->getFeedTimezone()); |
|
130
|
|
|
if ($date instanceof \DateTime) { |
|
131
|
|
|
$date->setTimezone($this->getTimezone()); |
|
132
|
|
|
|
|
133
|
|
|
return $date; |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
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
|
|
|
public function stringToDateTime($string) |
|
147
|
|
|
{ |
|
148
|
|
|
$this->logger->notice("unsupported date format, use strtotime() to build the DateTime instance : {$string}"); |
|
149
|
|
|
|
|
150
|
|
|
if (false === strtotime($string)) { |
|
151
|
|
|
throw new \InvalidArgumentException('Impossible to convert date : '.$string); |
|
152
|
|
|
} |
|
153
|
|
|
$date = new \DateTime($string); |
|
154
|
|
|
$date->setTimezone($this->getTimezone()); |
|
155
|
|
|
|
|
156
|
|
|
return $date; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* @return \DateTimeZone |
|
161
|
|
|
*/ |
|
162
|
1 |
|
public function getFeedTimezone() |
|
163
|
|
|
{ |
|
164
|
1 |
|
return $this->feedTimezone; |
|
165
|
|
|
} |
|
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) |
|
173
|
|
|
{ |
|
174
|
|
|
$this->feedTimezone = $timezone; |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* Resets feedTimezone to null. |
|
179
|
|
|
*/ |
|
180
|
|
|
public function resetFeedTimezone() |
|
181
|
|
|
{ |
|
182
|
|
|
$this->feedTimezone = null; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* @return \DateTimeZone |
|
187
|
|
|
*/ |
|
188
|
|
|
public function getServerTimezone() |
|
189
|
|
|
{ |
|
190
|
|
|
return $this->serverTimezone; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* @param \DateTimeZone $timezone |
|
195
|
|
|
*/ |
|
196
|
11 |
|
public function setServerTimezone(\DateTimeZone $timezone) |
|
197
|
|
|
{ |
|
198
|
11 |
|
$this->serverTimezone = $timezone; |
|
199
|
11 |
|
} |
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* @return \DateTimeZone |
|
203
|
|
|
*/ |
|
204
|
|
|
public function getTimezone() |
|
205
|
|
|
{ |
|
206
|
|
|
return $this->getServerTimezone(); |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
/** |
|
210
|
|
|
* @param \DateTimeZone $timezone |
|
211
|
|
|
*/ |
|
212
|
11 |
|
public function setTimezone(\DateTimeZone $timezone) |
|
213
|
|
|
{ |
|
214
|
11 |
|
$this->setServerTimezone($timezone); |
|
215
|
11 |
|
} |
|
216
|
|
|
} |
|
217
|
|
|
|