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