PathHeader.php 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 Path Header in Swift Mailer, such a Return-Path.
  11. *
  12. * @package Swift
  13. * @subpackage Mime
  14. * @author Chris Corbyn
  15. */
  16. class Swift_Mime_Headers_PathHeader extends Swift_Mime_Headers_AbstractHeader
  17. {
  18. /**
  19. * The address in this Header (if specified).
  20. *
  21. * @var string
  22. */
  23. private $_address;
  24. /**
  25. * Creates a new PathHeader with the given $name.
  26. *
  27. * @param string $name
  28. * @param Swift_Mime_Grammar $grammar
  29. */
  30. public function __construct($name, Swift_Mime_Grammar $grammar)
  31. {
  32. $this->setFieldName($name);
  33. parent::__construct($grammar);
  34. }
  35. /**
  36. * Get the type of Header that this instance represents.
  37. *
  38. * @see TYPE_TEXT, TYPE_PARAMETERIZED, TYPE_MAILBOX
  39. * @see TYPE_DATE, TYPE_ID, TYPE_PATH
  40. *
  41. * @return int
  42. */
  43. public function getFieldType()
  44. {
  45. return self::TYPE_PATH;
  46. }
  47. /**
  48. * Set the model for the field body.
  49. * This method takes a string for an address.
  50. *
  51. * @param string $model
  52. *
  53. * @throws Swift_RfcComplianceException
  54. */
  55. public function setFieldBodyModel($model)
  56. {
  57. $this->setAddress($model);
  58. }
  59. /**
  60. * Get the model for the field body.
  61. * This method returns a string email address.
  62. *
  63. * @return mixed
  64. */
  65. public function getFieldBodyModel()
  66. {
  67. return $this->getAddress();
  68. }
  69. /**
  70. * Set the Address which should appear in this Header.
  71. *
  72. * @param string $address
  73. *
  74. * @throws Swift_RfcComplianceException
  75. */
  76. public function setAddress($address)
  77. {
  78. if (is_null($address)) {
  79. $this->_address = null;
  80. } elseif ('' == $address) {
  81. $this->_address = '';
  82. } else {
  83. $this->_assertValidAddress($address);
  84. $this->_address = $address;
  85. }
  86. $this->setCachedValue(null);
  87. }
  88. /**
  89. * Get the address which is used in this Header (if any).
  90. *
  91. * Null is returned if no address is set.
  92. *
  93. * @return string
  94. */
  95. public function getAddress()
  96. {
  97. return $this->_address;
  98. }
  99. /**
  100. * Get the string value of the body in this Header.
  101. *
  102. * This is not necessarily RFC 2822 compliant since folding white space will
  103. * not be added at this stage (see {@link toString()} for that).
  104. *
  105. * @see toString()
  106. *
  107. * @return string
  108. */
  109. public function getFieldBody()
  110. {
  111. if (!$this->getCachedValue()) {
  112. if (isset($this->_address)) {
  113. $this->setCachedValue('<' . $this->_address . '>');
  114. }
  115. }
  116. return $this->getCachedValue();
  117. }
  118. /**
  119. * Throws an Exception if the address passed does not comply with RFC 2822.
  120. *
  121. * @param string $address
  122. *
  123. * @throws Swift_RfcComplianceException If address is invalid
  124. */
  125. private function _assertValidAddress($address)
  126. {
  127. if (!preg_match('/^' . $this->getGrammar()->getDefinition('addr-spec') . '$/D',
  128. $address))
  129. {
  130. throw new Swift_RfcComplianceException(
  131. 'Address set in PathHeader does not comply with addr-spec of RFC 2822.'
  132. );
  133. }
  134. }
  135. }