Dashboard sipadu mbip
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AbstractMultipartPart.php 2.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Mime\Part;
  11. use Symfony\Component\Mime\Header\Headers;
  12. /**
  13. * @author Fabien Potencier <fabien@symfony.com>
  14. *
  15. * @experimental in 4.3
  16. */
  17. abstract class AbstractMultipartPart extends AbstractPart
  18. {
  19. private $boundary;
  20. private $parts = [];
  21. public function __construct(AbstractPart ...$parts)
  22. {
  23. parent::__construct();
  24. foreach ($parts as $part) {
  25. $this->parts[] = $part;
  26. }
  27. }
  28. /**
  29. * @return AbstractPart[]
  30. */
  31. public function getParts(): array
  32. {
  33. return $this->parts;
  34. }
  35. public function getMediaType(): string
  36. {
  37. return 'multipart';
  38. }
  39. public function getPreparedHeaders(): Headers
  40. {
  41. $headers = parent::getPreparedHeaders();
  42. $headers->setHeaderParameter('Content-Type', 'boundary', $this->getBoundary());
  43. return $headers;
  44. }
  45. public function bodyToString(): string
  46. {
  47. $parts = $this->getParts();
  48. $string = '';
  49. foreach ($parts as $part) {
  50. $string .= '--'.$this->getBoundary()."\r\n".$part->toString()."\r\n";
  51. }
  52. $string .= '--'.$this->getBoundary()."--\r\n";
  53. return $string;
  54. }
  55. public function bodyToIterable(): iterable
  56. {
  57. $parts = $this->getParts();
  58. foreach ($parts as $part) {
  59. yield '--'.$this->getBoundary()."\r\n";
  60. yield from $part->toIterable();
  61. yield "\r\n";
  62. }
  63. yield '--'.$this->getBoundary()."--\r\n";
  64. }
  65. private function getBoundary(): string
  66. {
  67. if (null === $this->boundary) {
  68. $this->boundary = '_=_symfony_'.time().'_'.bin2hex(random_bytes(16)).'_=_';
  69. }
  70. return $this->boundary;
  71. }
  72. }