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

EncodingTransformer   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 21
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
eloc 5
dl 0
loc 21
ccs 7
cts 7
cp 1
rs 10
c 1
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A transform() 0 9 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