This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/src/Symfony/Component/Mailer/Envelope.php

81 lines
2.1 KiB
PHP
Raw Normal View History

2019-02-06 11:34:55 +00:00
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Mailer;
use Symfony\Component\Mailer\Exception\InvalidArgumentException;
use Symfony\Component\Mailer\Exception\LogicException;
2019-02-06 11:34:55 +00:00
use Symfony\Component\Mime\Address;
use Symfony\Component\Mime\RawMessage;
/**
* @author Fabien Potencier <fabien@symfony.com>
*/
class Envelope
2019-02-06 11:34:55 +00:00
{
private $sender;
private $recipients = [];
/**
* @param Address[] $recipients
*/
public function __construct(Address $sender, array $recipients)
{
$this->setSender($sender);
$this->setRecipients($recipients);
}
public static function create(RawMessage $message): self
{
if (RawMessage::class === \get_class($message)) {
throw new LogicException('Cannot send a RawMessage instance without an explicit Envelope.');
}
return new DelayedEnvelope($message);
2019-02-06 11:34:55 +00:00
}
public function setSender(Address $sender): void
{
2019-06-12 05:53:11 +01:00
$this->sender = new Address($sender->getAddress());
2019-02-06 11:34:55 +00:00
}
public function getSender(): Address
{
return $this->sender;
}
2019-06-12 05:53:11 +01:00
/**
* @param Address[] $recipients
*/
2019-02-06 11:34:55 +00:00
public function setRecipients(array $recipients): void
{
if (!$recipients) {
throw new InvalidArgumentException('An envelope must have at least one recipient.');
}
$this->recipients = [];
foreach ($recipients as $recipient) {
2019-06-12 05:53:11 +01:00
if (!$recipient instanceof Address) {
2019-02-06 11:34:55 +00:00
throw new InvalidArgumentException(sprintf('A recipient must be an instance of "%s" (got "%s").', Address::class, \is_object($recipient) ? \get_class($recipient) : \gettype($recipient)));
}
2019-06-12 05:53:11 +01:00
$this->recipients[] = new Address($recipient->getAddress());
2019-02-06 11:34:55 +00:00
}
}
/**
* @return Address[]
*/
public function getRecipients(): array
{
return $this->recipients;
}
}