TYPO3  7.6
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 
17 {
23  private $_mailboxes = array();
24 
32  public function __construct($name, Swift_Mime_HeaderEncoder $encoder, Swift_Mime_Grammar $grammar)
33  {
34  $this->setFieldName($name);
35  $this->setEncoder($encoder);
36  parent::__construct($grammar);
37  }
38 
47  public function getFieldType()
48  {
49  return self::TYPE_MAILBOX;
50  }
51 
61  public function setFieldBodyModel($model)
62  {
63  $this->setNameAddresses($model);
64  }
65 
75  public function getFieldBodyModel()
76  {
77  return $this->getNameAddresses();
78  }
79 
104  public function setNameAddresses($mailboxes)
105  {
106  $this->_mailboxes = $this->normalizeMailboxes((array) $mailboxes);
107  $this->setCachedValue(null); //Clear any cached value
108  }
109 
135  public function getNameAddressStrings()
136  {
137  return $this->_createNameAddressStrings($this->getNameAddresses());
138  }
139 
164  public function getNameAddresses()
165  {
166  return $this->_mailboxes;
167  }
168 
189  public function setAddresses($addresses)
190  {
191  $this->setNameAddresses(array_values((array) $addresses));
192  }
193 
201  public function getAddresses()
202  {
203  return array_keys($this->_mailboxes);
204  }
205 
211  public function removeAddresses($addresses)
212  {
213  $this->setCachedValue(null);
214  foreach ((array) $addresses as $address) {
215  unset($this->_mailboxes[$address]);
216  }
217  }
218 
231  public function getFieldBody()
232  {
233  // Compute the string value of the header only if needed
234  if (is_null($this->getCachedValue())) {
235  $this->setCachedValue($this->createMailboxListString($this->_mailboxes));
236  }
237 
238  return $this->getCachedValue();
239  }
240 
241  // -- Points of extension
242 
250  protected function normalizeMailboxes(array $mailboxes)
251  {
252  $actualMailboxes = array();
253 
254  foreach ($mailboxes as $key => $value) {
255  if (is_string($key)) {
256  //key is email addr
257  $address = $key;
258  $name = $value;
259  } else {
260  $address = $value;
261  $name = null;
262  }
263  $this->_assertValidAddress($address);
264  $actualMailboxes[$address] = $name;
265  }
266 
267  return $actualMailboxes;
268  }
269 
278  protected function createDisplayNameString($displayName, $shorten = false)
279  {
280  return $this->createPhrase($this, $displayName,
281  $this->getCharset(), $this->getEncoder(), $shorten
282  );
283  }
284 
294  protected function createMailboxListString(array $mailboxes)
295  {
296  return implode(', ', $this->_createNameAddressStrings($mailboxes));
297  }
298 
309  protected function tokenNeedsEncoding($token)
310  {
311  return preg_match('/[,;]/', $token) || parent::tokenNeedsEncoding($token);
312  }
313 
321  private function _createNameAddressStrings(array $mailboxes)
322  {
323  $strings = array();
324 
325  foreach ($mailboxes as $email => $name) {
326  $mailboxStr = $email;
327  if (!is_null($name)) {
328  $nameStr = $this->createDisplayNameString($name, empty($strings));
329  $mailboxStr = $nameStr.' <'.$mailboxStr.'>';
330  }
331  $strings[] = $mailboxStr;
332  }
333 
334  return $strings;
335  }
336 
344  private function _assertValidAddress($address)
345  {
346  if (!preg_match('/^'.$this->getGrammar()->getDefinition('addr-spec').'$/D',
347  $address)) {
349  'Address in mailbox given ['.$address.
350  '] does not comply with RFC 2822, 3.6.2.'
351  );
352  }
353  }
354 }