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 | 41 | public function __construct(LoggerInterface $logger = null) |
|
64 | |||
65 | /** |
||
66 | * @param $dateFormat |
||
67 | * @return $this |
||
68 | */ |
||
69 | 10 | public function addDateFormat($dateFormat) |
|
75 | |||
76 | /** |
||
77 | * @param array $dateFormats |
||
78 | * @return $this |
||
79 | */ |
||
80 | public function setDateFormats(array $dateFormats) |
||
86 | |||
87 | /** |
||
88 | * @return string |
||
89 | */ |
||
90 | 5 | public function getLastGuessedFormat() |
|
94 | |||
95 | /** |
||
96 | * |
||
97 | * @param string $date |
||
98 | * @return string|false date Format |
||
99 | * @throws InvalidArgumentException |
||
100 | */ |
||
101 | 5 | public function guessDateFormat($date) |
|
114 | |||
115 | /** |
||
116 | * Creates a DateTime instance for the given string. Default format is RFC2822 |
||
117 | * @param string $string |
||
118 | * @return \DateTime |
||
119 | */ |
||
120 | 5 | public function convertToDateTime($string) |
|
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 | public function stringToDateTime($string) |
||
153 | |||
154 | /** |
||
155 | * @return \DateTimeZone |
||
156 | */ |
||
157 | 5 | public function getTimezone() |
|
161 | |||
162 | /** |
||
163 | * @param \DateTimeZone $timezone |
||
164 | */ |
||
165 | 41 | public function setTimezone(\DateTimeZone $timezone) |
|
169 | } |
||
170 |