Author: Chris Corbyn
Inheritance: extends Swift_Mime_CharsetObserver
  /**
   * Get a token as an encoded word for safe insertion into headers.
   * @param string $token to encode
   * @param int $firstLineOffset, optional
   * @return string
   */
  protected function getTokenAsEncodedWord($token, $firstLineOffset = 0)
  {
    //Adjust $firstLineOffset to account for space needed for syntax
    $charsetDecl = $this->_charset;
    if (isset($this->_lang))
    {
      $charsetDecl .= '*' . $this->_lang;
    }
    $encodingWrapperLength = strlen(
      '=?' . $charsetDecl . '?' . $this->_encoder->getName() . '??='
      );

    if ($firstLineOffset >= 75) //Does this logic need to be here?
    {
      $firstLineOffset = 0;
    }

    $encodedTextLines = explode("\r\n",
      $this->_encoder->encodeString(
        $token, $firstLineOffset, 75 - $encodingWrapperLength
        )
      );

    foreach ($encodedTextLines as $lineNum => $line)
    {
      $encodedTextLines[$lineNum] = '=?' . $charsetDecl .
        '?' . $this->_encoder->getName() .
        '?' . $line . '?=';
    }

    return implode("\r\n ", $encodedTextLines);
  }
 /**
  * Get a token as an encoded word for safe insertion into headers.
  *
  * @param string $token           token to encode
  * @param int    $firstLineOffset optional
  *
  * @return string
  */
 protected function getTokenAsEncodedWord($token, $firstLineOffset = 0)
 {
     // Adjust $firstLineOffset to account for space needed for syntax
     $charsetDecl = $this->charset;
     if (isset($this->lang)) {
         $charsetDecl .= '*' . $this->lang;
     }
     $encodingWrapperLength = strlen('=?' . $charsetDecl . '?' . $this->encoder->getName() . '??=');
     if ($firstLineOffset >= 75) {
         //Does this logic need to be here?
         $firstLineOffset = 0;
     }
     $encodedTextLines = explode("\r\n", $this->encoder->encodeString($token, $firstLineOffset, 75 - $encodingWrapperLength, $this->charset));
     if (strtolower($this->charset) !== 'iso-2022-jp') {
         // special encoding for iso-2022-jp using mb_encode_mimeheader
         foreach ($encodedTextLines as $lineNum => $line) {
             $encodedTextLines[$lineNum] = '=?' . $charsetDecl . '?' . $this->encoder->getName() . '?' . $line . '?=';
         }
     }
     return implode("\r\n ", $encodedTextLines);
 }
 /**
  * Render a RFC 2047 compliant header parameter from the $name and $value.
  *
  * @param string $name
  * @param string $value
  *
  * @return string
  */
 private function _createParameter($name, $value)
 {
     $origValue = $value;
     $encoded = false;
     // Allow room for parameter name, indices, "=" and DQUOTEs
     $maxValueLength = $this->getMaxLineLength() - strlen($name . '=*N"";') - 1;
     $firstLineOffset = 0;
     // If it's not already a valid parameter value...
     if (!preg_match('/^' . self::TOKEN_REGEX . '$/D', $value)) {
         // TODO: text, or something else??
         // ... and it's not ascii
         if (!preg_match('/^' . $this->getGrammar()->getDefinition('text') . '*$/D', $value)) {
             $encoded = true;
             // Allow space for the indices, charset and language
             $maxValueLength = $this->getMaxLineLength() - strlen($name . '*N*="";') - 1;
             $firstLineOffset = strlen($this->getCharset() . "'" . $this->getLanguage() . "'");
         }
     }
     // Encode if we need to
     if ($encoded || strlen($value) > $maxValueLength) {
         if (isset($this->_paramEncoder)) {
             $value = $this->_paramEncoder->encodeString($origValue, $firstLineOffset, $maxValueLength, $this->getCharset());
         } else {
             // We have to go against RFC 2183/2231 in some areas for interoperability
             $value = $this->getTokenAsEncodedWord($origValue);
             $encoded = false;
         }
     }
     $valueLines = isset($this->_paramEncoder) ? explode("\r\n", $value) : array($value);
     // Need to add indices
     if (count($valueLines) > 1) {
         $paramLines = array();
         foreach ($valueLines as $i => $line) {
             $paramLines[] = $name . '*' . $i . $this->_getEndOfParameterValue($line, true, $i == 0);
         }
         return implode(";\r\n ", $paramLines);
     } else {
         return $name . $this->_getEndOfParameterValue($valueLines[0], $encoded, true);
     }
 }