Skip to content

Commit

Permalink
Rework on include options, create an array and only include when data…
Browse files Browse the repository at this point in the history
… is available
  • Loading branch information
PouleR committed Oct 19, 2021
1 parent 62ce6bb commit 3e5bbfb
Showing 1 changed file with 114 additions and 59 deletions.
173 changes: 114 additions & 59 deletions src/AppleMusicAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,15 @@ public function setMusicUserToken($musicUserToken)
* https://developer.apple.com/documentation/applemusicapi/get_a_storefront
*
* @param string $id The identifier (an ISO 3166 alpha-2 country code) for the storefront you want to fetch.
* @param string $include A comma separated list of additional relationships to include in the fetch.
* @param array $include A list of additional relationships to include in the fetch.
*
* @return array|object
*
* @throws AppleMusicAPIException
*/
public function getStorefront(string $id, string $include = '')
public function getStorefront(string $id, array $include = [])
{
$requestUrl = sprintf('storefronts/%s?include=%s', $id, $include);
$requestUrl = sprintf('storefronts/%s?include=%s', $id, implode(',', $include));

return $this->client->apiRequest('GET', $requestUrl);
}
Expand All @@ -78,15 +78,15 @@ public function getStorefront(string $id, string $include = '')
* Fetch all the storefronts in alphabetical order.
* https://developer.apple.com/documentation/applemusicapi/get_all_storefronts
*
* @param string $include A comma separated list of additional relationships to include in the fetch.
* @param array $include A list of additional relationships to include in the fetch.
*
* @return array|object
*
* @throws AppleMusicAPIException
*/
public function getAllStorefronts(string $include = '')
public function getAllStorefronts(array $include = [])
{
$requestUrl = sprintf('storefronts?include=%s', $include);
$requestUrl = sprintf('storefronts?include=%s', implode(',', $include));

return $this->client->apiRequest('GET', $requestUrl);
}
Expand Down Expand Up @@ -129,15 +129,19 @@ public function getCatalogCharts($storefront, $types = [], $genre = '', $limit =
*
* @param string $storefront An iTunes Store territory, specified by an ISO 3166 alpha-2 country code.
* @param string $playlistId The unique identifier for the playlist.
* @param string $include A comma separated list of additional relationships to include in the fetch.
* @param array $include A list of additional relationships to include in the fetch.
*
* @return array|object
*
* @throws AppleMusicAPIException
*/
public function getCatalogPlaylist(string $storefront, string $playlistId, string $include = '')
public function getCatalogPlaylist(string $storefront, string $playlistId, array $include = [])
{
$requestUrl = sprintf('catalog/%s/playlists/%s?include=%s', $storefront, $playlistId, $include);
$requestUrl = sprintf('catalog/%s/playlists/%s', $storefront, $playlistId);

if (count($include)) {
$requestUrl .= sprintf('?include=%s', implode(',', $include));
}

return $this->client->apiRequest('GET', $requestUrl);
}
Expand All @@ -148,15 +152,19 @@ public function getCatalogPlaylist(string $storefront, string $playlistId, strin
*
* @param string $storefront An iTunes Store territory, specified by an ISO 3166 alpha-2 country code.
* @param string $albumId The unique identifier for the album.
* @param string $include A comma separated list of additional relationships to include in the fetch.
* @param array $include A list of additional relationships to include in the fetch.
*
* @return array|object
*
* @throws AppleMusicAPIException
*/
public function getCatalogAlbum(string $storefront, string $albumId, string $include = '')
public function getCatalogAlbum(string $storefront, string $albumId, array $include = [])
{
$requestUrl = sprintf('catalog/%s/albums/%s?include=%s', $storefront, $albumId, $include);
$requestUrl = sprintf('catalog/%s/albums/%s', $storefront, $albumId);

if (count($include)) {
$requestUrl .= sprintf('?include=%s', implode(',', $include));
}

return $this->client->apiRequest('GET', $requestUrl);
}
Expand All @@ -167,15 +175,19 @@ public function getCatalogAlbum(string $storefront, string $albumId, string $inc
*
* @param string $storefront An iTunes Store territory, specified by an ISO 3166 alpha-2 country code.
* @param string $upc An Universal Product Code.
* @param string $include A comma separated list of additional relationships to include in the fetch.
* @param array $include A list of additional relationships to include in the fetch.
*
* @return array|object
*
* @throws AppleMusicAPIException
*/
public function getMultipleCatalogAlbumsByUpc(string $storefront, string $upc, string $include = '')
public function getMultipleCatalogAlbumsByUpc(string $storefront, string $upc, array $include = [])
{
$requestUrl = sprintf('catalog/%s/albums?filter[upc]=%s&include=%s', $storefront, $upc, $include);
$requestUrl = sprintf('catalog/%s/albums?filter[upc]=%s', $storefront, $upc);

if (count($include)) {
$requestUrl .= sprintf('&include=%s', implode(',', $include));
}

return $this->client->apiRequest('GET', $requestUrl);
}
Expand All @@ -186,15 +198,19 @@ public function getMultipleCatalogAlbumsByUpc(string $storefront, string $upc, s
*
* @param string $storefront An iTunes Store territory, specified by an ISO 3166 alpha-2 country code.
* @param string $songId The unique identifier for the song.
* @param string $include A comma separated list of additional relationships to include in the fetch.
* @param array $include A list of additional relationships to include in the fetch.
*
* @return array|object
*
* @throws AppleMusicAPIException
*/
public function getCatalogSong(string $storefront, string $songId, string $include = '')
public function getCatalogSong(string $storefront, string $songId, array $include = [])
{
$requestUrl = sprintf('catalog/%s/songs/%s?include=%s', $storefront, $songId, $include);
$requestUrl = sprintf('catalog/%s/songs/%s', $storefront, $songId);

if (count($include)) {
$requestUrl .= sprintf('?include=%s', implode(',', $include));
}

return $this->client->apiRequest('GET', $requestUrl);
}
Expand All @@ -205,15 +221,19 @@ public function getCatalogSong(string $storefront, string $songId, string $inclu
*
* @param string $storefront An iTunes Store territory, specified by an ISO 3166 alpha-2 country code.
* @param string $isrc An unique International Standard Recording Code.
* @param string $include A comma separated list of additional relationships to include in the fetch.
* @param array $include A list of additional relationships to include in the fetch.
*
* @return array|object
*
* @throws AppleMusicAPIException
*/
public function getMultipleCatalogSongsByIsrc(string $storefront, string $isrc, string $include = '')
public function getMultipleCatalogSongsByIsrc(string $storefront, string $isrc, array $include = [])
{
$requestUrl = sprintf('catalog/%s/songs?filter[isrc]=%s&include=%s', $storefront, $isrc, $include);
$requestUrl = sprintf('catalog/%s/songs?filter[isrc]=%s', $storefront, $isrc);

if (count($include)) {
$requestUrl .= sprintf('&include=%s', implode(',', $include));
}

return $this->client->apiRequest('GET', $requestUrl);
}
Expand All @@ -224,15 +244,19 @@ public function getMultipleCatalogSongsByIsrc(string $storefront, string $isrc,
*
* @param string $storefront An iTunes Store territory, specified by an ISO 3166 alpha-2 country code.
* @param string $artistId The unique identifier for the artist.
* @param string $include A comma separated list of additional relationships to include in the fetch.
* @param array $include A list of additional relationships to include in the fetch.
*
* @return array|object
*
* @throws AppleMusicAPIException
*/
public function getCatalogArtist(string $storefront, string $artistId, string $include = '')
public function getCatalogArtist(string $storefront, string $artistId, array $include = [])
{
$requestUrl = sprintf('catalog/%s/artists/%s?include=%s', $storefront, $artistId, $include);
$requestUrl = sprintf('catalog/%s/artists/%s', $storefront, $artistId);

if (count($include)) {
$requestUrl .= sprintf('?include=%s', implode(',', $include));
}

return $this->client->apiRequest('GET', $requestUrl);
}
Expand All @@ -243,15 +267,19 @@ public function getCatalogArtist(string $storefront, string $artistId, string $i
*
* @param string $storefront An iTunes Store territory, specified by an ISO 3166 alpha-2 country code.
* @param string $curatorId The unique identifier for the curator.
* @param string $include A comma separated list of additional relationships to include in the fetch.
* @param array $include A list of additional relationships to include in the fetch.
*
* @return array|object
*
* @throws AppleMusicAPIException
*/
public function getCatalogCurator(string $storefront, string $curatorId, string $include)
public function getCatalogCurator(string $storefront, string $curatorId, array $include = [])
{
$requestUrl = sprintf('catalog/%s/curators/%s?include=%s', $storefront, $curatorId, $include);
$requestUrl = sprintf('catalog/%s/curators/%s', $storefront, $curatorId);

if (count($include)) {
$requestUrl .= sprintf('?include=%s', implode(',', $include));
}

return $this->client->apiRequest('GET', $requestUrl);
}
Expand All @@ -263,43 +291,50 @@ public function getCatalogCurator(string $storefront, string $curatorId, string
* @param string $storefront
* @param string $curatorId
* @param string $relationship
* @param string $include A comma separated list of additional relationships to include in the fetch.
* @param array $include A list of additional relationships to include in the fetch.
*
* @return array|object
*
* @throws AppleMusicAPIException
*/
public function getCatalogCuratorRelationship(string $storefront, string $curatorId, string $relationship = 'playlists', int $limit = 10, int $offset = 0, string $include = '')
public function getCatalogCuratorRelationship(string $storefront, string $curatorId, string $relationship = 'playlists', int $limit = 10, int $offset = 0, array $include = [])
{
if ($relationship !== 'playlists') {
throw new AppleMusicAPIException('Invalid relationship given, only \'playlists\' is allowed at the moment.');
}

$requestUrl = sprintf(
'catalog/%s/curators/%s/%s?%s&include=%s',
'catalog/%s/curators/%s/%s?%s',
$storefront,
$curatorId,
$relationship,
$include,
$this->getLimitOffsetQueryString($limit, $offset)
);

if (count($include)) {
$requestUrl .= sprintf('&include=%s', implode(',', $include));
}

return $this->client->apiRequest('GET', $requestUrl);
}

/**
* Fetch a user’s storefront.
* https://developer.apple.com/documentation/applemusicapi/get_a_user_s_storefront
*
* @param string $include A comma separated list of additional relationships to include in the fetch.
* @param array $include A list of additional relationships to include in the fetch.
*
* @return array|object
*
* @throws AppleMusicAPIException
*/
public function getUsersStorefront(string $include = '')
public function getUsersStorefront(array $include = [])
{
$requestUrl = sprintf('me/storefront?include=%s', $include);
$requestUrl = 'me/storefront';

if (count($include)) {
$requestUrl .= sprintf('?include=%s', implode(',', $include));
}

return $this->client->apiRequest('GET', $requestUrl);
}
Expand All @@ -308,18 +343,22 @@ public function getUsersStorefront(string $include = '')
* Fetch the recently played resources for the user.
* https://developer.apple.com/documentation/applemusicapi/get_recently_played_resources
*
* @param int $limit The limit on the number of objects, or number of objects in the specified relationship,
* @param int $limit The limit on the number of objects, or number of objects in the specified relationship,
* that are returned.
* @param int $offset The next page or group of objects to fetch.
* @param string $include A comma separated list of additional relationships to include in the fetch.
* @param int $offset The next page or group of objects to fetch.
* @param array $include A list of additional relationships to include in the fetch.
*
* @return array|object
*
* @throws AppleMusicAPIException
*/
public function getRecentlyPlayedResources(int $limit = 5, int $offset = 0, string $include = '')
public function getRecentlyPlayedResources(int $limit = 5, int $offset = 0, array $include = [])
{
$requestUrl = sprintf('me/recent/played?%s&include=%s', $this->getLimitOffsetQueryString($limit, $offset, 10), $include);
$requestUrl = sprintf('me/recent/played?%s', $this->getLimitOffsetQueryString($limit, $offset, 10));

if (count($include)) {
$requestUrl .= sprintf('&include=%s', implode(',', $include));
}

return $this->client->apiRequest('GET', $requestUrl);
}
Expand All @@ -328,17 +367,21 @@ public function getRecentlyPlayedResources(int $limit = 5, int $offset = 0, stri
* Fetch all the library playlists in alphabetical order.
* https://developer.apple.com/documentation/applemusicapi/get_all_library_playlists
*
* @param int $limit
* @param int $offset
* @param string $include A comma separated list of additional relationships to include in the fetch.
* @param int $limit
* @param int $offset
* @param array $include A list of additional relationships to include in the fetch.
*
* @return array|object
*
* @throws AppleMusicAPIException
*/
public function getAllLibraryPlaylists(int $limit = 25, int $offset = 0, string $include = '')
public function getAllLibraryPlaylists(int $limit = 25, int $offset = 0, array $include = [])
{
$requestUrl = sprintf('me/library/playlists?%s&include=%s', $this->getLimitOffsetQueryString($limit, $offset), $include);
$requestUrl = sprintf('me/library/playlists?%s', $this->getLimitOffsetQueryString($limit, $offset));

if (count($include)) {
$requestUrl .= sprintf('&include=%s', implode(',', $include));
}

return $this->client->apiRequest('GET', $requestUrl);
}
Expand All @@ -347,17 +390,21 @@ public function getAllLibraryPlaylists(int $limit = 25, int $offset = 0, string
* Fetch all the library albums in alphabetical order.
* https://developer.apple.com/documentation/applemusicapi/get_all_library_albums
*
* @param int $limit
* @param int $offset
* @param string $include A comma separated list of additional relationships to include in the fetch.
* @param int $limit
* @param int $offset
* @param array $include A list of additional relationships to include in the fetch.
*
* @return array|object
*
* @throws AppleMusicAPIException
*/
public function getAllLibraryAlbums(int $limit = 25, int $offset = 0, string $include = '')
public function getAllLibraryAlbums(int $limit = 25, int $offset = 0, array $include = [])
{
$requestUrl = sprintf('me/library/albums?%s&include=%s', $this->getLimitOffsetQueryString($limit, $offset), $include);
$requestUrl = sprintf('me/library/albums?%s', $this->getLimitOffsetQueryString($limit, $offset));

if (count($include)) {
$requestUrl .= sprintf('&include=%s', implode(',', $include));
}

return $this->client->apiRequest('GET', $requestUrl);
}
Expand All @@ -366,17 +413,21 @@ public function getAllLibraryAlbums(int $limit = 25, int $offset = 0, string $in
* Fetch all the library artists in alphabetical order.
* https://developer.apple.com/documentation/applemusicapi/get_all_library_artists
*
* @param int $limit
* @param int $offset
* @param string $include A comma separated list of additional relationships to include in the fetch.
* @param int $limit
* @param int $offset
* @param array $include A list of additional relationships to include in the fetch.
*
* @return array|object
*
* @throws AppleMusicAPIException
*/
public function getAllLibraryArtists(int $limit = 25, int $offset = 0, string $include = '')
public function getAllLibraryArtists(int $limit = 25, int $offset = 0, array $include = [])
{
$requestUrl = sprintf('me/library/artists?%s&include=%s', $this->getLimitOffsetQueryString($limit, $offset), $include);
$requestUrl = sprintf('me/library/artists?%s', $this->getLimitOffsetQueryString($limit, $offset));

if (count($include)) {
$requestUrl .= sprintf('&include=%s', implode(',', $include));
}

return $this->client->apiRequest('GET', $requestUrl);
}
Expand All @@ -385,17 +436,21 @@ public function getAllLibraryArtists(int $limit = 25, int $offset = 0, string $i
* Fetch all the library music videos in alphabetical order.
* https://developer.apple.com/documentation/applemusicapi/get_all_library_music_videos
*
* @param int $limit
* @param int $offset
* @param string $include A comma separated list of additional relationships to include in the fetch.
* @param int $limit
* @param int $offset
* @param array $include A list of additional relationships to include in the fetch.
*
* @return array|object
*
* @throws AppleMusicAPIException
*/
public function getAllLibraryMusicVideos(int $limit = 25, int $offset = 0, string $include = '')
public function getAllLibraryMusicVideos(int $limit = 25, int $offset = 0, array $include = [])
{
$requestUrl = sprintf('me/library/music-videos?%s&include=%s', $this->getLimitOffsetQueryString($limit, $offset), $include);
$requestUrl = sprintf('me/library/music-videos?%s', $this->getLimitOffsetQueryString($limit, $offset));

if (count($include)) {
$requestUrl .= sprintf('&include=%s', implode(',', $include));
}

return $this->client->apiRequest('GET', $requestUrl);
}
Expand Down

0 comments on commit 3e5bbfb

Please sign in to comment.