SMimePart.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. private $body;
  18. private $type;
  19. private $subtype;
  20. private $parameters;
  21. /**
  22. * @param iterable|string $body
  23. */
  24. public function __construct($body, string $type, string $subtype, array $parameters)
  25. {
  26. parent::__construct();
  27. if (!\is_string($body) && !is_iterable($body)) {
  28. 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)));
  29. }
  30. $this->body = $body;
  31. $this->type = $type;
  32. $this->subtype = $subtype;
  33. $this->parameters = $parameters;
  34. }
  35. public function getMediaType(): string
  36. {
  37. return $this->type;
  38. }
  39. public function getMediaSubtype(): string
  40. {
  41. return $this->subtype;
  42. }
  43. public function bodyToString(): string
  44. {
  45. if (\is_string($this->body)) {
  46. return $this->body;
  47. }
  48. $body = '';
  49. foreach ($this->body as $chunk) {
  50. $body .= $chunk;
  51. }
  52. $this->body = $body;
  53. return $body;
  54. }
  55. public function bodyToIterable(): iterable
  56. {
  57. if (\is_string($this->body)) {
  58. yield $this->body;
  59. return;
  60. }
  61. $body = '';
  62. foreach ($this->body as $chunk) {
  63. $body .= $chunk;
  64. yield $chunk;
  65. }
  66. $this->body = $body;
  67. }
  68. public function getPreparedHeaders(): Headers
  69. {
  70. $headers = clone parent::getHeaders();
  71. $headers->setHeaderBody('Parameterized', 'Content-Type', $this->getMediaType().'/'.$this->getMediaSubtype());
  72. foreach ($this->parameters as $name => $value) {
  73. $headers->setHeaderParameter('Content-Type', $name, $value);
  74. }
  75. return $headers;
  76. }
  77. public function __sleep(): array
  78. {
  79. // convert iterables to strings for serialization
  80. if (is_iterable($this->body)) {
  81. $this->body = $this->bodyToString();
  82. }
  83. $this->_headers = $this->getHeaders();
  84. return ['_headers', 'body', 'type', 'subtype', 'parameters'];
  85. }
  86. public function __wakeup(): void
  87. {
  88. $r = new \ReflectionProperty(AbstractPart::class, 'headers');
  89. $r->setAccessible(true);
  90. $r->setValue($this, $this->_headers);
  91. unset($this->_headers);
  92. }
  93. }