Open Journal Systems  3.3.0
SMimePart.php
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 
13 
15 
19 class SMimePart extends AbstractPart
20 {
21  private $body;
22  private $type;
23  private $subtype;
24  private $parameters;
25 
29  public function __construct($body, string $type, string $subtype, array $parameters)
30  {
31  parent::__construct();
32 
33  if (!\is_string($body) && !is_iterable($body)) {
34  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)));
35  }
36 
37  $this->body = $body;
38  $this->type = $type;
39  $this->subtype = $subtype;
40  $this->parameters = $parameters;
41  }
42 
43  public function getMediaType(): string
44  {
45  return $this->type;
46  }
47 
48  public function getMediaSubtype(): string
49  {
50  return $this->subtype;
51  }
52 
53  public function bodyToString(): string
54  {
55  if (\is_string($this->body)) {
56  return $this->body;
57  }
58 
59  $body = '';
60  foreach ($this->body as $chunk) {
61  $body .= $chunk;
62  }
63  $this->body = $body;
64 
65  return $body;
66  }
67 
68  public function bodyToIterable(): iterable
69  {
70  if (\is_string($this->body)) {
71  yield $this->body;
72 
73  return;
74  }
75 
76  $body = '';
77  foreach ($this->body as $chunk) {
78  $body .= $chunk;
79  yield $chunk;
80  }
81  $this->body = $body;
82  }
83 
84  public function getPreparedHeaders(): Headers
85  {
86  $headers = clone parent::getHeaders();
87 
88  $headers->setHeaderBody('Parameterized', 'Content-Type', $this->getMediaType().'/'.$this->getMediaSubtype());
89 
90  foreach ($this->parameters as $name => $value) {
91  $headers->setHeaderParameter('Content-Type', $name, $value);
92  }
93 
94  return $headers;
95  }
96 
97  public function __sleep(): array
98  {
99  // convert iterables to strings for serialization
100  if (is_iterable($this->body)) {
101  $this->body = $this->bodyToString();
102  }
103 
104  $this->_headers = $this->getHeaders();
105 
106  return ['_headers', 'body', 'type', 'subtype', 'parameters'];
107  }
108 
109  public function __wakeup(): void
110  {
111  $r = new \ReflectionProperty(AbstractPart::class, 'headers');
112  $r->setAccessible(true);
113  $r->setValue($this, $this->_headers);
114  unset($this->_headers);
115  }
116 }
Symfony\Component\Mime\Part\AbstractPart\getHeaders
getHeaders()
Definition: AbstractPart.php:28
Symfony\Component\Mime\Part\SMimePart\getMediaSubtype
getMediaSubtype()
Definition: SMimePart.php:48
Symfony\Component\Mime\Part\SMimePart\__sleep
__sleep()
Definition: SMimePart.php:97
Symfony\Component\Mime\Part\SMimePart\bodyToIterable
bodyToIterable()
Definition: SMimePart.php:68
Symfony\Component\Mime\Part\SMimePart\__wakeup
__wakeup()
Definition: SMimePart.php:109
Symfony\Component\Mime\Part\SMimePart\bodyToString
bodyToString()
Definition: SMimePart.php:53
Symfony\Component\Mime\Part
Definition: AbstractMultipartPart.php:12
Symfony\Component\Mime\Part\AbstractPart
Definition: AbstractPart.php:19
Symfony\Component\Mime\Part\SMimePart\getMediaType
getMediaType()
Definition: SMimePart.php:43
Symfony\Component\Mime\Part\SMimePart\__construct
__construct($body, string $type, string $subtype, array $parameters)
Definition: SMimePart.php:29
Symfony\Component\Mime\Part\SMimePart\getPreparedHeaders
getPreparedHeaders()
Definition: SMimePart.php:84
Symfony\Component\Mime\Header\Headers
Definition: Headers.php:22
Symfony\Component\Mime\Part\SMimePart
Definition: SMimePart.php:19