PathHeader.php 3.7 KB

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