| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | namespace Genkgo\Mail\Mime; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Genkgo\Mail\GenericMessage; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Genkgo\Mail\Header\ContentType; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Genkgo\Mail\Header\ParsedHeader; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Genkgo\Mail\Header\HeaderValueParameter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Genkgo\Mail\HeaderInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Genkgo\Mail\MessageInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Genkgo\Mail\Stream\EmptyStream; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Genkgo\Mail\Stream\LineIterator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Genkgo\Mail\StreamInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | final class MultiPart implements MultiPartInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |      * @var PartInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     private $decoratedPart; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      * @var Boundary | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     private $boundary; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * @var iterable|PartInterface[] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     private $parts = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 | 8 |  |      * @param Boundary $boundary | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      * @param ContentType $contentType | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 | 8 |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     public function __construct(Boundary $boundary, ContentType $contentType) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 | 8 |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 | 1 |  |         $this->boundary = $boundary; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         if (\substr((string)$contentType->getValue(), 0, 10) !== 'multipart/') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 | 7 |  |             throw new \InvalidArgumentException('Content type must be of type multipart/type'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 | 7 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 7 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 7 |  |         $this->decoratedPart = (new GenericPart()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 7 |  |             ->withHeader( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 7 |  |                 new ParsedHeader( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 | 7 |  |                     $contentType->getName(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 | 7 |  |                     $contentType->getValue() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 | 7 |  |                         ->withParameter( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |                             new HeaderValueParameter( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |                                 'boundary', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |                                 (string)$boundary | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |                             ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 | 7 |  |                         ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |                 ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 3 |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      * @return iterable | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 3 |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     public function getHeaders(): iterable | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         return $this->decoratedPart->getHeaders(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 1 |  |      * @param string $name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 | 1 |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     public function hasHeader(string $name): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         return $this->decoratedPart->hasHeader($name); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 3 |  |      * @param string $name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |      * @return HeaderInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 3 |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     public function getHeader(string $name): HeaderInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         return $this->decoratedPart->getHeader($name); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 | 1 |  |      * @param HeaderInterface $header | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |      * @return PartInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 | 1 |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     public function withHeader(HeaderInterface $header): PartInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         throw new \RuntimeException('Cannot modify headers of MultiPart'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 | 1 |  |      * @param string $name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |      * @return PartInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 | 1 |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     public function withoutHeader(string $name): PartInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         throw new \RuntimeException('Cannot modify headers of MultiPart'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 | 1 |  |      * @param StreamInterface $body | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |      * @return PartInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 | 1 |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |     public function withBody(StreamInterface $body): PartInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         throw new \RuntimeException('Cannot modify body of MultiPart'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 | 2 |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |      * @return StreamInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 | 2 |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |     public function getBody(): StreamInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |         return new EmptyStream(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 | 2 |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |      * @return Boundary | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 | 2 |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |     public function getBoundary(): Boundary | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |         return $this->boundary; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 | 3 |  |      * @param PartInterface $part | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |      * @return MultiPartInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 | 3 |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 | 3 |  |     public function withPart(PartInterface $part): MultiPartInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 | 3 |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |         $clone = clone $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |         $clone->parts[] = $part; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |         return $clone; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 | 3 |  |      * @param iterable|PartInterface[] $parts | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |      * @return MultiPartInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 | 3 |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |     public function withParts(iterable $parts): MultiPartInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 | 3 |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 | 3 |  |         $clone = clone $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |         foreach ($parts as $part) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 | 3 |  |             $clone->parts[] = $part; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |         return $clone; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 | 2 |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |      * @return iterable|PartInterface[] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 | 2 |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |     public function getParts(): iterable | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |         return $this->parts; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |      * @param MessageInterface $message | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |      * @return MultiPart | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 166 |  |  |      */ | 
            
                                                        
            
                                    
            
            
                | 167 |  |  |     public static function fromMessage(MessageInterface $message): self | 
            
                                                        
            
                                    
            
            
                | 168 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 169 |  |  |         foreach ($message->getHeader('Content-Type') as $header) { | 
            
                                                        
            
                                    
            
            
                | 170 |  |  |             $contentType = $header->getValue()->getRaw(); | 
            
                                                        
            
                                    
            
            
                | 171 |  |  |             if (\substr($contentType, 0, 10) !== 'multipart/') { | 
            
                                                        
            
                                    
            
            
                | 172 |  |  |                 throw new \InvalidArgumentException( | 
            
                                                        
            
                                    
            
            
                | 173 |  |  |                     \sprintf( | 
            
                                                        
            
                                    
            
            
                | 174 |  |  |                         'Message is not a multipart/alternative message, but %s', | 
            
                                                        
            
                                    
            
            
                | 175 |  |  |                         $contentType | 
            
                                                        
            
                                    
            
            
                | 176 |  |  |                     ) | 
            
                                                        
            
                                    
            
            
                | 177 |  |  |                 ); | 
            
                                                        
            
                                    
            
            
                | 178 |  |  |             } | 
            
                                                        
            
                                    
            
            
                | 179 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 180 |  |  |             try { | 
            
                                                        
            
                                    
            
            
                | 181 |  |  |                 $boundary = new Boundary($header->getValue()->getParameter('boundary')->getValue()); | 
            
                                                        
            
                                    
            
            
                | 182 |  |  |             } catch (\UnexpectedValueException $e) { | 
            
                                                        
            
                                    
            
            
                | 183 |  |  |                 throw new \InvalidArgumentException('Message does not contain a boundary'); | 
            
                                                        
            
                                    
            
            
                | 184 |  |  |             } | 
            
                                                        
            
                                    
            
            
                | 185 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 186 |  |  |             $part = new self( | 
            
                                                        
            
                                    
            
            
                | 187 |  |  |                 $boundary, | 
            
                                                        
            
                                    
            
            
                | 188 |  |  |                 new ContentType($header->getValue()->getRaw()) | 
            
                                                        
            
                                    
            
            
                | 189 |  |  |             ); | 
            
                                                        
            
                                    
            
            
                | 190 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 191 |  |  |             $content = ''; | 
            
                                                        
            
                                    
            
            
                | 192 |  |  |             $preamble = true; | 
            
                                                        
            
                                    
            
            
                | 193 |  |  |             foreach (new LineIterator($message->getBody()) as $line) { | 
            
                                                        
            
                                    
            
            
                | 194 |  |  |                 if ($boundary->isOpening($line) && $preamble) { | 
            
                                                        
            
                                    
            
            
                | 195 |  |  |                     $content = ''; | 
            
                                                        
            
                                    
            
            
                | 196 |  |  |                     $preamble = false; | 
            
                                                        
            
                                    
            
            
                | 197 |  |  |                     continue; | 
            
                                                        
            
                                    
            
            
                | 198 |  |  |                 } | 
            
                                                        
            
                                    
            
            
                | 199 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 200 |  |  |                 if ($boundary->isClosing($line) || $boundary->isOpening($line)) { | 
            
                                                        
            
                                    
            
            
                | 201 |  |  |                     $message = GenericMessage::fromString(\rtrim($content)); | 
            
                                                        
            
                                    
            
            
                | 202 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 203 |  |  |                     try { | 
            
                                                        
            
                                    
            
            
                | 204 |  |  |                         $part->parts[] = MultiPart::fromMessage($message); | 
            
                                                        
            
                                    
            
            
                | 205 |  |  |                     } catch (\InvalidArgumentException $e) { | 
            
                                                        
            
                                    
            
            
                | 206 |  |  |                         $part->parts[] = GenericPart::fromMessage($message); | 
            
                                                        
            
                                    
            
            
                | 207 |  |  |                     } | 
            
                                                        
            
                                    
            
            
                | 208 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 209 |  |  |                     $content = ''; | 
            
                                                        
            
                                    
            
            
                | 210 |  |  |                 } | 
            
                                                        
            
                                    
            
            
                | 211 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 212 |  |  |                 if ($boundary->isOpening($line)) { | 
            
                                                        
            
                                    
            
            
                | 213 |  |  |                     continue; | 
            
                                                        
            
                                    
            
            
                | 214 |  |  |                 } | 
            
                                                        
            
                                    
            
            
                | 215 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 216 |  |  |                 if ($boundary->isClosing($line)) { | 
            
                                                        
            
                                    
            
            
                | 217 |  |  |                     break; | 
            
                                                        
            
                                    
            
            
                | 218 |  |  |                 } | 
            
                                                        
            
                                    
            
            
                | 219 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 220 |  |  |                 $content .= $line . "\r\n"; | 
            
                                                        
            
                                    
            
            
                | 221 |  |  |             } | 
            
                                                        
            
                                    
            
            
                | 222 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 223 |  |  |             return $part; | 
            
                                                        
            
                                    
            
            
                | 224 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 225 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 226 |  |  |         throw new \InvalidArgumentException('Message is not a multipart/alternative message'); | 
            
                                                        
            
                                    
            
            
                | 227 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 228 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 229 |  |  |  |