QP encoded strings have a maximum line length of 76 characters.
If the first line needs to be shorter, indicate the difference with
$firstLineOffset.
public encodeString ( string $string, $firstLineOffset, $maxLineLength ) : string | ||
$string | string | to encode |
Résultat | string |
public function testEncodingAndDecodingSamples() { $sampleFp = opendir($this->_samplesDir); while (false !== ($encodingDir = readdir($sampleFp))) { if (substr($encodingDir, 0, 1) == '.') { continue; } $encoding = $encodingDir; $charStream = new Swift_CharacterStream_ArrayCharacterStream($this->_factory, $encoding); $encoder = new Swift_Encoder_QpEncoder($charStream); $sampleDir = $this->_samplesDir . '/' . $encodingDir; if (is_dir($sampleDir)) { $fileFp = opendir($sampleDir); while (false !== ($sampleFile = readdir($fileFp))) { if (substr($sampleFile, 0, 1) == '.') { continue; } $text = file_get_contents($sampleDir . '/' . $sampleFile); $encodedText = $encoder->encodeString($text); foreach (explode("\r\n", $encodedText) as $line) { $this->assertLessThanOrEqual(76, strlen($line)); } $this->assertEquals(quoted_printable_decode($encodedText), $text, '%s: Encoded string should decode back to original string for sample ' . $sampleDir . '/' . $sampleFile); } closedir($fileFp); } } closedir($sampleFp); }
public function testFirstLineLengthCanBeDifferent() { $input = str_repeat('a', 140); $charStream = $this->_createCharStream(); $charStream->shouldReceive('flushContents')->once(); $charStream->shouldReceive('importString')->once()->with($input); $output = ''; for ($i = 0; $i < 140; ++$i) { $charStream->shouldReceive('readBytes')->once()->andReturn(array(ord('a'))); if (53 == $i || 53 + 75 == $i) { $output .= "=\r\n"; } $output .= 'a'; } $charStream->shouldReceive('readBytes')->once()->andReturn(false); $encoder = new Swift_Encoder_QpEncoder($charStream); $this->assertEquals($output, $encoder->encodeString($input, 22), '%s: First line should start at offset 22 so can only have max length 54'); }
/** * Takes an unencoded string and produces a QP encoded string from it. * * @param string $string string to encode * @param int $firstLineOffset optional * @param int $maxLineLength optional, 0 indicates the default of 76 chars * * @return string */ public function encodeString($string, $firstLineOffset = 0, $maxLineLength = 0) { return str_replace(array(' ', '=20', "=\r\n"), array('_', '_', "\r\n"), parent::encodeString($string, $firstLineOffset, $maxLineLength)); }
public function testFirstLineLengthCanBeDifferent() { $input = str_repeat('a', 140); $charStream = $this->_createCharStream(); $seq = $this->_mockery()->sequence('byte-sequence'); $exps = Expectations::create(); $exps->one($charStream)->flushContents(); $exps->one($charStream)->importString($input); $output = ''; for ($i = 0; $i < 140; ++$i) { $exps->one($charStream)->readBytes(optional())->inSequence($seq)->returns(array(ord('a'))); if (53 == $i || 53 + 75 == $i) { $output .= "=\r\n"; } $output .= 'a'; } $exps->one($charStream)->readBytes(optional())->inSequence($seq)->returns(false); $this->_checking($exps); $encoder = new Swift_Encoder_QpEncoder($charStream); $this->assertEqual($output, $encoder->encodeString($input, 22), '%s: First line should start at offset 22 so can only have max length 54'); }