XOOPS RMCommon Utilities  2.1.8.91RC
 All Classes Namespaces Files Functions Variables
MailboxHeader.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of SwiftMailer.
5  * (c) 2004-2009 Chris Corbyn
6  *
7  * For the full copyright and license information, please view the LICENSE
8  * file that was distributed with this source code.
9  */
10 
11 //@require 'Swift/Mime/Headers/AbstractHeader.php';
12 //@require 'Swift/Mime/HeaderEncoder.php';
13 
21 {
22 
28  private $_mailboxes = array();
29 
35  public function __construct($name, Swift_Mime_HeaderEncoder $encoder)
36  {
37  $this->setFieldName($name);
38  $this->setEncoder($encoder);
39  $this->initializeGrammar();
40  }
41 
48  public function getFieldType()
49  {
50  return self::TYPE_MAILBOX;
51  }
52 
59  public function setFieldBodyModel($model)
60  {
61  $this->setNameAddresses($model);
62  }
63 
70  public function getFieldBodyModel()
71  {
72  return $this->getNameAddresses();
73  }
74 
95  public function setNameAddresses($mailboxes)
96  {
97  $this->_mailboxes = $this->normalizeMailboxes((array) $mailboxes);
98  $this->setCachedValue(null); //Clear any cached value
99  }
100 
122  public function getNameAddressStrings()
123  {
124  return $this->_createNameAddressStrings($this->getNameAddresses());
125  }
126 
148  public function getNameAddresses()
149  {
150  return $this->_mailboxes;
151  }
152 
169  public function setAddresses($addresses)
170  {
171  return $this->setNameAddresses(array_values((array) $addresses));
172  }
173 
179  public function getAddresses()
180  {
181  return array_keys($this->_mailboxes);
182  }
183 
188  public function removeAddresses($addresses)
189  {
190  $this->setCachedValue(null);
191  foreach ((array) $addresses as $address)
192  {
193  unset($this->_mailboxes[$address]);
194  }
195  }
196 
205  public function getFieldBody()
206  {
207  //Compute the string value of the header only if needed
208  if (is_null($this->getCachedValue()))
209  {
210  $this->setCachedValue($this->createMailboxListString($this->_mailboxes));
211  }
212  return $this->getCachedValue();
213  }
214 
215  // -- Points of extension
216 
223  protected function normalizeMailboxes(array $mailboxes)
224  {
225  $actualMailboxes = array();
226 
227  foreach ($mailboxes as $key => $value)
228  {
229  if (is_string($key)) //key is email addr
230  {
231  $address = $key;
232  $name = $value;
233  }
234  else
235  {
236  $address = $value;
237  $name = null;
238  }
239  $this->_assertValidAddress($address);
240  $actualMailboxes[$address] = $name;
241  }
242 
243  return $actualMailboxes;
244  }
245 
253  protected function createDisplayNameString($displayName, $shorten = false)
254  {
255  return $this->createPhrase($this, $displayName,
256  $this->getCharset(), $this->getEncoder(), $shorten
257  );
258  }
259 
267  protected function createMailboxListString(array $mailboxes)
268  {
269  return implode(', ', $this->_createNameAddressStrings($mailboxes));
270  }
271 
272  // -- Private methods
273 
280  private function _createNameAddressStrings(array $mailboxes)
281  {
282  $strings = array();
283 
284  foreach ($mailboxes as $email => $name)
285  {
286  $mailboxStr = $email;
287  if (!is_null($name))
288  {
289  $nameStr = $this->createDisplayNameString($name, empty($strings));
290  $mailboxStr = $nameStr . ' <' . $mailboxStr . '>';
291  }
292  $strings[] = $mailboxStr;
293  }
294 
295  return $strings;
296  }
297 
304  private function _assertValidAddress($address)
305  {
306  if (!preg_match('/^' . $this->getGrammar('addr-spec') . '$/D',
307  $address))
308  {
310  'Address in mailbox given [' . $address .
311  '] does not comply with RFC 2822, 3.6.2.'
312  );
313  }
314  }
315 
316 }