You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

66 lines
1.5 KiB

3 years ago
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Mime\Header;
  11. /**
  12. * A Date MIME Header.
  13. *
  14. * @author Chris Corbyn
  15. */
  16. final class DateHeader extends AbstractHeader
  17. {
  18. private $dateTime;
  19. public function __construct(string $name, \DateTimeInterface $date)
  20. {
  21. parent::__construct($name);
  22. $this->setDateTime($date);
  23. }
  24. /**
  25. * @param \DateTimeInterface $body
  26. */
  27. public function setBody($body)
  28. {
  29. $this->setDateTime($body);
  30. }
  31. public function getBody(): \DateTimeImmutable
  32. {
  33. return $this->getDateTime();
  34. }
  35. public function getDateTime(): \DateTimeImmutable
  36. {
  37. return $this->dateTime;
  38. }
  39. /**
  40. * Set the date-time of the Date in this Header.
  41. *
  42. * If a DateTime instance is provided, it is converted to DateTimeImmutable.
  43. */
  44. public function setDateTime(\DateTimeInterface $dateTime)
  45. {
  46. if ($dateTime instanceof \DateTime) {
  47. $immutable = new \DateTimeImmutable('@'.$dateTime->getTimestamp());
  48. $dateTime = $immutable->setTimezone($dateTime->getTimezone());
  49. }
  50. $this->dateTime = $dateTime;
  51. }
  52. public function getBodyAsString(): string
  53. {
  54. return $this->dateTime->format(\DateTime::RFC2822);
  55. }
  56. }