DateHeader.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. }