Completed
Push — id3-metadata-objects ( 168cf3...e37014 )
by Daniel
08:47
created

TextFrame   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
c 1
b 0
f 0
lcom 1
cbo 2
dl 0
loc 107
ccs 0
cts 27
cp 0
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A readEncoding() 0 6 1
A getEncoding() 0 8 2
A readTexts() 0 12 2
A getText() 0 12 3
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\Metadata\Frame;
9
10
use GravityMedia\Metadata\ID3v2\Encoding;
11
use GravityMedia\Metadata\ID3v2\Metadata\StreamContainer;
12
use GravityMedia\Stream\Stream;
13
14
/**
15
 * ID3v2 text frame class.
16
 *
17
 * @package GravityMedia\Metadata\ID3v2\Metadata\Frame
18
 */
19
class TextFrame extends StreamContainer
20
{
21
    /**
22
     * String representations of encodings.
23
     *
24
     * @var string[]
25
     */
26
    protected static $encodings = [
27
        Encoding::ISO_8859_1 => 'ISO-8859-1',
28
        Encoding::UTF_8 => 'UTF-8',
29
        Encoding::UTF_16 => 'UTF-16',
30
        Encoding::UTF_16BE => 'UTF-16BE',
31
        Encoding::UTF_16LE => 'UTF-16LE'
32
    ];
33
34
    /**
35
     * @var int
36
     */
37
    private $textOffset;
38
39
    /**
40
     * @var int
41
     */
42
    private $encoding;
43
44
    /**
45
     * @var string[]
46
     */
47
    private $texts;
48
49
    /**
50
     * Create ID3v2 text frame object.
51
     *
52
     * @param Stream $stream
53
     * @param int    $textOffset
54
     */
55
    public function __construct(Stream $stream, $textOffset = 1)
56
    {
57
        parent::__construct($stream);
58
59
        $this->textOffset = $textOffset;
60
    }
61
62
    /**
63
     * Read encoding.
64
     *
65
     * @return int
66
     */
67
    protected function readEncoding()
68
    {
69
        $this->getStream()->seek($this->getOffset());
70
71
        return $this->getStream()->readUInt8();
72
    }
73
74
    /**
75
     * Get encoding.
76
     *
77
     * @return int
78
     */
79
    public function getEncoding()
80
    {
81
        if (null === $this->encoding) {
82
            $this->encoding = $this->readEncoding();
83
        }
84
85
        return $this->encoding;
86
    }
87
88
    /**
89
     * Read texts.
90
     *
91
     * @return string
92
     */
93
    protected function readTexts()
94
    {
95
        $this->getStream()->seek($this->getOffset() + $this->textOffset);
96
        $text = $this->getStream()->read($this->getStream()->getSize() - $this->textOffset);
97
98
        $encoding = $this->getEncoding();
99
        if (isset(static::$encodings[$encoding])) {
100
            $text = iconv(static::$encodings[$encoding], static::$encodings[Encoding::UTF_8], $text);
101
        }
102
103
        return explode("\x00", $text);
104
    }
105
106
    /**
107
     * Get text.
108
     *
109
     * @param int $index
110
     *
111
     * @return null|string
112
     */
113
    public function getText($index = 0)
114
    {
115
        if (null === $this->texts) {
116
            $this->texts = $this->readTexts();
117
        }
118
119
        if (!isset($this->texts[$index])) {
120
            return null;
121
        }
122
123
        return $this->texts[$index];
124
    }
125
}
126