PathHeader.php 3.1KB

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