Skip to content

Commit 3ec4d82

Browse files
author
JLTRY
committed
JSon response not detected
case of header = Array ( [0] => application/json; charset=utf-8 ) correct phpstan
1 parent cb57bdb commit 3ec4d82

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

src/Client.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Joomla\Http\Exception\UnexpectedResponseException;
1414
use Joomla\Http\Http;
1515
use Joomla\Http\HttpFactory;
16+
use Joomla\Http\Response;
1617
use Joomla\Input\Input;
1718
use Joomla\Uri\Uri;
1819

@@ -79,6 +80,28 @@ public function __construct($options = [], ?Http $http = null, ?Input $input = n
7980
$this->application = $application;
8081
}
8182

83+
/**
84+
* Tests if given response contains JSON header
85+
*
86+
* @param Response $response The response object
87+
*
88+
* @return boolean
89+
*
90+
*/
91+
private static function isJsonResponse(Response $response)
92+
{
93+
foreach (['Content-Type', 'content-type'] as $type) {
94+
$content_type = $response->getHeader[$type];
95+
if (is_array($content_type)) {
96+
$content_type = $content_type[0];
97+
}
98+
if (strpos($content_type, 'application/json') !== false) {
99+
return true;
100+
}
101+
}
102+
return false;
103+
}
104+
82105
/**
83106
* Get the access token or redirect to the authentication URL.
84107
*
@@ -114,7 +137,7 @@ public function authenticate()
114137
);
115138
}
116139

117-
if (in_array('application/json', $response->getHeader('Content-Type'))) {
140+
if ($this->isJsonResponse($response)) {
118141
$token = array_merge(json_decode((string) $response->getBody(), true), ['created' => time()]);
119142
} else {
120143
parse_str((string) $response->getBody(), $token);

0 commit comments

Comments
 (0)