ResultSet.php
1.3 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
<?php
namespace Lackoxygen\TiktokShop\Passage;
use Lackoxygen\TiktokShop\Util\Json;
use GuzzleHttp\Psr7\Response;
use Illuminate\Support\Arr;
class ResultSet
{
protected array $items = [];
protected $result;
public function __construct($result)
{
$this->result = $result;
if ($this->isResponse()) {
$this->items = Json::unmarshal($result->getBody()->getContents());
} else {
$this->items = is_array($result) ? $result : [$result];
}
}
protected function isResponse(): bool
{
return $this->result instanceof Response;
}
public function toArray(): array
{
return $this->items;
}
public function isSuccess(): bool
{
$array = $this->toArray();
if ($this->isResponse()) {
return 10000 === $array['code'];
}
return true === Arr::first($array);
}
public function getMessage(): string
{
$array = $this->toArray();
return (string) $array['message'] ?? '';
}
public function getData(): array
{
$array = $this->toArray();
return (array) Arr::get($array, 'data', []);
}
public function get(string $key, $default = '')
{
return Arr::get($this->getData(), $key, $default);
}
}