EncodingTransformer::transform()   A
last analyzed

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