Dashboard sipadu mbip
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.

DateHeader.php 1.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. * @experimental in 4.3
  17. */
  18. final class DateHeader extends AbstractHeader
  19. {
  20. private $dateTime;
  21. public function __construct(string $name, \DateTimeInterface $date)
  22. {
  23. parent::__construct($name);
  24. $this->setDateTime($date);
  25. }
  26. /**
  27. * @param \DateTimeInterface $body
  28. */
  29. public function setBody($body)
  30. {
  31. $this->setDateTime($body);
  32. }
  33. /**
  34. * @return \DateTimeImmutable
  35. */
  36. public function getBody()
  37. {
  38. return $this->getDateTime();
  39. }
  40. public function getDateTime(): \DateTimeImmutable
  41. {
  42. return $this->dateTime;
  43. }
  44. /**
  45. * Set the date-time of the Date in this Header.
  46. *
  47. * If a DateTime instance is provided, it is converted to DateTimeImmutable.
  48. */
  49. public function setDateTime(\DateTimeInterface $dateTime)
  50. {
  51. if ($dateTime instanceof \DateTime) {
  52. $immutable = new \DateTimeImmutable('@'.$dateTime->getTimestamp());
  53. $dateTime = $immutable->setTimezone($dateTime->getTimezone());
  54. }
  55. $this->dateTime = $dateTime;
  56. }
  57. public function getBodyAsString(): string
  58. {
  59. return $this->dateTime->format(\DateTime::RFC2822);
  60. }
  61. }