CharsetValueConverter::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
ccs 4
cts 4
cp 1
cc 1
eloc 3
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Ddeboer\DataImport\ValueConverter;
4
5
use Ddeboer\DataImport\Exception\UnexpectedTypeException;
6
7
/**
8
 * Convert a value in a specific charset
9
 *
10
 * @author Markus Bachmann <[email protected]>
11
 */
12
class CharsetValueConverter
13
{
14
    /**
15
     * @var string
16
     */
17
    private $charset;
18
19
    /**
20
     * @var string
21
     */
22
    private $inCharset;
23
24
    /**
25
     * @param string $charset   Charset to convert values to
26
     * @param string $inCharset Charset of input values
27
     */
28 1
    public function __construct($charset, $inCharset = 'UTF-8')
29
    {
30 1
        $this->charset = $charset;
31 1
        $this->inCharset = $inCharset;
32 1
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37 1
    public function __invoke($input)
38
    {
39 1
        if (function_exists('mb_convert_encoding')) {
40 1
            return mb_convert_encoding($input, $this->charset, $this->inCharset);
41
        }
42
43
        if (function_exists('iconv')) {
44
            return iconv($this->inCharset, $this->charset, $input);
45
        }
46
47
        throw new \RuntimeException('Could not convert the charset. Please install the mbstring or iconv extension!');
48
    }
49
}
50