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\Reader; |
9
|
|
|
|
10
|
|
|
use GravityMedia\Metadata\ID3v2\Filter\CharsetFilter; |
11
|
|
|
use GravityMedia\Metadata\ID3v2\StreamContainer; |
12
|
|
|
use GravityMedia\Stream\Stream; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* ID3v2 language text frame reader class. |
16
|
|
|
* |
17
|
|
|
* @package GravityMedia\Metadata\ID3v2\Reader |
18
|
|
|
*/ |
19
|
|
|
class LanguageTextFrameReader extends StreamContainer |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var CharsetFilter |
23
|
|
|
*/ |
24
|
|
|
private $charsetFilter; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var int |
28
|
|
|
*/ |
29
|
|
|
private $encoding; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
private $language; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var string[] |
38
|
|
|
*/ |
39
|
|
|
private $text; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* {@inheritdoc} |
43
|
|
|
*/ |
44
|
|
|
public function __construct(Stream $stream) |
45
|
|
|
{ |
46
|
|
|
parent::__construct($stream); |
47
|
|
|
|
48
|
|
|
$this->charsetFilter = new CharsetFilter(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Read encoding. |
53
|
|
|
* |
54
|
|
|
* @return int |
55
|
|
|
*/ |
56
|
|
|
protected function readEncoding() |
57
|
|
|
{ |
58
|
|
|
$this->getStream()->seek($this->getOffset()); |
59
|
|
|
|
60
|
|
|
return $this->getStream()->readUInt8(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Get encoding. |
65
|
|
|
* |
66
|
|
|
* @return int |
67
|
|
|
*/ |
68
|
|
|
public function getEncoding() |
69
|
|
|
{ |
70
|
|
|
if (null === $this->encoding) { |
71
|
|
|
$this->encoding = $this->readEncoding(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return $this->encoding; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Read language. |
79
|
|
|
* |
80
|
|
|
* @return string |
81
|
|
|
*/ |
82
|
|
|
public function readLanguage() |
83
|
|
|
{ |
84
|
|
|
$this->getStream()->seek($this->getOffset() + 1); |
85
|
|
|
$language = strtoupper(trim($this->getStream()->read(3), "\x00")); |
86
|
|
|
|
87
|
|
|
if ('XXX' === $language || strlen($language) !== 3) { |
88
|
|
|
$language = 'UND'; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return $language; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Get language. |
96
|
|
|
* |
97
|
|
|
* @return string |
98
|
|
|
*/ |
99
|
|
|
public function getLanguage() |
100
|
|
|
{ |
101
|
|
|
if (null === $this->language) { |
102
|
|
|
$this->language = $this->readLanguage(); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return $this->language; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Read text. |
110
|
|
|
* |
111
|
|
|
* @return string[] |
112
|
|
|
*/ |
113
|
|
View Code Duplication |
protected function readText() |
|
|
|
|
114
|
|
|
{ |
115
|
|
|
$this->getStream()->seek($this->getOffset() + 4); |
116
|
|
|
$text = $this->getStream()->read($this->getStream()->getSize() - 4); |
117
|
|
|
|
118
|
|
|
return explode("\x00", $this->charsetFilter->decode($text, $this->getEncoding())); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Get text. |
123
|
|
|
* |
124
|
|
|
* @return string[] |
125
|
|
|
*/ |
126
|
|
|
public function getText() |
127
|
|
|
{ |
128
|
|
|
if (null === $this->text) { |
129
|
|
|
$this->text = $this->readText(); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
return $this->text; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
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.