RelatedPart.php 1.4 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. private $mainPart;
  19. public function __construct(AbstractPart $mainPart, AbstractPart $part, AbstractPart ...$parts)
  20. {
  21. $this->mainPart = $mainPart;
  22. $this->prepareParts($part, ...$parts);
  23. parent::__construct($part, ...$parts);
  24. }
  25. public function getParts(): array
  26. {
  27. return array_merge([$this->mainPart], parent::getParts());
  28. }
  29. public function getMediaSubtype(): string
  30. {
  31. return 'related';
  32. }
  33. private function generateContentId(): string
  34. {
  35. return bin2hex(random_bytes(16)).'@symfony';
  36. }
  37. private function prepareParts(AbstractPart ...$parts): void
  38. {
  39. foreach ($parts as $part) {
  40. if (!$part->getHeaders()->has('Content-ID')) {
  41. $part->getHeaders()->setHeaderBody('Id', 'Content-ID', $this->generateContentId());
  42. }
  43. }
  44. }
  45. }