Skip to content

Commit 348d1d1

Browse files
committed
Add error handling to readme
1 parent 6a0f0d3 commit 348d1d1

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,39 @@ use Ddeboer\Transcoder\Transcoder;
8181
$transcoder->transcode('España', null, 'UTF-8');
8282
```
8383

84+
### Error handling
85+
86+
PHP’s `mv_convert_encoding` and `iconv` are inconvenient to use because they
87+
generate notices and warnings instead of proper exceptions. This library fixes
88+
that:
89+
90+
91+
```php
92+
use Ddeboer\Transcoder\Exception\UndetectableEncodingException;
93+
use Ddeboer\Transcoder\Exception\UnsupportedEncodingException;
94+
use Ddeboer\Transcoder\Exception\IllegalCharacterException;
95+
96+
$input = 'España';
97+
98+
try {
99+
$transcoder->transcode($input);
100+
} catch (UndetectableEncodingException $e) {
101+
// Failed to automatically detect $input’s encoding
102+
}
103+
104+
try {
105+
$transcoder->transcode($input, null, 'not-a-real-encoding');
106+
} catch (UnsupportedEncodingException $e) {
107+
// ‘not-a-real-encoding’ is an unsupported encoding
108+
}
109+
110+
try {
111+
$transcoder->transcode('Illegal quotes: ‘ ’', null, 'iso-8859-1');
112+
} catch (IllegalCharacterException $e) {
113+
// Curly quotes ‘ ’ are illegal in ISO-8859-1
114+
}
115+
```
116+
84117
### Transcoder fallback
85118

86119
In general, `mb_convert_encoding` is faster than `iconv`. However, as `iconv`

tests/MbTranscoderTest.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,11 @@ public function testDetectEncoding()
4646
*/
4747
public function testUndetectableEncoding()
4848
{
49-
$result = $this->transcoder->transcode('‘España’', null, 'windows-1252');
49+
$result = $this->transcoder->transcode(
50+
'‘curly quotes make this incompatible with 1252’',
51+
null,
52+
'windows-1252'
53+
);
5054
$this->transcoder->transcode($result);
5155
}
5256

@@ -63,7 +67,7 @@ public function getStrings()
6367
{
6468
return [
6569
['‘España’', 'windows-1252'],
66-
['España', 'iso-8859-1']
70+
['España', 'iso-8859-1']
6771
];
6872
}
6973
}

0 commit comments

Comments
 (0)