Skip to content

Commit

Permalink
Merge pull request #4 from Martin1982/add-search
Browse files Browse the repository at this point in the history
Add search
  • Loading branch information
PouleR authored Dec 7, 2022
2 parents a1cc588 + be87101 commit 1fd5d6b
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 16 deletions.
12 changes: 6 additions & 6 deletions src/DeezerAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class DeezerAPI
/**
* @var DeezerAPIClient
*/
protected $client;
protected DeezerAPIClient $client;

/**
* DeezerAPI constructor.
Expand Down Expand Up @@ -234,22 +234,22 @@ public function addPlaylistToFavorites($playlistId)
*
* @throws DeezerAPIException
*/
public function search($query, $strict = false, $order = null)
public function search(string $query, bool $strict = false, string $order = null): object|array
{
if (empty($query)) {
throw new DeezerAPIException('A query parameter is mandatory');
}

$parameters = sprintf('q=%s', $query);
$apiQuery = ['q' => $query];

if (true === $strict) {
$parameters.= '&strict=on';
$apiQuery['strict'] = 'on';
}

if ($order) {
$parameters.= sprintf('&order=%s', $order);
$apiQuery['order'] = $order;
}

return $this->client->apiRequest('GET', 'search', [], $parameters);
return $this->client->apiRequest('GET', 'search', [], null, $apiQuery);
}
}
23 changes: 15 additions & 8 deletions src/DeezerAPIClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,22 +74,29 @@ public function getResponseType(): int
* @param string $service
* @param array $headers
* @param array|string|resource|\Traversable|\Closure $body
* @param array|null $query
*
* @return object|array
*
* @throws DeezerAPIException
*/
public function apiRequest(string $method, string $service, array $headers = [], $body = null)
public function apiRequest(string $method, string $service, array $headers = [], $body = null, $query = null)
{
$url = sprintf(
'%s/%s?access_token=%s',
self::DEEZER_API_URL,
$service,
$this->accessToken
);
$url = sprintf('%s/%s', self::DEEZER_API_URL, $service);

if (null === $query) {
$query = [];
}

$query['access_token'] = $this->accessToken;
$url.= '?'.http_build_query($query);

try {
$response = $this->httpClient->request($method, $url, ['headers' => $headers, 'body' => $body]);
$response = $this->httpClient->request($method, $url, [
'headers' => $headers,
'body' => $body,
'query' => $query
]);

return json_decode($response->getContent(), $this->responseType === self::RETURN_AS_ASSOC);
} catch (ServerExceptionInterface | ClientExceptionInterface | RedirectionExceptionInterface | TransportExceptionInterface $exception) {
Expand Down
4 changes: 2 additions & 2 deletions tests/DeezerAPITest.php
Original file line number Diff line number Diff line change
Expand Up @@ -303,8 +303,8 @@ public function testSearch(): void
{
$this->client->expects(static::once())
->method('apiRequest')
->with('GET', 'search', [], 'q=bohemian&strict=on&order=RANKING')
->willReturn('{}');
->with('GET', 'search', [], null, ['q' => 'bohemian', 'strict' => 'on', 'order' => 'RANKING'])
->willReturn([]);
$this->deezerApi->search('bohemian', true, 'RANKING');
}
}

0 comments on commit 1fd5d6b

Please sign in to comment.