| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* This file is part of the Symfony package. |
| 5 |
* |
| 6 |
* (c) Fabien Potencier <fabien@symfony.com> |
| 7 |
* |
| 8 |
* For the full copyright and license information, please view the LICENSE |
| 9 |
* file that was distributed with this source code. |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace Symfony\Component\Mime\Header; |
| 13 |
|
| 14 |
use Symfony\Component\Mime\Address; |
| 15 |
use Symfony\Component\Mime\Exception\RfcComplianceException; |
| 16 |
|
| 17 |
/** |
| 18 |
* A Mailbox list MIME Header for something like From, To, Cc, and Bcc (one or more named addresses). |
| 19 |
* |
| 20 |
* @author Chris Corbyn |
| 21 |
*/ |
| 22 |
final class MailboxListHeader extends AbstractHeader |
| 23 |
{ |
| 24 |
private $addresses = []; |
| 25 |
|
| 26 |
/** |
| 27 |
* @param Address[] $addresses |
| 28 |
*/ |
| 29 |
public function __construct(string $name, array $addresses) |
| 30 |
{ |
| 31 |
parent::__construct($name); |
| 32 |
|
| 33 |
$this->setAddresses($addresses); |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* @param Address[] $body |
| 38 |
* |
| 39 |
* @throws RfcComplianceException |
| 40 |
*/ |
| 41 |
public function setBody($body) |
| 42 |
{ |
| 43 |
$this->setAddresses($body); |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* @return Address[] |
| 48 |
* |
| 49 |
* @throws RfcComplianceException |
| 50 |
*/ |
| 51 |
public function getBody(): array |
| 52 |
{ |
| 53 |
return $this->getAddresses(); |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Sets a list of addresses to be shown in this Header. |
| 58 |
* |
| 59 |
* @param Address[] $addresses |
| 60 |
* |
| 61 |
* @throws RfcComplianceException |
| 62 |
*/ |
| 63 |
public function setAddresses(array $addresses) |
| 64 |
{ |
| 65 |
$this->addresses = []; |
| 66 |
$this->addAddresses($addresses); |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Sets a list of addresses to be shown in this Header. |
| 71 |
* |
| 72 |
* @param Address[] $addresses |
| 73 |
* |
| 74 |
* @throws RfcComplianceException |
| 75 |
*/ |
| 76 |
public function addAddresses(array $addresses) |
| 77 |
{ |
| 78 |
foreach ($addresses as $address) { |
| 79 |
$this->addAddress($address); |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* @throws RfcComplianceException |
| 85 |
*/ |
| 86 |
public function addAddress(Address $address) |
| 87 |
{ |
| 88 |
$this->addresses[] = $address; |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* @return Address[] |
| 93 |
*/ |
| 94 |
public function getAddresses(): array |
| 95 |
{ |
| 96 |
return $this->addresses; |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Gets the full mailbox list of this Header as an array of valid RFC 2822 strings. |
| 101 |
* |
| 102 |
* @return string[] |
| 103 |
* |
| 104 |
* @throws RfcComplianceException |
| 105 |
*/ |
| 106 |
public function getAddressStrings(): array |
| 107 |
{ |
| 108 |
$strings = []; |
| 109 |
foreach ($this->addresses as $address) { |
| 110 |
$str = $address->getEncodedAddress(); |
| 111 |
if ($name = $address->getName()) { |
| 112 |
$str = $this->createPhrase($this, $name, $this->getCharset(), !$strings).' <'.$str.'>'; |
| 113 |
} |
| 114 |
$strings[] = $str; |
| 115 |
} |
| 116 |
|
| 117 |
return $strings; |
| 118 |
} |
| 119 |
|
| 120 |
public function getBodyAsString(): string |
| 121 |
{ |
| 122 |
return implode(', ', $this->getAddressStrings()); |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Redefine the encoding requirements for addresses. |
| 127 |
* |
| 128 |
* All "specials" must be encoded as the full header value will not be quoted |
| 129 |
* |
| 130 |
* @see RFC 2822 3.2.1 |
| 131 |
*/ |
| 132 |
protected function tokenNeedsEncoding(string $token): bool |
| 133 |
{ |
| 134 |
return preg_match('/[()<>\[\]:;@\,."]/', $token) || parent::tokenNeedsEncoding($token); |
| 135 |
} |
| 136 |
} |
| 137 |
|