/framework/vendor/swift/lib/classes/Swift/Encoder/Rfc2231Encoder.php

http://zoop.googlecode.com/ · PHP · 89 lines · 40 code · 13 blank · 36 comment · 5 complexity · 5d7ab5daabeacb4ec43f826d538ca751 MD5 · raw file

  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. //@require 'Swift/Encoder.php';
  10. //@require 'Swift/CharacterStream.php';
  11. /**
  12. * Handles RFC 2231 specified Encoding in Swift Mailer.
  13. * @package Swift
  14. * @subpackage Encoder
  15. * @author Chris Corbyn
  16. */
  17. class Swift_Encoder_Rfc2231Encoder implements Swift_Encoder
  18. {
  19. /**
  20. * A character stream to use when reading a string as characters instead of bytes.
  21. * @var Swift_CharacterStream
  22. * @access private
  23. */
  24. private $_charStream;
  25. /**
  26. * Creates a new Rfc2231Encoder using the given character stream instance.
  27. * @param Swift_CharacterStream
  28. */
  29. public function __construct(Swift_CharacterStream $charStream)
  30. {
  31. $this->_charStream = $charStream;
  32. }
  33. /**
  34. * Takes an unencoded string and produces a string encoded according to
  35. * RFC 2231 from it.
  36. * @param string $string to encode
  37. * @param int $firstLineOffset
  38. * @param int $maxLineLength, optional, 0 indicates the default of 75 bytes
  39. * @return string
  40. */
  41. public function encodeString($string, $firstLineOffset = 0,
  42. $maxLineLength = 0)
  43. {
  44. $lines = array(); $lineCount = 0;
  45. $lines[] = '';
  46. $currentLine =& $lines[$lineCount++];
  47. if (0 >= $maxLineLength)
  48. {
  49. $maxLineLength = 75;
  50. }
  51. $this->_charStream->flushContents();
  52. $this->_charStream->importString($string);
  53. $thisLineLength = $maxLineLength - $firstLineOffset;
  54. while (false !== $char = $this->_charStream->read(4))
  55. {
  56. $encodedChar = rawurlencode($char);
  57. if (0 != strlen($currentLine)
  58. && strlen($currentLine . $encodedChar) > $thisLineLength)
  59. {
  60. $lines[] = '';
  61. $currentLine =& $lines[$lineCount++];
  62. $thisLineLength = $maxLineLength;
  63. }
  64. $currentLine .= $encodedChar;
  65. }
  66. return implode("\r\n", $lines);
  67. }
  68. /**
  69. * Updates the charset used.
  70. * @param string $charset
  71. */
  72. public function charsetChanged($charset)
  73. {
  74. $this->_charStream->setCharacterSet($charset);
  75. }
  76. }