DateHeader.php 2.8KB

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