PathHeader.php 3.4 KB

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