24 private static $uniqueHeaders = [
25 'date',
'from',
'sender',
'reply-to',
'to',
'cc',
'bcc',
26 'message-id',
'in-reply-to',
'references',
'subject',
29 private $headers = [];
30 private $lineLength = 76;
34 foreach ($headers as $header) {
41 foreach ($this->headers as $name => $collection) {
42 foreach ($collection as $i => $header) {
43 $this->headers[$name][$i] = clone $header;
50 $this->lineLength = $lineLength;
51 foreach ($this->
all() as $header) {
52 $header->setMaxLineLength($lineLength);
58 return $this->lineLength;
104 public function addDateHeader(
string $name, \DateTimeInterface $dateTime): self
125 public function has(
string $name): bool
127 return isset($this->headers[strtolower($name)]);
136 'date' => DateHeader::class,
137 'from' => MailboxListHeader::class,
138 'sender' => MailboxHeader::class,
139 'reply-to' => MailboxListHeader::class,
140 'to' => MailboxListHeader::class,
141 'cc' => MailboxListHeader::class,
142 'bcc' => MailboxListHeader::class,
143 'message-id' => IdentificationHeader::class,
144 'in-reply-to' => IdentificationHeader::class,
145 'references' => IdentificationHeader::class,
146 'return-path' => PathHeader::class,
150 $name = strtolower($header->
getName());
152 if (isset($map[$name]) && !$header instanceof $map[$name]) {
153 throw new LogicException(sprintf(
'The "%s" header must be an instance of "%s" (got "%s").', $header->
getName(), $map[$name], \get_class($header)));
156 if (\in_array($name, self::$uniqueHeaders,
true) && isset($this->headers[$name]) && \count($this->headers[$name]) > 0) {
157 throw new LogicException(sprintf(
'Impossible to set header "%s" as it\'s already defined and must be unique.', $header->
getName()));
160 $this->headers[$name][] = $header;
167 $name = strtolower($name);
168 if (!isset($this->headers[$name])) {
172 $values = array_values($this->headers[$name]);
174 return array_shift($values);
177 public function all(
string $name =
null): iterable
179 if (
null === $name) {
180 foreach ($this->headers as $name => $collection) {
181 foreach ($collection as $header) {
182 yield $name => $header;
185 } elseif (isset($this->headers[strtolower($name)])) {
186 foreach ($this->headers[strtolower($name)] as $header) {
194 return array_keys($this->headers);
197 public function remove(
string $name):
void
199 unset($this->headers[strtolower($name)]);
204 return \in_array($name, self::$uniqueHeaders,
true);
210 foreach ($this->
toArray() as $str) {
211 $string .= $str.
"\r\n";
220 foreach ($this->
all() as $header) {
221 if (
'' !== $header->getBodyAsString()) {
222 $arr[] = $header->toString();
234 return $this->
has($name) ? $this->
get($name)->getBody() :
null;
242 if ($this->
has($name)) {
243 $this->
get($name)->setBody($body);
245 $this->{
'add'.$type.
'Header'}($name, $body);
254 if (!$this->
has($name)) {
258 $header = $this->
get($name);
260 throw new LogicException(sprintf(
'Unable to get parameter "%s" on header "%s" as the header is not of class "%s".', $parameter, $name, ParameterizedHeader::class));
263 return $header->getParameter($parameter);
271 if (!$this->
has($name)) {
272 throw new LogicException(sprintf(
'Unable to set parameter "%s" on header "%s" as the header is not defined.', $parameter, $name));
275 $header = $this->
get($name);
277 throw new LogicException(sprintf(
'Unable to set parameter "%s" on header "%s" as the header is not of class "%s".', $parameter, $name, ParameterizedHeader::class));
280 $header->setParameter($parameter, $value);