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 $timezone; |
||
42 | |||
43 | /** |
||
44 | * @var LoggerInterface |
||
45 | */ |
||
46 | protected $logger; |
||
47 | |||
48 | /** |
||
49 | * @var string |
||
50 | */ |
||
51 | protected $lastGuessedFormat = \DateTime::RFC2822; |
||
52 | |||
53 | /** |
||
54 | * @param \Psr\Log\LoggerInterface $logger |
||
55 | */ |
||
56 | 66 | public function __construct(LoggerInterface $logger = null) |
|
64 | |||
65 | /** |
||
66 | * @param $dateFormat |
||
67 | * @return $this |
||
68 | */ |
||
69 | 23 | public function addDateFormat($dateFormat) |
|
75 | |||
76 | /** |
||
77 | * @param array $dateFormats |
||
78 | * @return $this |
||
79 | */ |
||
80 | 3 | public function setDateFormats(array $dateFormats) |
|
81 | { |
||
82 | 3 | $this->dateFormats = $dateFormats; |
|
83 | |||
84 | 3 | return $this; |
|
85 | } |
||
86 | |||
87 | /** |
||
88 | * @return string |
||
89 | */ |
||
90 | 10 | public function getLastGuessedFormat() |
|
94 | |||
95 | /** |
||
96 | * Tries to guess the date's format from the list |
||
97 | * @param string $date |
||
98 | * @return string|false date Format |
||
99 | * @throws InvalidArgumentException |
||
100 | */ |
||
101 | 12 | public function guessDateFormat($date) |
|
102 | { |
||
103 | 12 | foreach ($this->dateFormats as $format) { |
|
104 | 12 | $test = \DateTime::createFromFormat($format, $date); |
|
105 | 12 | if ($test instanceof \DateTime) { |
|
106 | 10 | $this->lastGuessedFormat = $format; |
|
107 | |||
108 | 10 | return $format; |
|
109 | } |
||
110 | 8 | } |
|
111 | |||
112 | 2 | return false; |
|
113 | } |
||
114 | |||
115 | /** |
||
116 | * Creates a DateTime instance for the given string. Default format is RFC2822 |
||
117 | * @param string $string |
||
118 | * @return \DateTime |
||
119 | */ |
||
120 | 10 | public function convertToDateTime($string) |
|
121 | { |
||
122 | 10 | $string = trim($string); |
|
123 | 10 | foreach ([$this->getLastGuessedFormat(), $this->guessDateFormat($string) ] as $format) { |
|
124 | 10 | $date = \DateTime::createFromFormat($format, $string); |
|
125 | 10 | if ($date instanceof \DateTime) { |
|
126 | 9 | $date->setTimezone($this->getTimezone()); |
|
127 | |||
128 | 9 | return $date; |
|
129 | } |
||
130 | 7 | } |
|
131 | |||
132 | 1 | return $this->stringToDateTime($string); |
|
133 | } |
||
134 | |||
135 | /** |
||
136 | * Creates a DateTime instance for the given string if the format was not catch from the list |
||
137 | * @param string $string |
||
138 | * @return \DateTime |
||
139 | * @throws InvalidArgumentException |
||
140 | */ |
||
141 | 2 | public function stringToDateTime($string) |
|
142 | { |
||
143 | 2 | $this->logger->notice("unsupported date format, use strtotime() to build the DateTime instance : {$string}"); |
|
144 | |||
145 | 2 | if ( false === strtotime($string) ) { |
|
146 | 1 | throw new \InvalidArgumentException('Impossible to convert date : '.$string); |
|
147 | } |
||
148 | 1 | $date = new \DateTime($string); |
|
149 | 1 | $date->setTimezone($this->getTimezone()); |
|
150 | |||
151 | 1 | return $date; |
|
152 | } |
||
153 | |||
154 | /** |
||
155 | * @return \DateTimeZone |
||
156 | */ |
||
157 | 11 | public function getTimezone() |
|
161 | |||
162 | /** |
||
163 | * @param \DateTimeZone $timezone |
||
164 | */ |
||
165 | 66 | public function setTimezone(\DateTimeZone $timezone) |
|
169 | } |
||
170 |