-
-
Notifications
You must be signed in to change notification settings - Fork 160
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #147 from linniksa/patch-mime-decoder
Provide mime text decoder by RFC2047
- Loading branch information
Showing
4 changed files
with
105 additions
and
4 deletions.
There are no files selected for viewing
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
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,45 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Fetch package. | ||
* | ||
* (c) Robert Hafner <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Fetch; | ||
|
||
/** | ||
* This library is a wrapper around the Imap library functions included in php. | ||
* | ||
* @package Fetch | ||
* @author Robert Hafner <[email protected]> | ||
* @author Sergey Linnik <[email protected]> | ||
*/ | ||
final class MIME | ||
{ | ||
/** | ||
* @param string $text | ||
* @param string $targetCharset | ||
* | ||
* @return string | ||
*/ | ||
public static function decode($text, $targetCharset = 'utf-8') | ||
{ | ||
if (null === $text) { | ||
return null; | ||
} | ||
|
||
$result = ''; | ||
|
||
foreach (imap_mime_header_decode($text) as $word) { | ||
$ch = 'default' === $word->charset ? 'ascii' : $word->charset; | ||
|
||
$result .= iconv($ch, $targetCharset, $word->text); | ||
} | ||
|
||
return $result; | ||
} | ||
} |
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
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,51 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Fetch library. | ||
* | ||
* (c) Robert Hafner <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Fetch\Test; | ||
|
||
use Fetch\MIME; | ||
|
||
/** | ||
* @package Fetch | ||
* @author Robert Hafner <[email protected]> | ||
* @author Sergey Linnik <[email protected]> | ||
*/ | ||
class MIMETest extends \PHPUnit_Framework_TestCase | ||
{ | ||
public function decodeData() | ||
{ | ||
return array( | ||
array(null, null), | ||
array('Just text', 'Just text'), | ||
array('Keith Moore <[email protected]>', '=?US-ASCII?Q?Keith_Moore?= <[email protected]>'), | ||
array('Keld Jørn Simonsen <[email protected]>', '=?ISO-8859-1?Q?Keld_J=F8rn_Simonsen?= <[email protected]>'), | ||
array('André Pirard <[email protected]>', '=?ISO-8859-1?Q?Andr=E9?= Pirard <[email protected]>'), | ||
array( | ||
'If you can read this you understand the example.', | ||
'=?ISO-8859-1?B?SWYgeW91IGNhbiByZWFkIHRoaXMgeW8=?=' | ||
. PHP_EOL . | ||
'=?ISO-8859-2?B?dSB1bmRlcnN0YW5kIHRoZSBleGFtcGxlLg==?=' | ||
), | ||
); | ||
} | ||
|
||
/** | ||
* @dataProvider decodeData | ||
* | ||
* @param string $expected | ||
* @param string $text | ||
* @param string $charset | ||
*/ | ||
public function testDecode($expected, $text, $charset = 'UTF-8') | ||
{ | ||
self::assertSame($expected, MIME::decode($text, $charset)); | ||
} | ||
} |