PathHeader.php 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. $this->_address = null;
  71. } elseif ('' == $address) {
  72. $this->_address = '';
  73. } else {
  74. $this->_assertValidAddress($address);
  75. $this->_address = $address;
  76. }
  77. $this->setCachedValue(null);
  78. }
  79. /**
  80. * Get the address which is used in this Header (if any).
  81. * Null is returned if no address is set.
  82. * @return string
  83. */
  84. public function getAddress()
  85. {
  86. return $this->_address;
  87. }
  88. /**
  89. * Get the string value of the body in this Header.
  90. * This is not necessarily RFC 2822 compliant since folding white space will
  91. * not be added at this stage (see {@link toString()} for that).
  92. * @return string
  93. * @see toString()
  94. */
  95. public function getFieldBody()
  96. {
  97. if (!$this->getCachedValue()) {
  98. if (isset($this->_address)) {
  99. $this->setCachedValue('<' . $this->_address . '>');
  100. }
  101. }
  102. return $this->getCachedValue();
  103. }
  104. /**
  105. * Throws an Exception if the address passed does not comply with RFC 2822.
  106. * @param string $address
  107. * @throws Swift_RfcComplianceException If invalid.
  108. * @access private
  109. */
  110. private function _assertValidAddress($address)
  111. {
  112. if (!preg_match('/^' . $this->getGrammar()->getDefinition('addr-spec') . '$/D',
  113. $address))
  114. {
  115. throw new Swift_RfcComplianceException(
  116. 'Address set in PathHeader does not comply with addr-spec of RFC 2822.'
  117. );
  118. }
  119. }
  120. }