DateHeader.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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 \DateTimeImmutable $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(mixed $body): void
  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): void
  45. {
  46. $this->dateTime = \DateTimeImmutable::createFromInterface($dateTime);
  47. }
  48. public function getBodyAsString(): string
  49. {
  50. return $this->dateTime->format(\DateTimeInterface::RFC2822);
  51. }
  52. }