Address.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. /**
  3. * Swift Mailer Address Container (purely for rigid RFC conformance)
  4. * Please read the LICENSE file
  5. * @copyright Chris Corbyn <chris@w3style.co.uk>
  6. * @author Chris Corbyn <chris@w3style.co.uk>
  7. * @package Swift
  8. * @license GNU Lesser General Public License
  9. */
  10. require_once dirname(__FILE__) . "/ClassLoader.php";
  11. Swift_ClassLoader::load("Swift_AddressContainer");
  12. /**
  13. * Swift_Address is just a lone e-mail address reprsented as an object
  14. * @package Swift
  15. * @author Chris Corbyn <chris@w3style.co.uk>
  16. */
  17. class Swift_Address extends Swift_AddressContainer
  18. {
  19. /**
  20. * The e-mail address portion
  21. * @var string
  22. */
  23. protected $address = null;
  24. /**
  25. * The personal name part
  26. * @var string
  27. */
  28. protected $name = null;
  29. /**
  30. * Constructor
  31. * @param string The address portion
  32. * @param string The personal name, optional
  33. */
  34. public function __construct($address, $name=null)
  35. {
  36. $this->setAddress($address);
  37. if ($name !== null) $this->setName($name);
  38. }
  39. /**
  40. * Set the email address
  41. * @param string
  42. */
  43. public function setAddress($address)
  44. {
  45. $this->address = trim((string)$address);
  46. }
  47. /**
  48. * Get the address portion
  49. * @return string
  50. */
  51. public function getAddress()
  52. {
  53. return $this->address;
  54. }
  55. /**
  56. * Set the personal name
  57. * @param string
  58. */
  59. public function setName($name)
  60. {
  61. if ($name !== null) $this->name = (string) $name;
  62. else $this->name = null;
  63. }
  64. /**
  65. * Get personal name portion
  66. * @return string
  67. */
  68. public function getName()
  69. {
  70. return $this->name;
  71. }
  72. /**
  73. * Build the address the way it should be structured
  74. * @param boolean If the string will be sent to a SMTP server as an envelope
  75. * @return string
  76. */
  77. public function build($smtp=false)
  78. {
  79. if ($smtp)
  80. {
  81. return "<" . $this->address . ">";
  82. }
  83. else
  84. {
  85. if (($this->name !== null))
  86. {
  87. return $this->name . " <" . $this->address . ">";
  88. }
  89. else return $this->address;
  90. }
  91. }
  92. /**
  93. * PHP's casting conversion
  94. * @return string
  95. */
  96. public function __toString()
  97. {
  98. return $this->build(true);
  99. }
  100. }