Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
version: "3.8"

services:
package:
build: .
Expand Down
2 changes: 1 addition & 1 deletion src/Exceptions/ApiException.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,6 @@ private function getErrorLinkFromHttpBody(): ?string

public static function rethrowWithHint(\Exception $e, string $methodName)
{
return new \Exception(sprintf(self::HINT_MESSAGE, $methodName), 0, $e);
return new \Exception(\sprintf(self::HINT_MESSAGE, $methodName), 0, $e);
}
}
6 changes: 3 additions & 3 deletions src/Exceptions/InvalidArgumentException.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ final class InvalidArgumentException extends \Exception implements ExceptionInte
public static function invalidType(string $argumentName, array $validTypes): self
{
return new self(
sprintf('Argument "%s" is not a valid type! Please provide an argument that is of type: "%s"', $argumentName, implode('","', $validTypes)),
\sprintf('Argument "%s" is not a valid type! Please provide an argument that is of type: "%s"', $argumentName, implode('","', $validTypes)),
400,
null
);
Expand All @@ -18,7 +18,7 @@ public static function invalidType(string $argumentName, array $validTypes): sel
public static function emptyArgument(string $argumentName): self
{
return new self(
sprintf('Argument "%s" is empty.', $argumentName),
\sprintf('Argument "%s" is empty.', $argumentName),
400,
null
);
Expand All @@ -27,7 +27,7 @@ public static function emptyArgument(string $argumentName): self
public static function dateIsExpired(\DateTimeInterface $date): self
{
return new self(
sprintf('DateTime "%s" is expired. The date expiresAt should be in the future.', $date->format('Y-m-d H:i:s')),
\sprintf('DateTime "%s" is expired. The date expiresAt should be in the future.', $date->format('Y-m-d H:i:s')),
400,
null
);
Expand Down
20 changes: 18 additions & 2 deletions src/Http/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function __construct(
'User-Agent' => implode(';', array_merge($clientAgents, [Meilisearch::qualifiedVersion()])),
]);
if (null !== $apiKey && '' !== $apiKey) {
$this->headers['Authorization'] = sprintf('Bearer %s', $apiKey);
$this->headers['Authorization'] = \sprintf('Bearer %s', $apiKey);
}
$this->json = new Json();
}
Expand Down Expand Up @@ -176,7 +176,7 @@ private function parseResponse(ResponseInterface $response)
return null;
}

if (!\in_array('application/json', $response->getHeader('content-type'), true)) {
if (!$this->isJSONResponse($response->getHeader('content-type'))) {
throw new InvalidResponseBodyException($response, $response->getBody()->getContents());
}

Expand All @@ -188,4 +188,20 @@ private function parseResponse(ResponseInterface $response)

return $this->json->unserialize($response->getBody()->getContents());
}

/**
* Checks if any of the header values indicate a JSON response.
*
* @param array $headerValues the array of header values to check
*
* @return bool true if any header value contains 'application/json', otherwise false
*/
private function isJSONResponse(array $headerValues)
{
$filteredHeaders = array_filter($headerValues, function (string $headerValue) {
return false !== strpos($headerValue, 'application/json');
});

return \count($filteredHeaders) > 0;
}
}
4 changes: 2 additions & 2 deletions src/Http/Serialize/Json.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function serialize($data)
try {
$encoded = json_encode($data, JSON_THROW_ON_ERROR);
} catch (\JsonException $e) {
throw new JsonEncodingException(sprintf(self::JSON_ENCODE_ERROR_MESSAGE, $e->getMessage()), $e->getCode(), $e);
throw new JsonEncodingException(\sprintf(self::JSON_ENCODE_ERROR_MESSAGE, $e->getMessage()), $e->getCode(), $e);
}

return $encoded;
Expand All @@ -28,7 +28,7 @@ public function unserialize(string $string)
try {
$decoded = json_decode($string, true, 512, JSON_THROW_ON_ERROR);
} catch (\JsonException $e) {
throw new JsonDecodingException(sprintf(self::JSON_DECODE_ERROR_MESSAGE, $e->getMessage()), $e->getCode(), $e);
throw new JsonDecodingException(\sprintf(self::JSON_DECODE_ERROR_MESSAGE, $e->getMessage()), $e->getCode(), $e);
}

return $decoded;
Expand Down
2 changes: 1 addition & 1 deletion src/Meilisearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ class Meilisearch

public static function qualifiedVersion()
{
return sprintf('Meilisearch PHP (v%s)', self::VERSION);
return \sprintf('Meilisearch PHP (v%s)', self::VERSION);
}
}
8 changes: 8 additions & 0 deletions tests/Http/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,14 @@ public function testInvalidResponseContentTypeThrowsException(): void
$client->get('/');
}

public function testClientHasCustomJSONContentType(): void
{
$httpClient = $this->createHttpClientMock(200, '{}', 'application/json; charset=utf-8');
$client = new Client('http://localhost:7070', null, $httpClient);

$client->get('/');
}

public function testClientHasDefaultUserAgent(): void
{
$httpClient = $this->createHttpClientMock(200, '{}');
Expand Down
2 changes: 1 addition & 1 deletion tests/VersionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class VersionTest extends TestCase
{
public function testQualifiedVersion(): void
{
$qualifiedVersion = sprintf('Meilisearch PHP (v%s)', Meilisearch::VERSION);
$qualifiedVersion = \sprintf('Meilisearch PHP (v%s)', Meilisearch::VERSION);

self::assertSame(Meilisearch::qualifiedVersion(), $qualifiedVersion);
}
Expand Down
Loading