RawMessage.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. return $this->message = implode('', iterator_to_array($this->message, false));
  31. }
  32. public function toIterable(): iterable
  33. {
  34. if (\is_string($this->message)) {
  35. yield $this->message;
  36. return;
  37. }
  38. $message = '';
  39. foreach ($this->message as $chunk) {
  40. $message .= $chunk;
  41. yield $chunk;
  42. }
  43. $this->message = $message;
  44. }
  45. /**
  46. * @throws LogicException if the message is not valid
  47. */
  48. public function ensureValidity()
  49. {
  50. }
  51. /**
  52. * @internal
  53. */
  54. final public function serialize(): string
  55. {
  56. return serialize($this->__serialize());
  57. }
  58. /**
  59. * @internal
  60. */
  61. final public function unserialize($serialized)
  62. {
  63. $this->__unserialize(unserialize($serialized));
  64. }
  65. public function __serialize(): array
  66. {
  67. return [$this->toString()];
  68. }
  69. public function __unserialize(array $data): void
  70. {
  71. [$this->message] = $data;
  72. }
  73. }