EncodingTransformer   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 19
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

2 Methods

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