| 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 DateTimeImmutable; |
| 11 |
use ZBateson\MailMimeParser\Header\Consumer\ConsumerService; |
| 12 |
use ZBateson\MailMimeParser\Header\Part\DatePart; |
| 13 |
|
| 14 |
/** |
| 15 |
* Reads a DatePart value header in either RFC 2822 or RFC 822 format. |
| 16 |
* |
| 17 |
* @author Zaahid Bateson |
| 18 |
*/ |
| 19 |
class DateHeader extends AbstractHeader |
| 20 |
{ |
| 21 |
/** |
| 22 |
* Returns a DateConsumer. |
| 23 |
* |
| 24 |
* @return Consumer\AbstractConsumer |
| 25 |
*/ |
| 26 |
protected function getConsumer(ConsumerService $consumerService) |
| 27 |
{ |
| 28 |
return $consumerService->getDateConsumer(); |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* Convenience method returning the part's DateTime object, or null if the |
| 33 |
* date could not be parsed. |
| 34 |
* |
| 35 |
* @return \DateTime|null The parsed DateTime object. |
| 36 |
*/ |
| 37 |
public function getDateTime() : ?\DateTime |
| 38 |
{ |
| 39 |
if (!empty($this->parts) && $this->parts[0] instanceof DatePart) { |
| 40 |
return $this->parts[0]->getDateTime(); |
| 41 |
} |
| 42 |
return null; |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Returns a DateTimeImmutable for the part's DateTime object, or null if |
| 47 |
* the date could not be parsed. |
| 48 |
* |
| 49 |
* @return DateTimeImmutable|null The parsed DateTimeImmutable object. |
| 50 |
*/ |
| 51 |
public function getDateTimeImmutable() : ?\DateTimeImmutable |
| 52 |
{ |
| 53 |
$dateTime = $this->getDateTime(); |
| 54 |
if ($dateTime !== null) { |
| 55 |
return DateTimeImmutable::createFromMutable($dateTime); |
| 56 |
} |
| 57 |
return null; |
| 58 |
} |
| 59 |
} |
| 60 |
|