QpContentEncoderProxy.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. /*
  3. * This file is part of SwiftMailer.
  4. * (c) 2004-2009 Chris Corbyn
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. /**
  10. * Proxy for quoted-printable content encoders.
  11. *
  12. * Switches on the best QP encoder implementation for current charset.
  13. *
  14. * @author Jean-François Simon <jeanfrancois.simon@sensiolabs.com>
  15. */
  16. class Swift_Mime_ContentEncoder_QpContentEncoderProxy implements Swift_Mime_ContentEncoder
  17. {
  18. /**
  19. * @var Swift_Mime_ContentEncoder_QpContentEncoder
  20. */
  21. private $safeEncoder;
  22. /**
  23. * @var Swift_Mime_ContentEncoder_NativeQpContentEncoder
  24. */
  25. private $nativeEncoder;
  26. /**
  27. * @var string|null
  28. */
  29. private $charset;
  30. /**
  31. * Constructor.
  32. *
  33. * @param string|null $charset
  34. */
  35. public function __construct(Swift_Mime_ContentEncoder_QpContentEncoder $safeEncoder, Swift_Mime_ContentEncoder_NativeQpContentEncoder $nativeEncoder, $charset)
  36. {
  37. $this->safeEncoder = $safeEncoder;
  38. $this->nativeEncoder = $nativeEncoder;
  39. $this->charset = $charset;
  40. }
  41. /**
  42. * Make a deep copy of object.
  43. */
  44. public function __clone()
  45. {
  46. $this->safeEncoder = clone $this->safeEncoder;
  47. $this->nativeEncoder = clone $this->nativeEncoder;
  48. }
  49. /**
  50. * {@inheritdoc}
  51. */
  52. public function charsetChanged($charset)
  53. {
  54. $this->charset = $charset;
  55. $this->safeEncoder->charsetChanged($charset);
  56. }
  57. /**
  58. * {@inheritdoc}
  59. */
  60. public function encodeByteStream(Swift_OutputByteStream $os, Swift_InputByteStream $is, $firstLineOffset = 0, $maxLineLength = 0)
  61. {
  62. $this->getEncoder()->encodeByteStream($os, $is, $firstLineOffset, $maxLineLength);
  63. }
  64. /**
  65. * {@inheritdoc}
  66. */
  67. public function getName()
  68. {
  69. return 'quoted-printable';
  70. }
  71. /**
  72. * {@inheritdoc}
  73. */
  74. public function encodeString($string, $firstLineOffset = 0, $maxLineLength = 0)
  75. {
  76. return $this->getEncoder()->encodeString($string, $firstLineOffset, $maxLineLength);
  77. }
  78. /**
  79. * @return Swift_Mime_ContentEncoder
  80. */
  81. private function getEncoder()
  82. {
  83. return 'utf-8' === $this->charset ? $this->nativeEncoder : $this->safeEncoder;
  84. }
  85. }