DateHeader.php 2.7KB

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