| 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; |
| 13 |
|
| 14 |
use Symfony\Component\Mime\Exception\LogicException; |
| 15 |
|
| 16 |
/** |
| 17 |
* @author Fabien Potencier <fabien@symfony.com> |
| 18 |
*/ |
| 19 |
class RawMessage implements \Serializable |
| 20 |
{ |
| 21 |
/** |
| 22 |
* @var iterable|string |
| 23 |
*/ |
| 24 |
private $message; |
| 25 |
|
| 26 |
/** |
| 27 |
* @param iterable|string $message |
| 28 |
*/ |
| 29 |
public function __construct($message) |
| 30 |
{ |
| 31 |
$this->message = $message; |
| 32 |
} |
| 33 |
|
| 34 |
public function toString(): string |
| 35 |
{ |
| 36 |
if (\is_string($this->message)) { |
| 37 |
return $this->message; |
| 38 |
} |
| 39 |
if ($this->message instanceof \Traversable) { |
| 40 |
$this->message = iterator_to_array($this->message, false); |
| 41 |
} |
| 42 |
|
| 43 |
return $this->message = implode('', $this->message); |
| 44 |
} |
| 45 |
|
| 46 |
public function toIterable(): iterable |
| 47 |
{ |
| 48 |
if (\is_string($this->message)) { |
| 49 |
yield $this->message; |
| 50 |
|
| 51 |
return; |
| 52 |
} |
| 53 |
|
| 54 |
$message = ''; |
| 55 |
foreach ($this->message as $chunk) { |
| 56 |
$message .= $chunk; |
| 57 |
yield $chunk; |
| 58 |
} |
| 59 |
$this->message = $message; |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* @throws LogicException if the message is not valid |
| 64 |
*/ |
| 65 |
public function ensureValidity() |
| 66 |
{ |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* @internal |
| 71 |
*/ |
| 72 |
final public function serialize(): string |
| 73 |
{ |
| 74 |
return serialize($this->__serialize()); |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* @internal |
| 79 |
*/ |
| 80 |
final public function unserialize($serialized) |
| 81 |
{ |
| 82 |
$this->__unserialize(unserialize($serialized)); |
| 83 |
} |
| 84 |
|
| 85 |
public function __serialize(): array |
| 86 |
{ |
| 87 |
return [$this->toString()]; |
| 88 |
} |
| 89 |
|
| 90 |
public function __unserialize(array $data): void |
| 91 |
{ |
| 92 |
[$this->message] = $data; |
| 93 |
} |
| 94 |
} |
| 95 |
|