AbstractPart.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. abstract class AbstractPart
  16. {
  17. private Headers $headers;
  18. public function __construct()
  19. {
  20. $this->headers = new Headers();
  21. }
  22. public function getHeaders(): Headers
  23. {
  24. return $this->headers;
  25. }
  26. public function getPreparedHeaders(): Headers
  27. {
  28. $headers = clone $this->headers;
  29. $headers->setHeaderBody('Parameterized', 'Content-Type', $this->getMediaType().'/'.$this->getMediaSubtype());
  30. return $headers;
  31. }
  32. public function toString(): string
  33. {
  34. return $this->getPreparedHeaders()->toString()."\r\n".$this->bodyToString();
  35. }
  36. public function toIterable(): iterable
  37. {
  38. yield $this->getPreparedHeaders()->toString();
  39. yield "\r\n";
  40. yield from $this->bodyToIterable();
  41. }
  42. public function asDebugString(): string
  43. {
  44. return $this->getMediaType().'/'.$this->getMediaSubtype();
  45. }
  46. abstract public function bodyToString(): string;
  47. abstract public function bodyToIterable(): iterable;
  48. abstract public function getMediaType(): string;
  49. abstract public function getMediaSubtype(): string;
  50. public function __serialize(): array
  51. {
  52. if (!method_exists($this, '__sleep')) {
  53. return ['headers' => $this->headers];
  54. }
  55. trigger_deprecation('symfony/mime', '7.4', 'Implementing "%s::__sleep()" is deprecated, use "__serialize()" instead.', get_debug_type($this));
  56. $data = [];
  57. foreach ($this->__sleep() as $key) {
  58. try {
  59. if (($r = new \ReflectionProperty($this, $key))->isInitialized($this)) {
  60. $data[$key] = $r->getValue($this);
  61. }
  62. } catch (\ReflectionException) {
  63. $data[$key] = $this->$key;
  64. }
  65. }
  66. return $data;
  67. }
  68. public function __unserialize(array $data): void
  69. {
  70. if ($wakeup = method_exists($this, '__wakeup') && self::class === (new \ReflectionMethod($this, '__unserialize'))->class) {
  71. trigger_deprecation('symfony/mime', '7.4', 'Implementing "%s::__wakeup()" is deprecated, use "__unserialize()" instead.', get_debug_type($this));
  72. }
  73. if (['headers'] === array_keys($data)) {
  74. $this->headers = $data['headers'];
  75. if ($wakeup) {
  76. $this->__wakeup();
  77. }
  78. return;
  79. }
  80. trigger_deprecation('symfony/mime', '7.4', 'Passing more than just key "headers" to "%s::__unserialize()" is deprecated, populate properties in "%s::__unserialize()" instead.', self::class, get_debug_type($this));
  81. \Closure::bind(function ($data) use ($wakeup) {
  82. foreach ($data as $key => $value) {
  83. $this->{("\0" === $key[0] ?? '') ? substr($key, 1 + strrpos($key, "\0")) : $key} = $value;
  84. }
  85. if ($wakeup) {
  86. $this->__wakeup();
  87. }
  88. }, $this, static::class)($data);
  89. }
  90. }