|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Aosmak\Laravel\Layer\Sdk\Models; |
|
4
|
|
|
|
|
5
|
|
|
use Psr\Http\Message\ResponseInterface as GuzzleResponseInterface; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Class Response |
|
9
|
|
|
* @package namespace Aosmak\Laravel\Layer\Sdk\Models |
|
10
|
|
|
*/ |
|
11
|
|
|
class Response implements ResponseInterface |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* Request status code |
|
15
|
|
|
* |
|
16
|
|
|
* @var int |
|
17
|
|
|
*/ |
|
18
|
|
|
private $statusCode; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Request content |
|
22
|
|
|
* |
|
23
|
|
|
* @var array |
|
24
|
|
|
*/ |
|
25
|
|
|
private $contents; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Headers |
|
29
|
|
|
* |
|
30
|
|
|
* @var array |
|
31
|
|
|
*/ |
|
32
|
|
|
private $headers; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Get status code |
|
36
|
|
|
* |
|
37
|
|
|
* @param \Psr\Http\Message\ResponseInterface $response |
|
38
|
|
|
* |
|
39
|
|
|
* @return void |
|
40
|
|
|
*/ |
|
41
|
87 |
|
public function __construct(GuzzleResponseInterface $response) |
|
42
|
|
|
{ |
|
43
|
87 |
|
$this->statusCode = $response->getStatusCode(); |
|
44
|
87 |
|
$this->headers = $response->getHeaders(); |
|
45
|
87 |
|
$this->contents = $this->obtainResponseContent($response->getBody()->getContents()); |
|
46
|
87 |
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Get status code |
|
50
|
|
|
* |
|
51
|
|
|
* @return int status code |
|
52
|
|
|
*/ |
|
53
|
87 |
|
public function getStatusCode(): int |
|
54
|
|
|
{ |
|
55
|
87 |
|
return $this->statusCode; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Get request content |
|
60
|
|
|
* |
|
61
|
|
|
* @return array request content |
|
62
|
|
|
*/ |
|
63
|
53 |
|
public function getContents(): array |
|
64
|
|
|
{ |
|
65
|
53 |
|
return $this->contents; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Get request headers |
|
70
|
|
|
* |
|
71
|
|
|
* @return array request headers |
|
72
|
|
|
*/ |
|
73
|
1 |
|
public function getHeaders(): array |
|
74
|
|
|
{ |
|
75
|
1 |
|
return $this->headers; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Check whether a request was successful |
|
80
|
|
|
* |
|
81
|
|
|
* @return bool |
|
82
|
|
|
*/ |
|
83
|
87 |
|
public function isSuccessful(): bool |
|
84
|
|
|
{ |
|
85
|
87 |
|
return ($this->statusCode >= 200 && $this->statusCode <= 299); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Get an item ID |
|
90
|
|
|
* |
|
91
|
|
|
* @return string|null conversation ID |
|
92
|
|
|
*/ |
|
93
|
11 |
|
public function getCreatedItemId(): ?string |
|
94
|
|
|
{ |
|
95
|
11 |
|
if (!empty($this->contents['id']) && strpos($this->contents['id'], 'layer:///') !== false) { |
|
96
|
10 |
|
return substr(strrchr($this->contents['id'], '/'), 1); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
9 |
|
return null; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Get a response content |
|
104
|
|
|
* |
|
105
|
|
|
* @param string $contents request contents |
|
106
|
|
|
* |
|
107
|
|
|
* @return array |
|
108
|
|
|
*/ |
|
109
|
87 |
|
private function obtainResponseContent(string $contents): array |
|
110
|
|
|
{ |
|
111
|
87 |
|
if (strlen($contents) > 1) { |
|
112
|
57 |
|
$contents = json_decode($contents, 1); |
|
113
|
|
|
return is_array($contents) ? $contents : []; |
|
114
|
|
|
} |
|
115
|
60 |
|
|
|
116
|
|
|
return []; |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|