|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the ZBateson\MailMimeParser project. |
|
4
|
|
|
* |
|
5
|
|
|
* @license http://opensource.org/licenses/bsd-license.php BSD |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace ZBateson\MailMimeParser\Header; |
|
9
|
|
|
|
|
10
|
|
|
use DateTime; |
|
11
|
|
|
use DateTimeImmutable; |
|
12
|
|
|
use Psr\Log\LoggerInterface; |
|
13
|
|
|
use ZBateson\MailMimeParser\Header\Consumer\DateConsumerService; |
|
14
|
|
|
use ZBateson\MailMimeParser\Header\Part\DatePart; |
|
15
|
|
|
use ZBateson\MailMimeParser\MailMimeParser; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Reads a DatePart value header in either RFC 2822 or RFC 822 format. |
|
19
|
|
|
* |
|
20
|
|
|
* @author Zaahid Bateson |
|
21
|
|
|
*/ |
|
22
|
|
|
class DateHeader extends AbstractHeader |
|
23
|
|
|
{ |
|
24
|
24 |
|
public function __construct( |
|
25
|
|
|
string $name, |
|
26
|
|
|
string $value, |
|
27
|
|
|
?LoggerInterface $logger = null, |
|
28
|
|
|
?DateConsumerService $consumerService = null |
|
29
|
|
|
) { |
|
30
|
24 |
|
$di = MailMimeParser::getGlobalContainer(); |
|
31
|
24 |
|
parent::__construct( |
|
32
|
24 |
|
$logger ?? $di->get(LoggerInterface::class), |
|
33
|
24 |
|
$consumerService ?? $di->get(DateConsumerService::class), |
|
34
|
24 |
|
$name, |
|
35
|
24 |
|
$value |
|
36
|
24 |
|
); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Convenience method returning the part's DateTime object, or null if the |
|
41
|
|
|
* date could not be parsed. |
|
42
|
|
|
* |
|
43
|
|
|
* @return ?DateTime The parsed DateTime object. |
|
44
|
|
|
*/ |
|
45
|
6 |
|
public function getDateTime() : ?DateTime |
|
46
|
|
|
{ |
|
47
|
6 |
|
if (!empty($this->parts) && $this->parts[0] instanceof DatePart) { |
|
48
|
5 |
|
return $this->parts[0]->getDateTime(); |
|
49
|
|
|
} |
|
50
|
1 |
|
return null; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Returns a DateTimeImmutable for the part's DateTime object, or null if |
|
55
|
|
|
* the date could not be parsed. |
|
56
|
|
|
* |
|
57
|
|
|
* @return ?DateTimeImmutable The parsed DateTimeImmutable object. |
|
58
|
|
|
*/ |
|
59
|
|
|
public function getDateTimeImmutable() : ?DateTimeImmutable |
|
60
|
|
|
{ |
|
61
|
|
|
$dateTime = $this->getDateTime(); |
|
62
|
|
|
if ($dateTime !== null) { |
|
63
|
|
|
return DateTimeImmutable::createFromMutable($dateTime); |
|
64
|
|
|
} |
|
65
|
|
|
return null; |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|