Add JsonResponse middleware for Guzzle because json_decode(->getBody()->getContents()) sucks.
This commit is contained in:
parent
f1ff5b2340
commit
a385f4c134
1 changed files with 86 additions and 0 deletions
86
src/Guzzle/JsonResponse.php
Normal file
86
src/Guzzle/JsonResponse.php
Normal file
|
|
@ -0,0 +1,86 @@
|
|||
<?php
|
||||
|
||||
namespace Benzine\Guzzle;
|
||||
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\StreamInterface;
|
||||
|
||||
class JsonResponse implements ResponseInterface {
|
||||
public function __construct(
|
||||
protected ResponseInterface $response
|
||||
){}
|
||||
|
||||
public function json(){
|
||||
return json_decode($this->response->getBody()->getContents(), false);
|
||||
}
|
||||
|
||||
public function getProtocolVersion()
|
||||
{
|
||||
return $this->response->getProtocolVersion();
|
||||
}
|
||||
|
||||
public function withProtocolVersion($version)
|
||||
{
|
||||
return $this->response->withProtocolVersion($version);
|
||||
}
|
||||
|
||||
public function getHeaders()
|
||||
{
|
||||
return $this->response->getHeaders();
|
||||
}
|
||||
|
||||
public function hasHeader($name)
|
||||
{
|
||||
return $this->response->hasHeader($name);
|
||||
}
|
||||
|
||||
public function getHeader($name)
|
||||
{
|
||||
return $this->response->getHeader($name);
|
||||
}
|
||||
|
||||
public function getHeaderLine($name)
|
||||
{
|
||||
return $this->response->getHeaderLine($name);
|
||||
}
|
||||
|
||||
public function withHeader($name, $value)
|
||||
{
|
||||
return $this->response->withHeader($name, $value);
|
||||
}
|
||||
|
||||
public function withAddedHeader($name, $value)
|
||||
{
|
||||
return $this->response->withAddedHeader($name, $value);
|
||||
}
|
||||
|
||||
public function withoutHeader($name)
|
||||
{
|
||||
return $this->response->withoutHeader($name);
|
||||
}
|
||||
|
||||
public function getBody()
|
||||
{
|
||||
return $this->response->getBody();
|
||||
}
|
||||
|
||||
public function withBody(StreamInterface $body)
|
||||
{
|
||||
return $this->response->withBody($body);
|
||||
}
|
||||
|
||||
public function getStatusCode()
|
||||
{
|
||||
return $this->response->getStatusCode();
|
||||
}
|
||||
|
||||
public function withStatus($code, $reasonPhrase = '')
|
||||
{
|
||||
return $this->response->withStatus($code, $reasonPhrase);
|
||||
}
|
||||
|
||||
public function getReasonPhrase()
|
||||
{
|
||||
return $this->response->getReasonPhrase();
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue