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
|
|
|
class DateTimeBuilder |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* Supported date formats |
17
|
|
|
* @var array |
18
|
|
|
*/ |
19
|
|
|
protected $dateFormats = [ |
20
|
|
|
\DateTime::RFC2822, |
21
|
|
|
\DateTime::ATOM, |
22
|
|
|
\DateTime::RFC3339, |
23
|
|
|
\DateTime::RSS, |
24
|
|
|
\DateTime::W3C, |
25
|
|
|
'Y-m-d\TH:i:s.uP', |
26
|
|
|
'Y-m-d', |
27
|
|
|
'd/m/Y', |
28
|
|
|
'D, d M Y H:i O', |
29
|
|
|
'D, d M Y H:i:s O', |
30
|
|
|
'D M d Y H:i:s e', |
31
|
|
|
]; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var \DateTimeZone |
35
|
|
|
*/ |
36
|
|
|
protected $timezone; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
protected $lastGuessedFormat = \DateTime::RFC2822; |
42
|
|
|
|
43
|
64 |
|
public function __construct() |
44
|
|
|
{ |
45
|
64 |
|
$this->setTimezone(new \DateTimeZone(date_default_timezone_get())); |
46
|
64 |
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param $dateFormat |
50
|
|
|
* @return $this |
51
|
|
|
*/ |
52
|
21 |
|
public function addDateFormat($dateFormat) |
53
|
|
|
{ |
54
|
21 |
|
$this->dateFormats[] = $dateFormat; |
55
|
|
|
|
56
|
21 |
|
return $this; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param array $dateFormats |
61
|
|
|
* @return $this |
62
|
|
|
*/ |
63
|
3 |
|
public function setDateFormats(array $dateFormats) |
64
|
|
|
{ |
65
|
3 |
|
$this->dateFormats = $dateFormats; |
66
|
|
|
|
67
|
3 |
|
return $this; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return string |
72
|
|
|
*/ |
73
|
10 |
|
public function getLastGuessedFormat() |
74
|
|
|
{ |
75
|
10 |
|
return $this->lastGuessedFormat; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* |
80
|
|
|
* @param string $date |
81
|
|
|
* @return string|false date Format |
82
|
|
|
* @throws InvalidArgumentException |
83
|
|
|
*/ |
84
|
12 |
|
public function guessDateFormat($date) |
85
|
|
|
{ |
86
|
12 |
|
foreach ($this->dateFormats as $format) { |
87
|
12 |
|
$test = \DateTime::createFromFormat($format, $date); |
88
|
12 |
|
if ($test instanceof \DateTime) { |
89
|
10 |
|
$this->lastGuessedFormat = $format; |
90
|
|
|
|
91
|
10 |
|
return $format; |
92
|
|
|
} |
93
|
8 |
|
} |
94
|
|
|
|
95
|
2 |
|
return false; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Creates a DateTime instance for the given string. Default format is RFC2822 |
100
|
|
|
* @param string $string |
101
|
|
|
* @return \DateTime |
102
|
|
|
* @throws InvalidArgumentException |
103
|
|
|
*/ |
104
|
10 |
|
public function convertToDateTime($string) |
105
|
|
|
{ |
106
|
10 |
|
$string = trim($string); |
107
|
10 |
|
foreach ([$this->getLastGuessedFormat(), $this->guessDateFormat($string) ] as $format) { |
108
|
10 |
|
$date = \DateTime::createFromFormat($format, $string); |
109
|
10 |
|
if ($date instanceof \DateTime) { |
110
|
9 |
|
$date->setTimezone($this->getTimezone()); |
111
|
|
|
|
112
|
9 |
|
return $date; |
113
|
|
|
} |
114
|
7 |
|
} |
115
|
|
|
|
116
|
1 |
|
throw new \InvalidArgumentException('Impossible to convert date : '.$string); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @return \DateTimeZone |
121
|
|
|
*/ |
122
|
10 |
|
public function getTimezone() |
123
|
|
|
{ |
124
|
10 |
|
return $this->timezone; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param \DateTimeZone $timezone |
129
|
|
|
*/ |
130
|
64 |
|
public function setTimezone(\DateTimeZone $timezone) |
131
|
|
|
{ |
132
|
64 |
|
$this->timezone = $timezone; |
133
|
64 |
|
} |
134
|
|
|
} |
135
|
|
|
|