Dashboard sipadu mbip
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Rfc2231Encoder.php 1.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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\Encoder;
  11. use Symfony\Component\Mime\CharacterStream;
  12. /**
  13. * @author Chris Corbyn
  14. *
  15. * @experimental in 4.3
  16. */
  17. final class Rfc2231Encoder implements EncoderInterface
  18. {
  19. /**
  20. * Takes an unencoded string and produces a string encoded according to RFC 2231 from it.
  21. */
  22. public function encodeString(string $string, ?string $charset = 'utf-8', int $firstLineOffset = 0, int $maxLineLength = 0): string
  23. {
  24. $lines = [];
  25. $lineCount = 0;
  26. $lines[] = '';
  27. $currentLine = &$lines[$lineCount++];
  28. if (0 >= $maxLineLength) {
  29. $maxLineLength = 75;
  30. }
  31. $charStream = new CharacterStream($string, $charset);
  32. $thisLineLength = $maxLineLength - $firstLineOffset;
  33. while (null !== $char = $charStream->read(4)) {
  34. $encodedChar = rawurlencode($char);
  35. if (0 !== \strlen($currentLine) && \strlen($currentLine.$encodedChar) > $thisLineLength) {
  36. $lines[] = '';
  37. $currentLine = &$lines[$lineCount++];
  38. $thisLineLength = $maxLineLength;
  39. }
  40. $currentLine .= $encodedChar;
  41. }
  42. return implode("\r\n", $lines);
  43. }
  44. }