Open Journal Systems  3.3.0
AbstractMultipartPart.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 abstract class AbstractMultipartPart extends AbstractPart
20 {
21  private $boundary;
22  private $parts = [];
23 
24  public function __construct(AbstractPart ...$parts)
25  {
26  parent::__construct();
27 
28  foreach ($parts as $part) {
29  $this->parts[] = $part;
30  }
31  }
32 
36  public function getParts(): array
37  {
38  return $this->parts;
39  }
40 
41  public function getMediaType(): string
42  {
43  return 'multipart';
44  }
45 
46  public function getPreparedHeaders(): Headers
47  {
48  $headers = parent::getPreparedHeaders();
49  $headers->setHeaderParameter('Content-Type', 'boundary', $this->getBoundary());
50 
51  return $headers;
52  }
53 
54  public function bodyToString(): string
55  {
56  $parts = $this->getParts();
57  $string = '';
58  foreach ($parts as $part) {
59  $string .= '--'.$this->getBoundary()."\r\n".$part->toString()."\r\n";
60  }
61  $string .= '--'.$this->getBoundary()."--\r\n";
62 
63  return $string;
64  }
65 
66  public function bodyToIterable(): iterable
67  {
68  $parts = $this->getParts();
69  foreach ($parts as $part) {
70  yield '--'.$this->getBoundary()."\r\n";
71  yield from $part->toIterable();
72  yield "\r\n";
73  }
74  yield '--'.$this->getBoundary()."--\r\n";
75  }
76 
77  public function asDebugString(): string
78  {
79  $str = parent::asDebugString();
80  foreach ($this->getParts() as $part) {
81  $lines = explode("\n", $part->asDebugString());
82  $str .= "\n └ ".array_shift($lines);
83  foreach ($lines as $line) {
84  $str .= "\n |".$line;
85  }
86  }
87 
88  return $str;
89  }
90 
91  private function getBoundary(): string
92  {
93  if (null === $this->boundary) {
94  $this->boundary = strtr(base64_encode(random_bytes(6)), '+/', '-_');
95  }
96 
97  return $this->boundary;
98  }
99 }
Symfony\Component\Mime\Part\AbstractMultipartPart\getParts
getParts()
Definition: AbstractMultipartPart.php:36
Symfony\Component\Mime\Part\AbstractMultipartPart
Definition: AbstractMultipartPart.php:19
Symfony\Component\Mime\Part
Definition: AbstractMultipartPart.php:12
Symfony\Component\Mime\Part\AbstractMultipartPart\bodyToIterable
bodyToIterable()
Definition: AbstractMultipartPart.php:66
Symfony\Component\Mime\Part\AbstractPart
Definition: AbstractPart.php:19
Symfony\Component\Mime\Part\AbstractMultipartPart\getMediaType
getMediaType()
Definition: AbstractMultipartPart.php:41
Symfony\Component\Mime\Header\Headers
Definition: Headers.php:22
Symfony\Component\Mime\Part\AbstractMultipartPart\bodyToString
bodyToString()
Definition: AbstractMultipartPart.php:54
Symfony\Component\Mime\Part\AbstractMultipartPart\__construct
__construct(AbstractPart ... $parts)
Definition: AbstractMultipartPart.php:24
Symfony\Component\Mime\Part\AbstractMultipartPart\getPreparedHeaders
getPreparedHeaders()
Definition: AbstractMultipartPart.php:46
Symfony\Component\Mime\Part\AbstractMultipartPart\asDebugString
asDebugString()
Definition: AbstractMultipartPart.php:77