1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* (c) Christian Gripp <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Core23\LastFm\Model; |
13
|
|
|
|
14
|
|
|
final class SongInfo |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
private $name; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var string|null |
23
|
|
|
*/ |
24
|
|
|
private $mbid; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var int|null |
28
|
|
|
*/ |
29
|
|
|
private $duration; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var Artist|null |
33
|
|
|
*/ |
34
|
|
|
private $artist; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var int |
38
|
|
|
*/ |
39
|
|
|
private $listeners; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var int |
43
|
|
|
*/ |
44
|
|
|
private $playcount; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var Tag[] |
48
|
|
|
*/ |
49
|
|
|
private $topTags; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param Tag[] $topTags |
53
|
|
|
*/ |
54
|
|
|
public function __construct( |
55
|
|
|
string $name, |
56
|
|
|
?string $mbid, |
57
|
|
|
?int $duration, |
58
|
|
|
?Artist $artist, |
59
|
|
|
int $listeners, |
60
|
|
|
int $playcount, |
61
|
|
|
array $topTags |
62
|
|
|
) { |
63
|
|
|
$this->name = $name; |
64
|
|
|
$this->mbid = $mbid; |
65
|
|
|
$this->duration = $duration; |
66
|
|
|
$this->artist = $artist; |
67
|
|
|
$this->listeners = $listeners; |
68
|
|
|
$this->playcount = $playcount; |
69
|
|
|
$this->topTags = $topTags; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function getName(): string |
73
|
|
|
{ |
74
|
|
|
return $this->name; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function getMbid(): ?string |
78
|
|
|
{ |
79
|
|
|
return $this->mbid; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function getDuration(): ?int |
83
|
|
|
{ |
84
|
|
|
return $this->duration; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function getArtist(): ?Artist |
88
|
|
|
{ |
89
|
|
|
return $this->artist; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function getListeners(): int |
93
|
|
|
{ |
94
|
|
|
return $this->listeners; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function getPlaycount(): int |
98
|
|
|
{ |
99
|
|
|
return $this->playcount; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @return Tag[] |
104
|
|
|
*/ |
105
|
|
|
public function getTopTags(): array |
106
|
|
|
{ |
107
|
|
|
return $this->topTags; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @return SongInfo |
112
|
|
|
*/ |
113
|
|
|
public static function fromApi(array $data): self |
114
|
|
|
{ |
115
|
|
|
$tags = self::createTagsFromApi($data); |
116
|
|
|
|
117
|
|
|
$artist = null; |
118
|
|
|
|
119
|
|
|
if (isset($data['artist'])) { |
120
|
|
|
if (\is_array($data['artist'])) { |
121
|
|
|
$artist = Artist::fromApi($data['artist']); |
122
|
|
|
} else { |
123
|
|
|
$artist = new Artist($data['artist'], null, [], null); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return new self( |
128
|
|
|
$data['name'], |
129
|
|
|
$data['mbid'] ?? null, |
130
|
|
|
isset($data['duration']) ? (int) $data['duration'] : null, |
131
|
|
|
$artist, |
132
|
|
|
isset($data['listeners']) ? (int) $data['listeners'] : 0, |
133
|
|
|
isset($data['playcount']) ? (int) $data['playcount'] : 0, |
134
|
|
|
$tags |
135
|
|
|
); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
private static function createTagsFromApi(array $data): array |
139
|
|
|
{ |
140
|
|
|
$tags = []; |
141
|
|
|
|
142
|
|
|
if (isset($data['toptags']['tag'])) { |
143
|
|
|
foreach ((array) $data['toptags']['tag'] as $tag) { |
144
|
|
|
$tags[] = Tag::fromApi($tag); |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
return $tags; |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|