RawMessage.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Mime;
  11. use Symfony\Component\Mime\Exception\LogicException;
  12. /**
  13. * @author Fabien Potencier <fabien@symfony.com>
  14. */
  15. class RawMessage implements \Serializable
  16. {
  17. private $message;
  18. /**
  19. * @param iterable|string $message
  20. */
  21. public function __construct($message)
  22. {
  23. $this->message = $message;
  24. }
  25. public function toString(): string
  26. {
  27. if (\is_string($this->message)) {
  28. return $this->message;
  29. }
  30. if ($this->message instanceof \Traversable) {
  31. $this->message = iterator_to_array($this->message, false);
  32. }
  33. return $this->message = implode('', $this->message);
  34. }
  35. public function toIterable(): iterable
  36. {
  37. if (\is_string($this->message)) {
  38. yield $this->message;
  39. return;
  40. }
  41. $message = '';
  42. foreach ($this->message as $chunk) {
  43. $message .= $chunk;
  44. yield $chunk;
  45. }
  46. $this->message = $message;
  47. }
  48. /**
  49. * @throws LogicException if the message is not valid
  50. */
  51. public function ensureValidity()
  52. {
  53. }
  54. /**
  55. * @internal
  56. */
  57. final public function serialize(): string
  58. {
  59. return serialize($this->__serialize());
  60. }
  61. /**
  62. * @internal
  63. */
  64. final public function unserialize($serialized)
  65. {
  66. $this->__unserialize(unserialize($serialized));
  67. }
  68. public function __serialize(): array
  69. {
  70. return [$this->toString()];
  71. }
  72. public function __unserialize(array $data): void
  73. {
  74. [$this->message] = $data;
  75. }
  76. }