This repository has been archived by the owner on Nov 17, 2021. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Native quoted printable content encoder
- Loading branch information
Showing
3 changed files
with
163 additions
and
1 deletion.
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
lib/classes/Swift/Mime/ContentEncoder/NativeQpContentEncoder.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
|
||
class Swift_Mime_ContentEncoder_NativeQpContentEncoder implements Swift_Mime_ContentEncoder | ||
{ | ||
/** | ||
* Notify this observer that the entity's charset has changed. | ||
* @param string $charset | ||
*/ | ||
public function charsetChanged($charset) | ||
{ | ||
if ($charset !== 'utf-8') { | ||
throw new RuntimeException( | ||
sprintf('Charset "%s" not supported. NativeQpContentEncoder only supports "utf-8"', $charset)); | ||
} | ||
} | ||
|
||
/** | ||
* Encode $in to $out. | ||
* @param Swift_OutputByteStream $os to read from | ||
* @param Swift_InputByteStream $is to write to | ||
* @param int $firstLineOffset | ||
* @param int $maxLineLength - 0 indicates the default length for this encoding | ||
*/ | ||
public function encodeByteStream( | ||
Swift_OutputByteStream $os, Swift_InputByteStream $is, $firstLineOffset = 0, | ||
$maxLineLength = 0) | ||
{ | ||
$string = ''; | ||
|
||
while (false !== $bytes = $os->read(8192)) { | ||
$string .= $bytes; | ||
} | ||
|
||
$is->write($this->encodeString($string)); | ||
} | ||
|
||
/** | ||
* Get the MIME name of this content encoding scheme. | ||
* @return string | ||
*/ | ||
public function getName() | ||
{ | ||
return 'quoted-printable'; | ||
} | ||
|
||
/** | ||
* Encode a given string to produce an encoded string. | ||
* @param string $string | ||
* @param int $firstLineOffset if first line needs to be shorter | ||
* @param int $maxLineLength - 0 indicates the default length for this encoding | ||
* @return string | ||
*/ | ||
public function encodeString($string, $firstLineOffset = 0, | ||
$maxLineLength = 0) | ||
{ | ||
return quoted_printable_encode($string); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
tests/acceptance/Swift/Mime/ContentEncoder/NativeQpContentEncoderAcceptanceTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
<?php | ||
|
||
require_once 'Swift/Tests/SwiftUnitTestCase.php'; | ||
require_once 'Swift/Mime/ContentEncoder/NativeQpContentEncoder.php'; | ||
require_once 'Swift/CharacterStream/ArrayCharacterStream.php'; | ||
require_once 'Swift/CharacterReaderFactory/SimpleCharacterReaderFactory.php'; | ||
require_once 'Swift/ByteStream/ArrayByteStream.php'; | ||
|
||
class Swift_Mime_ContentEncoder_NativeQpContentEncoderAcceptanceTest | ||
extends Swift_Tests_SwiftUnitTestCase | ||
{ | ||
/** | ||
* @var Swift_Mime_ContentEncoder_NativeQpContentEncoder | ||
*/ | ||
protected $_encoder; | ||
|
||
public function setUp() | ||
{ | ||
$this->_samplesDir = realpath(dirname(__FILE__) . '/../../../../_samples/charsets'); | ||
$this->_encoder = new Swift_Mime_ContentEncoder_NativeQpContentEncoder(); | ||
} | ||
|
||
public function testEncodingAndDecodingSamples() | ||
{ | ||
$sampleFp = opendir($this->_samplesDir); | ||
while (false !== $encodingDir = readdir($sampleFp)) | ||
{ | ||
if (substr($encodingDir, 0, 1) == '.') | ||
{ | ||
continue; | ||
} | ||
|
||
$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); | ||
|
||
$os = new Swift_ByteStream_ArrayByteStream(); | ||
$os->write($text); | ||
|
||
$is = new Swift_ByteStream_ArrayByteStream(); | ||
$this->_encoder->encodeByteStream($os, $is); | ||
|
||
$encoded = ''; | ||
while (false !== $bytes = $is->read(8192)) | ||
{ | ||
$encoded .= $bytes; | ||
} | ||
|
||
$this->assertEqual( | ||
quoted_printable_decode($encoded), $text, | ||
'%s: Encoded string should decode back to original string for sample ' . | ||
$sampleDir . '/' . $sampleFile | ||
); | ||
} | ||
closedir($fileFp); | ||
} | ||
|
||
} | ||
closedir($sampleFp); | ||
|
||
} | ||
|
||
public function testEncodingAndDecodingSamplesFromDiConfiguredInstance() | ||
{ | ||
$encoder = $this->_createEncoderFromContainer(); | ||
$this->assertSame('=C3=A4=C3=B6=C3=BC=C3=9F', $encoder->encodeString('äöüß')); | ||
} | ||
|
||
public function testCharsetChangeNotImplemented() | ||
{ | ||
$this->_encoder->charsetChanged('utf-8'); | ||
$this->expectException(new RuntimeException('Charset "charset" not supported. NativeQpContentEncoder only supports "utf-8"')); | ||
$this->_encoder->charsetChanged('charset'); | ||
} | ||
|
||
public function testGetName() | ||
{ | ||
$this->assertSame('quoted-printable', $this->_encoder->getName()); | ||
} | ||
|
||
// -- Private Methods | ||
|
||
private function _createEncoderFromContainer() | ||
{ | ||
return Swift_DependencyContainer::getInstance() | ||
->lookup('mime.nativeqpcontentencoder') | ||
; | ||
} | ||
|
||
} |