Skip to content

Commit 6a0f0d3

Browse files
committed
Add unsupported encoding test for iconv
1 parent 500eb71 commit 6a0f0d3

File tree

4 files changed

+31
-7
lines changed

4 files changed

+31
-7
lines changed

src/Exception/UnsupportedEncodingException.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,13 @@
44

55
class UnsupportedEncodingException extends \RuntimeException
66
{
7-
public function __construct($encoding)
7+
public function __construct($encoding, $message = null)
88
{
9-
parent::__construct(sprintf('Encoding %s is unsupported on this platform', $encoding));
9+
$error = sprintf('Encoding %s is unsupported on this platform', $encoding);
10+
if ($message) {
11+
$error .= ': ' . $message;
12+
}
13+
14+
return parent::__construct($error);
1015
}
1116
}

src/IconvTranscoder.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Ddeboer\Transcoder\Exception\ExtensionMissingException;
66
use Ddeboer\Transcoder\Exception\IllegalCharacterException;
7+
use Ddeboer\Transcoder\Exception\UnsupportedEncodingException;
78

89
class IconvTranscoder implements TranscoderInterface
910
{
@@ -25,7 +26,11 @@ public function transcode($string, $from = null, $to = null)
2526
{
2627
set_error_handler(
2728
function ($no, $message) use ($string) {
28-
throw new IllegalCharacterException($string, $message);
29+
if (1 === preg_match('/Wrong charset, conversion (.+) is/', $message, $matches)) {
30+
throw new UnsupportedEncodingException($matches[1], $message);
31+
} else {
32+
throw new IllegalCharacterException($string, $message);
33+
}
2934
},
3035
E_NOTICE | E_USER_NOTICE
3136
);

src/Transcoder.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Ddeboer\Transcoder;
44

5+
use Ddeboer\Transcoder\Exception\ExtensionMissingException;
56
use Ddeboer\Transcoder\Exception\UnsupportedEncodingException;
67

78
class Transcoder implements TranscoderInterface
@@ -18,13 +19,16 @@ public function __construct(array $transcoders)
1819
$this->transcoders = $transcoders;
1920
}
2021

22+
/**
23+
* {@inheritdoc}
24+
*/
2125
public function transcode($string, $from = null, $to = null)
2226
{
2327
foreach ($this->transcoders as $transcoder) {
2428
try {
2529
return $transcoder->transcode($string, $from, $to);
2630
} catch (UnsupportedEncodingException $e) {
27-
31+
// Ignore as long as the fallback transcoder is all right
2832
}
2933
}
3034

@@ -51,12 +55,13 @@ public static function create($defaultEncoding = 'UTF-8')
5155
try {
5256
$transcoders[] = new MbTranscoder($defaultEncoding);
5357
} catch (ExtensionMissingException $mb) {
54-
// Ignore missing mbstring extension; fallback to iconv
58+
// Ignore missing mbstring extension; fall back to iconv
5559
}
5660

5761
try {
5862
$transcoders[] = new IconvTranscoder($defaultEncoding);
5963
} catch (ExtensionMissingException $iconv) {
64+
// Neither mbstring nor iconv
6065
throw $iconv;
6166
}
6267

tests/IconvTranscoderTest.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,19 @@ protected function setUp()
1515
{
1616
$this->transcoder = new IconvTranscoder();
1717
}
18-
18+
19+
/**
20+
* @expectedException \Ddeboer\Transcoder\Exception\UnsupportedEncodingException
21+
* @expectedExceptionMessage bad-encoding
22+
*/
23+
public function testTranscodeUnsupportedFromEncoding()
24+
{
25+
$this->transcoder->transcode('bla', 'bad-encoding');
26+
}
27+
1928
public function testDetectEncoding()
2029
{
21-
$this->transcoder->transcode('España', "UTF-8", 'iso-8859-1');
30+
$this->transcoder->transcode('España', null, 'iso-8859-1');
2231
}
2332

2433
/**

0 commit comments

Comments
 (0)