diff --git a/src/DeezerAPI.php b/src/DeezerAPI.php index e14ee14..b8f4d4f 100644 --- a/src/DeezerAPI.php +++ b/src/DeezerAPI.php @@ -10,7 +10,7 @@ class DeezerAPI /** * @var DeezerAPIClient */ - protected $client; + protected DeezerAPIClient $client; /** * DeezerAPI constructor. @@ -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); } } diff --git a/src/DeezerAPIClient.php b/src/DeezerAPIClient.php index 3f3c22e..db15814 100644 --- a/src/DeezerAPIClient.php +++ b/src/DeezerAPIClient.php @@ -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) { diff --git a/tests/DeezerAPITest.php b/tests/DeezerAPITest.php index 0d0b868..0c3e840 100644 --- a/tests/DeezerAPITest.php +++ b/tests/DeezerAPITest.php @@ -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'); } }