ResultSet.php
1.7 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<?php
namespace Lackoxygen\TiktokShop\Response;
use GuzzleHttp\Psr7\Response;
use Illuminate\Support\Arr;
use Lackoxygen\TiktokShop\Util\Json;
class ResultSet
{
/**
* @var array|mixed
*/
protected array $items = [];
/**
* @var Response
*/
protected Response $response;
/**
* @param Response $response
*/
public function __construct(Response $response)
{
$this->response = $response;
$this->items = Json::unmarshal($response->getBody()->getContents());
}
/**
* @return bool
*/
public function isSuccess(): bool
{
$array = $this->toArray();
return 10000 === $array['code'];
}
/**
* @return string
*/
public function getMessage(): string
{
$array = $this->toArray();
return (string)$array['sub_msg'] ?? '';
}
/**
* @return array
*/
public function getData(): array
{
$array = $this->toArray();
return (array)Arr::get($array, 'data', []);
}
/**
* @param string $key
* @param string $default
* @return array|\ArrayAccess|mixed
*/
public function get(string $key, string $default = '')
{
return Arr::get($this->getData(), $key, $default);
}
/**
* @return array
*/
public function toArray(): array
{
return $this->items;
}
/**
* @return false|string
*/
public function toJson()
{
return Json::marshal($this->toArray());
}
/**
* @return string
*/
public function raw(): string
{
$body = $this->response->getBody();
$body->rewind();
return $body->getContents();
}
}