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