1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace SpotifyApiConnect\Application\SpotifyWebApiPhp; |
5
|
|
|
|
6
|
|
|
use SpotifyApiConnect\Domain\DataTransferObject\PlaylistDataProvider; |
7
|
|
|
use SpotifyApiConnect\Domain\DataTransferObject\PlaylistTracksDataProvider; |
8
|
|
|
use SpotifyWebAPI\SpotifyWebAPI as BaseSpotifyWebAPI; |
9
|
|
|
use SpotifyWebAPI\Request; |
10
|
|
|
use SpotifyWebAPI\SpotifyWebAPIException; |
11
|
|
|
|
12
|
|
|
class SpotifyWebApi implements SpotifyWebApiInterface |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var BaseSpotifyWebAPI |
16
|
|
|
*/ |
17
|
|
|
private $baseSpotifyWebAPI; |
18
|
|
|
|
19
|
|
|
public function __construct() |
20
|
|
|
{ |
21
|
|
|
$this->baseSpotifyWebAPI = new BaseSpotifyWebAPI(); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param string $userId |
26
|
|
|
* @param string $playlistId |
27
|
|
|
* @param array $tracks |
28
|
|
|
* @param array $options |
29
|
|
|
* @return bool |
30
|
|
|
*/ |
31
|
|
|
public function addUserPlaylistTracks(string $userId, string $playlistId, array $tracks, array $options = []) |
32
|
|
|
{ |
33
|
|
|
return $this->baseSpotifyWebAPI->addUserPlaylistTracks($userId, $playlistId, $tracks, $options); |
|
|
|
|
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param string $userId |
38
|
|
|
* @param string $playlistId |
39
|
|
|
* @param array $tracks |
40
|
|
|
* @param string $snapshotId |
41
|
|
|
* @return bool|string |
42
|
|
|
*/ |
43
|
|
|
public function deleteUserPlaylistTracks(string $userId, string $playlistId, array $tracks, string $snapshotId = '') |
44
|
|
|
{ |
45
|
|
|
return $this->baseSpotifyWebAPI->deleteUserPlaylistTracks($userId, $playlistId, $tracks, $snapshotId); |
|
|
|
|
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param string $playlistId |
50
|
|
|
* @param array $options |
51
|
|
|
* @return PlaylistDataProvider |
52
|
|
|
*/ |
53
|
|
|
public function getPlaylist(string $playlistId, array $options = []): PlaylistDataProvider |
54
|
|
|
{ |
55
|
|
|
$this->baseSpotifyWebAPI->setReturnType(Request::RETURN_ASSOC); |
56
|
|
|
$jsonObjectResult = $this->baseSpotifyWebAPI->getPlaylist($playlistId, $options); |
57
|
|
|
$this->baseSpotifyWebAPI->setReturnType(Request::RETURN_OBJECT); |
58
|
|
|
|
59
|
|
|
$playlistDataProvider = new PlaylistDataProvider(); |
60
|
|
|
$playlistDataProvider->fromArray($jsonObjectResult); |
|
|
|
|
61
|
|
|
|
62
|
|
|
return $playlistDataProvider; |
63
|
|
|
|
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param string $userId |
68
|
|
|
* @param array $options |
69
|
|
|
* @return array|object |
70
|
|
|
*/ |
71
|
|
|
public function getUserPlaylists(string $userId, array $options = []) |
72
|
|
|
{ |
73
|
|
|
return $this->baseSpotifyWebAPI->getUserPlaylists($userId, $options); |
|
|
|
|
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param string $playlistId |
78
|
|
|
* @param array $options |
79
|
|
|
* @return PlaylistTracksDataProvider |
80
|
|
|
*/ |
81
|
|
|
public function getPlaylistTracks(string $playlistId, array $options = []) : PlaylistTracksDataProvider |
82
|
|
|
{ |
83
|
|
|
$this->baseSpotifyWebAPI->setReturnType(Request::RETURN_ASSOC); |
84
|
|
|
$jsonObjectResult = $this->baseSpotifyWebAPI->getPlaylistTracks($playlistId, $options); |
85
|
|
|
$this->baseSpotifyWebAPI->setReturnType(Request::RETURN_OBJECT); |
86
|
|
|
|
87
|
|
|
$playlistTracksDataProvider = new PlaylistTracksDataProvider(); |
88
|
|
|
$playlistTracksDataProvider->fromArray($jsonObjectResult); |
|
|
|
|
89
|
|
|
|
90
|
|
|
return $playlistTracksDataProvider; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param string $query |
95
|
|
|
* @param array $type |
96
|
|
|
* @param array $options |
97
|
|
|
* @return array|object |
98
|
|
|
*/ |
99
|
|
|
public function search(string $query, array $type, array $options = []) |
100
|
|
|
{ |
101
|
|
|
return $this->baseSpotifyWebAPI->search($query, $type, $options); |
|
|
|
|
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param string $accessToken |
106
|
|
|
*/ |
107
|
|
|
public function setAccessToken(string $accessToken) |
108
|
|
|
{ |
109
|
|
|
$this->baseSpotifyWebAPI->setAccessToken($accessToken); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
} |