zhaojs
2023-07-31 d705c1fff4f6dcb08eda795229c98567dd5ec9f8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php
 
/*
 * This file is part of the Symfony package.
 *
 * (c) Fabien Potencier <fabien@symfony.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
 
namespace Symfony\Component\Mime\Part;
 
use Symfony\Component\Mime\Header\Headers;
 
/**
 * @author Sebastiaan Stok <s.stok@rollerscapes.net>
 */
class SMimePart extends AbstractPart
{
    /** @internal */
    protected $_headers;
 
    private $body;
    private $type;
    private $subtype;
    private $parameters;
 
    /**
     * @param iterable|string $body
     */
    public function __construct($body, string $type, string $subtype, array $parameters)
    {
        unset($this->_headers);
 
        parent::__construct();
 
        if (!\is_string($body) && !is_iterable($body)) {
            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)));
        }
 
        $this->body = $body;
        $this->type = $type;
        $this->subtype = $subtype;
        $this->parameters = $parameters;
    }
 
    public function getMediaType(): string
    {
        return $this->type;
    }
 
    public function getMediaSubtype(): string
    {
        return $this->subtype;
    }
 
    public function bodyToString(): string
    {
        if (\is_string($this->body)) {
            return $this->body;
        }
 
        $body = '';
        foreach ($this->body as $chunk) {
            $body .= $chunk;
        }
        $this->body = $body;
 
        return $body;
    }
 
    public function bodyToIterable(): iterable
    {
        if (\is_string($this->body)) {
            yield $this->body;
 
            return;
        }
 
        $body = '';
        foreach ($this->body as $chunk) {
            $body .= $chunk;
            yield $chunk;
        }
        $this->body = $body;
    }
 
    public function getPreparedHeaders(): Headers
    {
        $headers = clone parent::getHeaders();
 
        $headers->setHeaderBody('Parameterized', 'Content-Type', $this->getMediaType().'/'.$this->getMediaSubtype());
 
        foreach ($this->parameters as $name => $value) {
            $headers->setHeaderParameter('Content-Type', $name, $value);
        }
 
        return $headers;
    }
 
    public function __sleep(): array
    {
        // convert iterables to strings for serialization
        if (is_iterable($this->body)) {
            $this->body = $this->bodyToString();
        }
 
        $this->_headers = $this->getHeaders();
 
        return ['_headers', 'body', 'type', 'subtype', 'parameters'];
    }
 
    public function __wakeup(): void
    {
        $r = new \ReflectionProperty(AbstractPart::class, 'headers');
        $r->setAccessible(true);
        $r->setValue($this, $this->_headers);
        unset($this->_headers);
    }
}