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
<?php
namespace Lackoxygen\TiktokShop\Response;
use Illuminate\Support\Arr;
use Lackoxygen\TiktokShop\Util\Json;
class ResultSet
{
/**
* @var array|mixed
*/
protected array $items = [];
/**
* @param array $items
*/
public function __construct(array $items)
{
$this->items = $items;
}
/**
* @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['message'] ?? '';
}
/**
* @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());
}
/**
* @param $name
* @return $this|array|\ArrayAccess|mixed
*/
public function __get($name)
{
if (!Arr::exists($this->items, $name)) {
return $this->items;
}
$items = Arr::get($this->items, $name);
if (is_array($items)) {
return new static($items);
}
return $items;
}
}