Passed
Push — master ( 362ec5...c42e48 )
by Petr
05:09
created

EncodingTransformer::transform()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 10
c 1
b 0
f 0
cc 2
nc 2
nop 1
crap 2
1
<?php
2
/**
3
 * This file is part of riesenia/pohoda package.
4
 *
5
 * Licensed under the MIT License
6
 * (c) RIESENIA.com
7
 */
8
9
declare(strict_types=1);
10
11
12
namespace Riesenia\Pohoda\ValueTransformer;
13
14
15
/**
16
 * A transformer that converts the encoding of a string.
17
 *
18
 * Since most transformers will expect UTF-8 formatted strings, this transformer should either be the last
19
 * to run or be immediately followed by another one that will convert the string back to UTF-8.
20
 */
21
class EncodingTransformer implements ValueTransformerInterface
22
{
23 7
    public function __construct(
24
        protected readonly string $fromEncoding,
25
        protected readonly string $toEncoding,
26
    )
27
    {
28 7
    }
29
30
    /**
31
     * {@inheritdoc}
32
     */
33 6
    public function transform(string $value): string
34
    {
35 6
        $result = @\iconv($this->fromEncoding, $this->toEncoding, $value);
36
37 6
        if (false === $result) {
38 1
            return $value;
39
        }
40
41 5
        return $result;
42
    }
43
}
44