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