RecipientList.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <?php
  2. /**
  3. * Swift Mailer Recipient List Container
  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_Address");
  12. Swift_ClassLoader::load("Swift_Iterator_Array");
  13. /**
  14. * Swift's Recipient List container. Contains To, Cc, Bcc
  15. * @package Swift
  16. * @author Chris Corbyn <chris@w3style.co.uk>
  17. */
  18. class Swift_RecipientList extends Swift_AddressContainer
  19. {
  20. /**
  21. * The recipients in the To: header
  22. * @var array
  23. */
  24. protected $to = array();
  25. /**
  26. * The recipients in the Cc: header
  27. * @var array
  28. */
  29. protected $cc = array();
  30. /**
  31. * The recipients in the Bcc: header
  32. * @var array
  33. */
  34. protected $bcc = array();
  35. /**
  36. * Iterators to use when getting lists back out.
  37. * If any iterators are present here, their relevant "addXX()" methods will be useless.
  38. * As per the last note, any iterators need to be pre-configured before Swift::send() is called.
  39. * @var array,Swift_Iterator
  40. */
  41. protected $iterators = array("to" => null, "cc" => null, "bcc" => null);
  42. /**
  43. * Add a recipient.
  44. * @param string The address
  45. * @param string The name
  46. * @param string The field (to, cc or bcc)
  47. */
  48. public function add($address, $name="", $where="to")
  49. {
  50. if ($address instanceof Swift_Address)
  51. {
  52. $address_str = trim(strtolower($address->getAddress()));
  53. }
  54. elseif (is_array($address))
  55. {
  56. foreach ($address as $a) $this->add($a, $name, $where);
  57. return;
  58. }
  59. else
  60. {
  61. $address_str = (string) $address;
  62. $address_str = trim(strtolower($address_str));
  63. $address = new Swift_Address($address_str, $name);
  64. }
  65. if (in_array($where, array("to", "cc", "bcc")))
  66. {
  67. $container =& $this->$where;
  68. $container[$address_str] = $address;
  69. }
  70. }
  71. /**
  72. * Remove a recipient.
  73. * @param string The address
  74. * @param string The field (to, cc or bcc)
  75. */
  76. public function remove($address, $where="to")
  77. {
  78. if ($address instanceof Swift_Address)
  79. {
  80. $key = trim(strtolower($address->getAddress()));
  81. }
  82. else $key = trim(strtolower((string) $address));
  83. if (in_array($where, array("to", "cc", "bcc")))
  84. {
  85. if (array_key_exists($key, $this->$where)) unset($this->{$where}[$key]);
  86. }
  87. }
  88. /**
  89. * Get an iterator object for all the recipients in the given field.
  90. * @param string The field name (to, cc or bcc)
  91. * @return Swift_Iterator
  92. */
  93. public function getIterator($where)
  94. {
  95. if (!empty($this->iterators[$where]))
  96. {
  97. return $this->iterators[$where];
  98. }
  99. elseif (in_array($where, array("to", "cc", "bcc")))
  100. {
  101. $it = new Swift_Iterator_Array($this->$where);
  102. return $it;
  103. }
  104. }
  105. /**
  106. * Override the loading of the default iterator (Swift_ArrayIterator) and use the one given here.
  107. * @param Swift_Iterator The iterator to use. It must be populated already.
  108. */
  109. public function setIterator(Swift_Iterator $it, $where)
  110. {
  111. if (in_array($where, array("to", "cc", "bcc")))
  112. {
  113. $this->iterators[$where] = $it;
  114. }
  115. }
  116. /**
  117. * Add a To: recipient
  118. * @param mixed The address to add. Can be a string or Swift_Address
  119. * @param string The personal name, optional
  120. */
  121. public function addTo($address, $name=null)
  122. {
  123. $this->add($address, $name, "to");
  124. }
  125. /**
  126. * Get an array of addresses in the To: field
  127. * The array contains Swift_Address objects
  128. * @return array
  129. */
  130. public function getTo()
  131. {
  132. return $this->to;
  133. }
  134. /**
  135. * Remove a To: recipient from the list
  136. * @param mixed The address to remove. Can be Swift_Address or a string
  137. */
  138. public function removeTo($address)
  139. {
  140. $this->remove($address, "to");
  141. }
  142. /**
  143. * Empty all To: addresses
  144. */
  145. public function flushTo()
  146. {
  147. $this->to = null;
  148. $this->to = array();
  149. }
  150. /**
  151. * Add a Cc: recipient
  152. * @param mixed The address to add. Can be a string or Swift_Address
  153. * @param string The personal name, optional
  154. */
  155. public function addCc($address, $name=null)
  156. {
  157. $this->add($address, $name, "cc");
  158. }
  159. /**
  160. * Get an array of addresses in the Cc: field
  161. * The array contains Swift_Address objects
  162. * @return array
  163. */
  164. public function getCc()
  165. {
  166. return $this->cc;
  167. }
  168. /**
  169. * Remove a Cc: recipient from the list
  170. * @param mixed The address to remove. Can be Swift_Address or a string
  171. */
  172. public function removeCc($address)
  173. {
  174. $this->remove($address, "cc");
  175. }
  176. /**
  177. * Empty all Cc: addresses
  178. */
  179. public function flushCc()
  180. {
  181. $this->cc = null;
  182. $this->cc = array();
  183. }
  184. /**
  185. * Add a Bcc: recipient
  186. * @param mixed The address to add. Can be a string or Swift_Address
  187. * @param string The personal name, optional
  188. */
  189. public function addBcc($address, $name=null)
  190. {
  191. $this->add($address, $name, "bcc");
  192. }
  193. /**
  194. * Get an array of addresses in the Bcc: field
  195. * The array contains Swift_Address objects
  196. * @return array
  197. */
  198. public function getBcc()
  199. {
  200. return $this->bcc;
  201. }
  202. /**
  203. * Remove a Bcc: recipient from the list
  204. * @param mixed The address to remove. Can be Swift_Address or a string
  205. */
  206. public function removeBcc($address)
  207. {
  208. $this->remove($address, "bcc");
  209. }
  210. /**
  211. * Empty all Bcc: addresses
  212. */
  213. public function flushBcc()
  214. {
  215. $this->bcc = null;
  216. $this->bcc = array();
  217. }
  218. /**
  219. * Empty the entire list
  220. */
  221. public function flush()
  222. {
  223. $this->flushTo();
  224. $this->flushCc();
  225. $this->flushBcc();
  226. }
  227. }