DateHeader.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. /*
  3. * This file is part of SwiftMailer.
  4. * (c) 2004-2009 Chris Corbyn
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. /**
  10. * A Date MIME Header for Swift Mailer.
  11. *
  12. * @author Chris Corbyn
  13. */
  14. class Swift_Mime_Headers_DateHeader extends Swift_Mime_Headers_AbstractHeader
  15. {
  16. /**
  17. * Date-time value of this Header.
  18. *
  19. * @var DateTimeImmutable
  20. */
  21. private $dateTime;
  22. /**
  23. * Creates a new DateHeader with $name.
  24. *
  25. * @param string $name of Header
  26. */
  27. public function __construct($name)
  28. {
  29. $this->setFieldName($name);
  30. }
  31. /**
  32. * Get the type of Header that this instance represents.
  33. *
  34. * @see TYPE_TEXT, TYPE_PARAMETERIZED, TYPE_MAILBOX
  35. * @see TYPE_DATE, TYPE_ID, TYPE_PATH
  36. *
  37. * @return int
  38. */
  39. public function getFieldType()
  40. {
  41. return self::TYPE_DATE;
  42. }
  43. /**
  44. * Set the model for the field body.
  45. *
  46. * @param DateTimeInterface $model
  47. */
  48. public function setFieldBodyModel($model)
  49. {
  50. $this->setDateTime($model);
  51. }
  52. /**
  53. * Get the model for the field body.
  54. *
  55. * @return DateTimeImmutable
  56. */
  57. public function getFieldBodyModel()
  58. {
  59. return $this->getDateTime();
  60. }
  61. /**
  62. * Get the date-time representing the Date in this Header.
  63. *
  64. * @return DateTimeImmutable
  65. */
  66. public function getDateTime()
  67. {
  68. return $this->dateTime;
  69. }
  70. /**
  71. * Set the date-time of the Date in this Header.
  72. *
  73. * If a DateTime instance is provided, it is converted to DateTimeImmutable.
  74. */
  75. public function setDateTime(DateTimeInterface $dateTime)
  76. {
  77. $this->clearCachedValueIf($this->getCachedValue() != $dateTime->format(DateTime::RFC2822));
  78. if ($dateTime instanceof DateTime) {
  79. $immutable = new DateTimeImmutable('@'.$dateTime->getTimestamp());
  80. $dateTime = $immutable->setTimezone($dateTime->getTimezone());
  81. }
  82. $this->dateTime = $dateTime;
  83. }
  84. /**
  85. * Get the string value of the body in this Header.
  86. *
  87. * This is not necessarily RFC 2822 compliant since folding white space will
  88. * not be added at this stage (see {@link toString()} for that).
  89. *
  90. * @see toString()
  91. *
  92. * @return string
  93. */
  94. public function getFieldBody()
  95. {
  96. if (!$this->getCachedValue()) {
  97. if (isset($this->dateTime)) {
  98. $this->setCachedValue($this->dateTime->format(DateTime::RFC2822));
  99. }
  100. }
  101. return $this->getCachedValue();
  102. }
  103. }