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 ArtistInfo |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
private $name; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var string|null |
23
|
|
|
*/ |
24
|
|
|
private $mbid; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var Image[] |
28
|
|
|
*/ |
29
|
|
|
private $image; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var string|null |
33
|
|
|
*/ |
34
|
|
|
private $url; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var int |
38
|
|
|
*/ |
39
|
|
|
private $playcount; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var string|null |
43
|
|
|
*/ |
44
|
|
|
private $bioSummary; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var string|null |
48
|
|
|
*/ |
49
|
|
|
private $bioContent; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var int |
53
|
|
|
*/ |
54
|
|
|
private $tagcount; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @var Tag[] |
58
|
|
|
*/ |
59
|
|
|
private $tags; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param string $name |
63
|
|
|
* @param string|null $mbid |
64
|
|
|
* @param Image[] $image |
65
|
|
|
* @param string|null $url |
66
|
|
|
* @param int $playcount |
67
|
|
|
* @param string|null $bioSummary |
68
|
|
|
* @param string|null $bioContent |
69
|
|
|
* @param int $tagcount |
70
|
|
|
* @param Tag[] $tags |
71
|
|
|
*/ |
72
|
|
|
public function __construct( |
73
|
|
|
string $name, |
74
|
|
|
?string $mbid, |
75
|
|
|
array $image, |
76
|
|
|
?string $url, |
77
|
|
|
int $playcount, |
78
|
|
|
?string $bioSummary, |
79
|
|
|
?string $bioContent, |
80
|
|
|
int $tagcount, |
81
|
|
|
array $tags |
82
|
|
|
) { |
83
|
|
|
$this->name = $name; |
84
|
|
|
$this->mbid = $mbid; |
85
|
|
|
$this->image = $image; |
86
|
|
|
$this->url = $url; |
87
|
|
|
$this->playcount = $playcount; |
88
|
|
|
$this->bioSummary = $bioSummary; |
89
|
|
|
$this->bioContent = $bioContent; |
90
|
|
|
$this->tagcount = $tagcount; |
91
|
|
|
$this->tags = $tags; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @return string |
96
|
|
|
*/ |
97
|
|
|
public function getName(): string |
98
|
|
|
{ |
99
|
|
|
return $this->name; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @return string|null |
104
|
|
|
*/ |
105
|
|
|
public function getMbid(): ?string |
106
|
|
|
{ |
107
|
|
|
return $this->mbid; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @return Image[] |
112
|
|
|
*/ |
113
|
|
|
public function getImage(): array |
114
|
|
|
{ |
115
|
|
|
return $this->image; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @return string|null |
120
|
|
|
*/ |
121
|
|
|
public function getUrl(): ?string |
122
|
|
|
{ |
123
|
|
|
return $this->url; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @return int |
128
|
|
|
*/ |
129
|
|
|
public function getPlaycount(): int |
130
|
|
|
{ |
131
|
|
|
return $this->playcount; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @return string|null |
136
|
|
|
*/ |
137
|
|
|
public function getBioSummary(): ?string |
138
|
|
|
{ |
139
|
|
|
return $this->bioSummary; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @return string|null |
144
|
|
|
*/ |
145
|
|
|
public function getBioContent(): ?string |
146
|
|
|
{ |
147
|
|
|
return $this->bioContent; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @return int |
152
|
|
|
*/ |
153
|
|
|
public function getTagcount(): int |
154
|
|
|
{ |
155
|
|
|
return $this->tagcount; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @return Tag[] |
160
|
|
|
*/ |
161
|
|
|
public function getTags(): array |
162
|
|
|
{ |
163
|
|
|
return $this->tags; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @param array $data |
168
|
|
|
* |
169
|
|
|
* @return ArtistInfo |
170
|
|
|
*/ |
171
|
|
|
public static function fromApi(array $data): self |
172
|
|
|
{ |
173
|
|
|
$images = self::createImagesFromApi($data); |
174
|
|
|
$tags = self::createTagsFromApi($data); |
175
|
|
|
|
176
|
|
|
return new self( |
177
|
|
|
$data['name'], |
178
|
|
|
$data['mbid'] ?? null, |
179
|
|
|
$images, |
180
|
|
|
$data['url'] ?? null, |
181
|
|
|
isset($data['playcount']) ? (int) $data['playcount'] : 0, |
182
|
|
|
$data['bio']['summary'] ?? null, |
183
|
|
|
$data['bio']['content'] ?? null, |
184
|
|
|
isset($data['tagcount']) ? (int) $data['tagcount'] : 0, |
185
|
|
|
$tags |
186
|
|
|
); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* @param array $data |
191
|
|
|
* |
192
|
|
|
* @return array |
193
|
|
|
*/ |
194
|
|
|
private static function createImagesFromApi(array $data): array |
195
|
|
|
{ |
196
|
|
|
$images = []; |
197
|
|
|
|
198
|
|
|
if (\array_key_exists('image', $data)) { |
199
|
|
|
foreach ((array) $data['image'] as $image) { |
200
|
|
|
$images[] = new Image($image['#text']); |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
return $images; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* @param array $data |
209
|
|
|
* |
210
|
|
|
* @return array |
211
|
|
|
*/ |
212
|
|
|
private static function createTagsFromApi(array $data): array |
213
|
|
|
{ |
214
|
|
|
$tags = []; |
215
|
|
|
|
216
|
|
|
if (\array_key_exists('tags', $data) && \array_key_exists('tag', $data['tags'])) { |
217
|
|
|
foreach ((array) $data['tags']['tag'] as $tag) { |
218
|
|
|
$tags[] = Tag::fromApi($tag); |
219
|
|
|
} |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
return $tags; |
223
|
|
|
} |
224
|
|
|
} |
225
|
|
|
|