SMimePart.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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\Part;
  11. use Symfony\Component\Mime\Header\Headers;
  12. /**
  13. * @author Sebastiaan Stok <s.stok@rollerscapes.net>
  14. */
  15. class SMimePart extends AbstractPart
  16. {
  17. /** @internal */
  18. protected $_headers;
  19. private $body;
  20. private $type;
  21. private $subtype;
  22. private $parameters;
  23. /**
  24. * @param iterable|string $body
  25. */
  26. public function __construct($body, string $type, string $subtype, array $parameters)
  27. {
  28. unset($this->_headers);
  29. parent::__construct();
  30. if (!\is_string($body) && !is_iterable($body)) {
  31. throw new \TypeError(sprintf('The body of "%s" must be a string or a iterable (got "%s").', self::class, \is_object($body) ? \get_class($body) : \gettype($body)));
  32. }
  33. $this->body = $body;
  34. $this->type = $type;
  35. $this->subtype = $subtype;
  36. $this->parameters = $parameters;
  37. }
  38. public function getMediaType(): string
  39. {
  40. return $this->type;
  41. }
  42. public function getMediaSubtype(): string
  43. {
  44. return $this->subtype;
  45. }
  46. public function bodyToString(): string
  47. {
  48. if (\is_string($this->body)) {
  49. return $this->body;
  50. }
  51. $body = '';
  52. foreach ($this->body as $chunk) {
  53. $body .= $chunk;
  54. }
  55. $this->body = $body;
  56. return $body;
  57. }
  58. public function bodyToIterable(): iterable
  59. {
  60. if (\is_string($this->body)) {
  61. yield $this->body;
  62. return;
  63. }
  64. $body = '';
  65. foreach ($this->body as $chunk) {
  66. $body .= $chunk;
  67. yield $chunk;
  68. }
  69. $this->body = $body;
  70. }
  71. public function getPreparedHeaders(): Headers
  72. {
  73. $headers = clone parent::getHeaders();
  74. $headers->setHeaderBody('Parameterized', 'Content-Type', $this->getMediaType().'/'.$this->getMediaSubtype());
  75. foreach ($this->parameters as $name => $value) {
  76. $headers->setHeaderParameter('Content-Type', $name, $value);
  77. }
  78. return $headers;
  79. }
  80. public function __sleep(): array
  81. {
  82. // convert iterables to strings for serialization
  83. if (is_iterable($this->body)) {
  84. $this->body = $this->bodyToString();
  85. }
  86. $this->_headers = $this->getHeaders();
  87. return ['_headers', 'body', 'type', 'subtype', 'parameters'];
  88. }
  89. public function __wakeup(): void
  90. {
  91. $r = new \ReflectionProperty(AbstractPart::class, 'headers');
  92. $r->setAccessible(true);
  93. $r->setValue($this, $this->_headers);
  94. unset($this->_headers);
  95. }
  96. }