DateHeader.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. * The UNIX timestamp value of this Header.
  18. *
  19. * @var int
  20. */
  21. private $_timestamp;
  22. /**
  23. * Creates a new DateHeader with $name and $timestamp.
  24. *
  25. * Example:
  26. * <code>
  27. * <?php
  28. * $header = new Swift_Mime_Headers_DateHeader('Date', time());
  29. * ?>
  30. * </code>
  31. *
  32. * @param string $name of Header
  33. * @param Swift_Mime_Grammar $grammar
  34. */
  35. public function __construct($name, Swift_Mime_Grammar $grammar)
  36. {
  37. $this->setFieldName($name);
  38. parent::__construct($grammar);
  39. }
  40. /**
  41. * Get the type of Header that this instance represents.
  42. *
  43. * @see TYPE_TEXT, TYPE_PARAMETERIZED, TYPE_MAILBOX
  44. * @see TYPE_DATE, TYPE_ID, TYPE_PATH
  45. *
  46. * @return int
  47. */
  48. public function getFieldType()
  49. {
  50. return self::TYPE_DATE;
  51. }
  52. /**
  53. * Set the model for the field body.
  54. *
  55. * This method takes a UNIX timestamp.
  56. *
  57. * @param int $model
  58. */
  59. public function setFieldBodyModel($model)
  60. {
  61. $this->setTimestamp($model);
  62. }
  63. /**
  64. * Get the model for the field body.
  65. *
  66. * This method returns a UNIX timestamp.
  67. *
  68. * @return mixed
  69. */
  70. public function getFieldBodyModel()
  71. {
  72. return $this->getTimestamp();
  73. }
  74. /**
  75. * Get the UNIX timestamp of the Date in this Header.
  76. *
  77. * @return int
  78. */
  79. public function getTimestamp()
  80. {
  81. return $this->_timestamp;
  82. }
  83. /**
  84. * Set the UNIX timestamp of the Date in this Header.
  85. *
  86. * @param int $timestamp
  87. */
  88. public function setTimestamp($timestamp)
  89. {
  90. if (!is_null($timestamp)) {
  91. $timestamp = (int) $timestamp;
  92. }
  93. $this->clearCachedValueIf($this->_timestamp != $timestamp);
  94. $this->_timestamp = $timestamp;
  95. }
  96. /**
  97. * Get the string value of the body in this Header.
  98. *
  99. * This is not necessarily RFC 2822 compliant since folding white space will
  100. * not be added at this stage (see {@link toString()} for that).
  101. *
  102. * @see toString()
  103. *
  104. * @return string
  105. */
  106. public function getFieldBody()
  107. {
  108. if (!$this->getCachedValue()) {
  109. if (isset($this->_timestamp)) {
  110. $this->setCachedValue(date('r', $this->_timestamp));
  111. }
  112. }
  113. return $this->getCachedValue();
  114. }
  115. }