RelatedPart.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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\Multipart;
  11. use Symfony\Component\Mime\Part\AbstractMultipartPart;
  12. use Symfony\Component\Mime\Part\AbstractPart;
  13. /**
  14. * @author Fabien Potencier <fabien@symfony.com>
  15. */
  16. final class RelatedPart extends AbstractMultipartPart
  17. {
  18. public function __construct(
  19. private AbstractPart $mainPart,
  20. AbstractPart $part,
  21. AbstractPart ...$parts,
  22. ) {
  23. $this->prepareParts($part, ...$parts);
  24. parent::__construct($part, ...$parts);
  25. }
  26. public function getParts(): array
  27. {
  28. return array_merge([$this->mainPart], parent::getParts());
  29. }
  30. public function getMediaSubtype(): string
  31. {
  32. return 'related';
  33. }
  34. private function generateContentId(): string
  35. {
  36. return bin2hex(random_bytes(16)).'@symfony';
  37. }
  38. private function prepareParts(AbstractPart ...$parts): void
  39. {
  40. foreach ($parts as $part) {
  41. if (!$part->getHeaders()->has('Content-ID')) {
  42. $part->getHeaders()->setHeaderBody('Id', 'Content-ID', $this->generateContentId());
  43. }
  44. }
  45. }
  46. }