| 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 |
* An ID MIME Header for something like Message-ID or Content-ID (one or more addresses). |
| 19 |
* |
| 20 |
* @author Chris Corbyn |
| 21 |
*/ |
| 22 |
final class IdentificationHeader extends AbstractHeader |
| 23 |
{ |
| 24 |
private $ids = []; |
| 25 |
private $idsAsAddresses = []; |
| 26 |
|
| 27 |
/** |
| 28 |
* @param string|array $ids |
| 29 |
*/ |
| 30 |
public function __construct(string $name, $ids) |
| 31 |
{ |
| 32 |
parent::__construct($name); |
| 33 |
|
| 34 |
$this->setId($ids); |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* @param string|array $body a string ID or an array of IDs |
| 39 |
* |
| 40 |
* @throws RfcComplianceException |
| 41 |
*/ |
| 42 |
public function setBody($body) |
| 43 |
{ |
| 44 |
$this->setId($body); |
| 45 |
} |
| 46 |
|
| 47 |
public function getBody(): array |
| 48 |
{ |
| 49 |
return $this->getIds(); |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Set the ID used in the value of this header. |
| 54 |
* |
| 55 |
* @param string|array $id |
| 56 |
* |
| 57 |
* @throws RfcComplianceException |
| 58 |
*/ |
| 59 |
public function setId($id) |
| 60 |
{ |
| 61 |
$this->setIds(\is_array($id) ? $id : [$id]); |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Get the ID used in the value of this Header. |
| 66 |
* |
| 67 |
* If multiple IDs are set only the first is returned. |
| 68 |
*/ |
| 69 |
public function getId(): ?string |
| 70 |
{ |
| 71 |
return $this->ids[0] ?? null; |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Set a collection of IDs to use in the value of this Header. |
| 76 |
* |
| 77 |
* @param string[] $ids |
| 78 |
* |
| 79 |
* @throws RfcComplianceException |
| 80 |
*/ |
| 81 |
public function setIds(array $ids) |
| 82 |
{ |
| 83 |
$this->ids = []; |
| 84 |
$this->idsAsAddresses = []; |
| 85 |
foreach ($ids as $id) { |
| 86 |
$this->idsAsAddresses[] = new Address($id); |
| 87 |
$this->ids[] = $id; |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Get the list of IDs used in this Header. |
| 93 |
* |
| 94 |
* @return string[] |
| 95 |
*/ |
| 96 |
public function getIds(): array |
| 97 |
{ |
| 98 |
return $this->ids; |
| 99 |
} |
| 100 |
|
| 101 |
public function getBodyAsString(): string |
| 102 |
{ |
| 103 |
$addrs = []; |
| 104 |
foreach ($this->idsAsAddresses as $address) { |
| 105 |
$addrs[] = '<'.$address->toString().'>'; |
| 106 |
} |
| 107 |
|
| 108 |
return implode(' ', $addrs); |
| 109 |
} |
| 110 |
} |
| 111 |
|