Completed
Push — id3-metadata-objects ( 491068...1cf97b )
by Daniel
09:08
created

CharsetFilter::encode()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 8
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 8
loc 8
ccs 0
cts 4
cp 0
rs 9.4285
cc 2
eloc 4
nc 2
nop 2
crap 6
1
<?php
2
/**
3
 * This file is part of the Metadata package.
4
 *
5
 * @author Daniel Schröder <[email protected]>
6
 */
7
8
namespace GravityMedia\Metadata\ID3v2\Filter;
9
10
use GravityMedia\Metadata\ID3v2\Encoding;
11
12
/**
13
 * Charset filter class.
14
 *
15
 * @package GravityMedia\Metadata\ID3v2\Filter
16
 */
17
class CharsetFilter
18
{
19
    /**
20
     * String representations of encodings.
21
     *
22
     * @var string[]
23
     */
24
    protected static $encodings = [
25
        Encoding::ISO_8859_1 => 'ISO-8859-1',
26
        Encoding::UTF_8 => 'UTF-8',
27
        Encoding::UTF_16 => 'UTF-16',
28
        Encoding::UTF_16BE => 'UTF-16BE',
29
        Encoding::UTF_16LE => 'UTF-16LE'
30
    ];
31
32
    /**
33
     * The internal encoding.
34
     *
35
     * @var int
36
     */
37
    private $encoding;
38
39
    /**
40
     * Create charset filter object.
41
     *
42
     * @param int $encoding
43
     */
44
    public function __construct($encoding = Encoding::UTF_8)
45
    {
46
        $this->encoding = $encoding;
47
    }
48
49
    /**
50
     * Encode string.
51
     *
52
     * @param string $string
53
     * @param int    $encoding
54
     *
55
     * @return string
56
     */
57 View Code Duplication
    public function encode($string, $encoding = Encoding::UTF_8)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
58
    {
59
        if ($encoding === $this->encoding) {
60
            return $string;
61
        }
62
63
        return iconv(static::$encodings[$this->encoding], static::$encodings[$encoding], $string);
64
    }
65
66
    /**
67
     * Decode string.
68
     *
69
     * @param string $string
70
     * @param int    $encoding
71
     *
72
     * @return string
73
     */
74 View Code Duplication
    public function decode($string, $encoding = Encoding::UTF_8)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
75
    {
76
        if ($encoding === $this->encoding) {
77
            return $string;
78
        }
79
80
        return iconv(static::$encodings[$encoding], static::$encodings[$this->encoding], $string);
81
    }
82
}
83