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; |
9
|
|
|
|
10
|
|
|
use GravityMedia\Metadata\ID3v2\Filter\SynchsafeIntegerFilter; |
11
|
|
|
use GravityMedia\Metadata\ID3v2\Flag\HeaderFlag; |
12
|
|
|
use GravityMedia\Metadata\ID3v2\Version; |
13
|
|
|
use GravityMedia\Stream\Stream; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* ID3v2 header metadata class. |
17
|
|
|
* |
18
|
|
|
* @package GravityMedia\Metadata\ID3v2\Metadata |
19
|
|
|
*/ |
20
|
|
|
class HeaderMetadata |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var Stream |
24
|
|
|
*/ |
25
|
|
|
protected $stream; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var int |
29
|
|
|
*/ |
30
|
|
|
protected $version; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var SynchsafeIntegerFilter |
34
|
|
|
*/ |
35
|
|
|
protected $synchsafeIntegerFilter; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Create ID3v2 header metadata object. |
39
|
|
|
* |
40
|
|
|
* @param Stream $stream |
41
|
|
|
* @param int $version |
42
|
|
|
*/ |
43
|
|
|
public function __construct(Stream $stream, $version) |
44
|
|
|
{ |
45
|
|
|
$this->stream = $stream; |
46
|
|
|
$this->version = $version; |
47
|
|
|
$this->synchsafeIntegerFilter = new SynchsafeIntegerFilter(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Read ID3v2 header size. |
52
|
|
|
* |
53
|
|
|
* @return int |
54
|
|
|
*/ |
55
|
|
|
public function readSize() |
56
|
|
|
{ |
57
|
|
|
$this->stream->seek(6); |
58
|
|
|
|
59
|
|
|
return $this->synchsafeIntegerFilter->decode($this->stream->readUInt32()); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Read ID3v2 header flags. |
64
|
|
|
* |
65
|
|
|
* @return array |
66
|
|
|
*/ |
67
|
|
|
public function readFlags() |
68
|
|
|
{ |
69
|
|
|
$this->stream->seek(5); |
70
|
|
|
|
71
|
|
|
$flags = $this->stream->readUInt8(); |
72
|
|
|
|
73
|
|
View Code Duplication |
if (Version::VERSION_22 === $this->version) { |
|
|
|
|
74
|
|
|
return [ |
75
|
|
|
HeaderFlag::FLAG_UNSYNCHRONISATION => (bool)($flags & 0x80), |
76
|
|
|
HeaderFlag::FLAG_COMPRESSION => (bool)($flags & 0x40) |
77
|
|
|
]; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
View Code Duplication |
if (Version::VERSION_23 === $this->version) { |
|
|
|
|
81
|
|
|
return [ |
82
|
|
|
HeaderFlag::FLAG_UNSYNCHRONISATION => (bool)($flags & 0x80), |
83
|
|
|
HeaderFlag::FLAG_EXTENDED_HEADER => (bool)($flags & 0x40), |
84
|
|
|
HeaderFlag::FLAG_EXPERIMENTAL_INDICATOR => (bool)($flags & 0x20) |
85
|
|
|
]; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return [ |
89
|
|
|
HeaderFlag::FLAG_UNSYNCHRONISATION => (bool)($flags & 0x80), |
90
|
|
|
HeaderFlag::FLAG_EXTENDED_HEADER => (bool)($flags & 0x40), |
91
|
|
|
HeaderFlag::FLAG_EXPERIMENTAL_INDICATOR => (bool)($flags & 0x20), |
92
|
|
|
HeaderFlag::FLAG_FOOTER_PRESENT => (bool)($flags & 0x10) |
93
|
|
|
]; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
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.