Completed
Push — id3-metadata-objects ( 161f4c...a3a88a )
by Daniel
03:11
created

Tag::getFrames()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
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\ID3v1;
9
10
use GravityMedia\Metadata\Exception\BadMethodCallException;
11
use GravityMedia\Metadata\Exception\InvalidArgumentException;
12
use GravityMedia\Metadata\ID3v1\Enum\Genre;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, GravityMedia\Metadata\ID3v1\Genre.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
13
use GravityMedia\Metadata\ID3v1\Enum\Version;
14
15
/**
16
 * ID3v1 tag
17
 *
18
 * @package GravityMedia\Metadata\ID3v1
19
 */
20
class Tag implements TagInterface
21
{
22
    /**
23
     * @var int
24
     */
25
    protected $version;
26
27
    /**
28
     * @var string
29
     */
30
    protected $title;
31
32
    /**
33
     * @var string
34
     */
35
    protected $artist;
36
37
    /**
38
     * @var string
39
     */
40
    protected $album;
41
42
    /**
43
     * @var int
44
     */
45
    protected $year;
46
47
    /**
48
     * @var string
49
     */
50
    protected $comment;
51
52
    /**
53
     * @var int
54
     */
55
    protected $track;
56
57
    /**
58
     * @var int
59
     */
60
    protected $genre;
61
62
    /**
63
     * Create tag object.
64
     *
65
     * @param int $version The version (default is 1: v1.1)
66
     *
67
     * @throws InvalidArgumentException An exception is thrown on invalid version arguments
68
     */
69 View Code Duplication
    public function __construct($version = Version::VERSION_11)
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...
70
    {
71
        if (!in_array($version, Version::values())) {
72
            throw new InvalidArgumentException('Invalid version.');
73
        }
74
75
        $this->version = $version;
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81
    public function getVersion()
82
    {
83
        return $this->version;
84
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89
    public function getTitle()
90
    {
91
        return $this->title;
92
    }
93
94
    /**
95
     * Set title
96
     *
97
     * @param string $title The title
98
     *
99
     * @throws InvalidArgumentException An exception is thrown when the title exceeds 30 characters
100
     *
101
     * @return $this
102
     */
103
    public function setTitle($title)
104
    {
105
        if (strlen($title) > 30) {
106
            throw new InvalidArgumentException('Title argument exceeds maximum number of characters.');
107
        }
108
109
        $this->title = $title;
110
111
        return $this;
112
    }
113
114
    /**
115
     * {@inheritdoc}
116
     */
117
    public function getArtist()
118
    {
119
        return $this->artist;
120
    }
121
122
    /**
123
     * Set artist
124
     *
125
     * @param string $artist The artist
126
     *
127
     * @throws InvalidArgumentException An exception is thrown when the artist exceeds 30 characters
128
     *
129
     * @return $this
130
     */
131
    public function setArtist($artist)
132
    {
133
        if (strlen($artist) > 30) {
134
            throw new InvalidArgumentException('Artist argument exceeds maximum number of characters.');
135
        }
136
137
        $this->artist = $artist;
138
139
        return $this;
140
    }
141
142
    /**
143
     * {@inheritdoc}
144
     */
145
    public function getAlbum()
146
    {
147
        return $this->album;
148
    }
149
150
    /**
151
     * Set album
152
     *
153
     * @param string $album The album
154
     *
155
     * @throws InvalidArgumentException An exception is thrown when the album exceeds 30 characters
156
     *
157
     * @return $this
158
     */
159
    public function setAlbum($album)
160
    {
161
        if (strlen($album) > 30) {
162
            throw new InvalidArgumentException('Album argument exceeds maximum number of characters.');
163
        }
164
165
        $this->album = $album;
166
167
        return $this;
168
    }
169
170
    /**
171
     * {@inheritdoc}
172
     */
173
    public function getYear()
174
    {
175
        return $this->year;
176
    }
177
178
    /**
179
     * Set year
180
     *
181
     * @param int $year The year
182
     *
183
     * @throws InvalidArgumentException An exception is thrown when the year does not have exactly 4 digits
184
     *
185
     * @return $this
186
     */
187
    public function setYear($year)
188
    {
189
        if (preg_match('/^\d{4}$/', $year) < 1) {
190
            throw new InvalidArgumentException('Year argument must have exactly 4 digits.');
191
        }
192
193
        $this->year = $year;
194
195
        return $this;
196
    }
197
198
    /**
199
     * {@inheritdoc}
200
     */
201
    public function getComment()
202
    {
203
        return $this->comment;
204
    }
205
206
    /**
207
     * Set comment
208
     *
209
     * @param string $comment The comment
210
     *
211
     * @throws InvalidArgumentException An exception is thrown when the comment exceeds 28 characters
212
     *                                            (ID3 v1.1) or 30 characters (ID3 v1.0)
213
     *
214
     * @return $this
215
     */
216
    public function setComment($comment)
217
    {
218
        $max = Version::VERSION_11 === $this->version ? 28 : 30;
219
        if (strlen($comment) > $max) {
220
            throw new InvalidArgumentException('Comment argument exceeds maximum number of characters.');
221
        }
222
223
        $this->comment = $comment;
224
225
        return $this;
226
    }
227
228
    /**
229
     * {@inheritdoc}
230
     */
231
    public function getTrack()
232
    {
233
        return $this->track;
234
    }
235
236
    /**
237
     * Set track
238
     *
239
     * @param int $track The track number
240
     *
241
     * @throws BadMethodCallException An exception is thrown on ID3 v1.0 tag
242
     * @throws InvalidArgumentException An exception is thrown when the year does not have exactly 2 digits
243
     *
244
     * @return $this
245
     */
246
    public function setTrack($track)
247
    {
248
        if (Version::VERSION_10 === $this->version) {
249
            throw new BadMethodCallException('Track is not supported in this version.');
250
        }
251
252
        if (preg_match('/^\d{1,2}$/', $track) < 1) {
253
            throw new InvalidArgumentException('Track argument exceeds 2 digits.');
254
        }
255
256
        $this->track = $track;
257
258
        return $this;
259
    }
260
261
    /**
262
     * {@inheritdoc}
263
     */
264
    public function getGenre()
265
    {
266
        return $this->genre;
267
    }
268
269
    /**
270
     * Set genre
271
     *
272
     * @param int $genre
273
     *
274
     * @throws InvalidArgumentException An exception is thrown on invalid genre arguments
275
     *
276
     * @return $this
277
     */
278
    public function setGenre($genre)
279
    {
280
        if (!in_array($genre, Genre::values())) {
281
            throw new InvalidArgumentException('Invalid genre.');
282
        }
283
284
        $this->genre = $genre;
285
286
        return $this;
287
    }
288
}
289