作者 lackoxygen

feat:get attr

@@ -6,6 +6,8 @@ use Lackoxygen\TiktokShop\Attribute\Config\Config; @@ -6,6 +6,8 @@ use Lackoxygen\TiktokShop\Attribute\Config\Config;
6 use Lackoxygen\TiktokShop\Attribute\Config\Option; 6 use Lackoxygen\TiktokShop\Attribute\Config\Option;
7 use Lackoxygen\TiktokShop\Exception\ClientException; 7 use Lackoxygen\TiktokShop\Exception\ClientException;
8 use Lackoxygen\TiktokShop\Response\ResultSet; 8 use Lackoxygen\TiktokShop\Response\ResultSet;
  9 +use Lackoxygen\TiktokShop\Util\Json;
  10 +use Psr\Http\Message\ResponseInterface;
9 11
10 class RequestProxy extends Config 12 class RequestProxy extends Config
11 { 13 {
@@ -47,10 +49,14 @@ class RequestProxy extends Config @@ -47,10 +49,14 @@ class RequestProxy extends Config
47 49
48 $result = call_user_func_array([$request, $name], $arguments); 50 $result = call_user_func_array([$request, $name], $arguments);
49 51
50 - if (!is_null($result)) { 52 + if (is_array($result)) {
51 return new ResultSet($result); 53 return new ResultSet($result);
52 } 54 }
53 55
54 - return new ResultSet($request()); 56 + $response = $request();
  57 +
  58 + $result = Json::unmarshal($response->getBody()->getContents());
  59 +
  60 + return new ResultSet($result);
55 } 61 }
56 } 62 }
@@ -2,7 +2,6 @@ @@ -2,7 +2,6 @@
2 2
3 namespace Lackoxygen\TiktokShop\Response; 3 namespace Lackoxygen\TiktokShop\Response;
4 4
5 -use GuzzleHttp\Psr7\Response;  
6 use Illuminate\Support\Arr; 5 use Illuminate\Support\Arr;
7 use Lackoxygen\TiktokShop\Util\Json; 6 use Lackoxygen\TiktokShop\Util\Json;
8 7
@@ -14,18 +13,11 @@ class ResultSet @@ -14,18 +13,11 @@ class ResultSet
14 protected array $items = []; 13 protected array $items = [];
15 14
16 /** 15 /**
17 - * @var Response 16 + * @param array $items
18 */ 17 */
19 - protected Response $response;  
20 -  
21 - /**  
22 - * @param Response $response  
23 - */  
24 - public function __construct(Response $response) 18 + public function __construct(array $items)
25 { 19 {
26 - $this->response = $response;  
27 -  
28 - $this->items = Json::unmarshal($response->getBody()->getContents()); 20 + $this->items = $items;
29 } 21 }
30 22
31 /** 23 /**
@@ -85,14 +77,20 @@ class ResultSet @@ -85,14 +77,20 @@ class ResultSet
85 } 77 }
86 78
87 /** 79 /**
88 - * @return string 80 + * @param $name
  81 + * @return $this|array|\ArrayAccess|mixed
89 */ 82 */
90 - public function raw(): string 83 + public function __get($name)
91 { 84 {
92 - $body = $this->response->getBody(); 85 + if (!Arr::exists($this->items, $name)) {
  86 + return $this->items;
  87 + }
  88 + $items = Arr::get($this->items, $name);
93 89
94 - $body->rewind(); 90 + if (is_array($items)) {
  91 + return new static($items);
  92 + }
95 93
96 - return $body->getContents(); 94 + return $items;
97 } 95 }
98 } 96 }