"Fossies" - the Fresh Open Source Software Archive

Member "drupal-9.4.5/vendor/symfony/mime/Part/AbstractMultipartPart.php" (20 Jul 2022, 2345 Bytes) of package /linux/www/drupal-9.4.5.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    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 Fabien Potencier <fabien@symfony.com>
   18  */
   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 
   33     /**
   34      * @return AbstractPart[]
   35      */
   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 }