Response.php
1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<?php
namespace Lackoxygen\MinPayment;
use Lackoxygen\MinPayment\Constant\Status;
use Lackoxygen\MinPayment\Contract\ResponseInterface;
use Psr\Http\Message\ResponseInterface as PsrResponseInterface;
class Response implements ResponseInterface
{
/**
* @var PsrResponseInterface $response
*/
protected $response;
/**
* @param PsrResponseInterface $response
*/
public function __construct(PsrResponseInterface $response)
{
$this->response = $response;
}
/**
* @return bool
*/
public function isSuccess(): bool
{
return Status::SUCCESS === (string)\Arr::get($this->toArray(), 'code');
}
/**
* @return array
*/
public function toArray(): array
{
return (array)\json_decode($this->getContent(), true);
}
/**
* @return string
*/
public function toString(): string
{
return $this->getContent();
}
/**
* @return string
*/
public function getContent(): string
{
static $content = null;
if (is_null($content)) {
$this->response->getBody()->rewind();
$content = $this->response->getBody()->getContents();
}
return $content;
}
}