QpContentEncoderProxy.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 null|string
  28. */
  29. private $charset;
  30. /**
  31. * Constructor.
  32. *
  33. * @param Swift_Mime_ContentEncoder_QpContentEncoder $safeEncoder
  34. * @param Swift_Mime_ContentEncoder_NativeQpContentEncoder $nativeEncoder
  35. * @param string|null $charset
  36. */
  37. public function __construct(Swift_Mime_ContentEncoder_QpContentEncoder $safeEncoder, Swift_Mime_ContentEncoder_NativeQpContentEncoder $nativeEncoder, $charset)
  38. {
  39. $this->safeEncoder = $safeEncoder;
  40. $this->nativeEncoder = $nativeEncoder;
  41. $this->charset = $charset;
  42. }
  43. /**
  44. * {@inheritdoc}
  45. */
  46. public function charsetChanged($charset)
  47. {
  48. $this->charset = $charset;
  49. }
  50. /**
  51. * {@inheritdoc}
  52. */
  53. public function encodeByteStream(Swift_OutputByteStream $os, Swift_InputByteStream $is, $firstLineOffset = 0, $maxLineLength = 0)
  54. {
  55. $this->getEncoder()->encodeByteStream($os, $is, $firstLineOffset, $maxLineLength);
  56. }
  57. /**
  58. * {@inheritdoc}
  59. */
  60. public function getName()
  61. {
  62. return 'quoted-printable';
  63. }
  64. /**
  65. * {@inheritdoc}
  66. */
  67. public function encodeString($string, $firstLineOffset = 0, $maxLineLength = 0)
  68. {
  69. return $this->getEncoder()->encodeString($string, $firstLineOffset, $maxLineLength);
  70. }
  71. /**
  72. * @return Swift_Mime_ContentEncoder
  73. */
  74. private function getEncoder()
  75. {
  76. return 'utf-8' === $this->charset ? $this->nativeEncoder : $this->safeEncoder;
  77. }
  78. }