. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ namespace fkooman\OAuth\Client\Http; use fkooman\OAuth\Client\Http\Exception\ResponseException; class Response { /** @var int */ private $statusCode; /** @var string */ private $responseBody; /** @var array */ private $responseHeaders; /** * @param int $statusCode * @param string $responseBody * @param array $responseHeaders */ public function __construct($statusCode, $responseBody, array $responseHeaders = []) { $this->statusCode = $statusCode; $this->responseBody = $responseBody; $this->responseHeaders = $responseHeaders; } /** * @return int */ public function getStatusCode() { return $this->statusCode; } /** * @return string */ public function getBody() { return $this->responseBody; } /** * @param string $key * * @return bool */ public function hasHeader($key) { foreach ($this->responseHeaders as $k => $v) { if (strtoupper($key) === strtoupper($k)) { return true; } } return false; } /** * @param string $key * * @return string */ public function getHeader($key) { foreach ($this->responseHeaders as $k => $v) { if (strtoupper($key) === strtoupper($k)) { return $v; } } throw new ResponseException(sprintf('header "%s" not set', $key)); } /** * @return mixed */ public function json() { if (false === strpos($this->getHeader('Content-Type'), 'application/json')) { throw new ResponseException(sprintf('response MUST have JSON content type')); } $decodedJson = json_decode($this->responseBody, true); if (null === $decodedJson && JSON_ERROR_NONE !== json_last_error()) { $errorMsg = function_exists('json_last_error_msg') ? json_last_error_msg() : json_last_error(); throw new ResponseException(sprintf('unable to decode JSON: %s', $errorMsg)); } return $decodedJson; } /** * @return bool */ public function isOkay() { return 200 <= $this->statusCode && 300 > $this->statusCode; } }