作者 lackoxygen

feat:get attr

... ... @@ -6,6 +6,8 @@ use Lackoxygen\TiktokShop\Attribute\Config\Config;
use Lackoxygen\TiktokShop\Attribute\Config\Option;
use Lackoxygen\TiktokShop\Exception\ClientException;
use Lackoxygen\TiktokShop\Response\ResultSet;
use Lackoxygen\TiktokShop\Util\Json;
use Psr\Http\Message\ResponseInterface;
class RequestProxy extends Config
{
... ... @@ -47,10 +49,14 @@ class RequestProxy extends Config
$result = call_user_func_array([$request, $name], $arguments);
if (!is_null($result)) {
if (is_array($result)) {
return new ResultSet($result);
}
return new ResultSet($request());
$response = $request();
$result = Json::unmarshal($response->getBody()->getContents());
return new ResultSet($result);
}
}
... ...
... ... @@ -2,7 +2,6 @@
namespace Lackoxygen\TiktokShop\Response;
use GuzzleHttp\Psr7\Response;
use Illuminate\Support\Arr;
use Lackoxygen\TiktokShop\Util\Json;
... ... @@ -14,18 +13,11 @@ class ResultSet
protected array $items = [];
/**
* @var Response
* @param array $items
*/
protected Response $response;
/**
* @param Response $response
*/
public function __construct(Response $response)
public function __construct(array $items)
{
$this->response = $response;
$this->items = Json::unmarshal($response->getBody()->getContents());
$this->items = $items;
}
/**
... ... @@ -85,14 +77,20 @@ class ResultSet
}
/**
* @return string
* @param $name
* @return $this|array|\ArrayAccess|mixed
*/
public function raw(): string
public function __get($name)
{
$body = $this->response->getBody();
if (!Arr::exists($this->items, $name)) {
return $this->items;
}
$items = Arr::get($this->items, $name);
$body->rewind();
if (is_array($items)) {
return new static($items);
}
return $body->getContents();
return $items;
}
}
... ...