|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace BEAR\Resource; |
|
6
|
|
|
|
|
7
|
|
|
use function is_array; |
|
8
|
|
|
use function strtoupper; |
|
9
|
|
|
use Symfony\Contracts\HttpClient\HttpClientInterface; |
|
10
|
|
|
use Symfony\Contracts\HttpClient\ResponseInterface; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* @method HttpResourceObject get(AbstractUri|string $uri, array $params = []) |
|
14
|
|
|
* @method HttpResourceObject head(AbstractUri|string $uri, array $params = []) |
|
15
|
|
|
* @method HttpResourceObject put(AbstractUri|string $uri, array $params = []) |
|
16
|
|
|
* @method HttpResourceObject post(AbstractUri|string $uri, array $params = []) |
|
17
|
|
|
* @method HttpResourceObject patch(AbstractUri|string $uri, array $params = []) |
|
18
|
|
|
* @method HttpResourceObject delete(AbstractUri|string $uri, array $params = []) |
|
19
|
|
|
*/ |
|
20
|
|
|
final class HttpResourceObject extends ResourceObject |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @var HttpClientInterface |
|
24
|
|
|
*/ |
|
25
|
|
|
private $client; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var ResponseInterface |
|
29
|
|
|
*/ |
|
30
|
|
|
private $response; |
|
31
|
|
|
|
|
32
|
|
|
public function __construct(HttpClientInterface $client) |
|
33
|
|
|
{ |
|
34
|
|
|
$this->client = $client; |
|
35
|
|
|
unset($this->code, $this->headers, $this->body, $this->view); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function __get(string $name) |
|
39
|
|
|
{ |
|
40
|
|
|
if ($name === 'code') { |
|
41
|
|
|
return $this->response->getStatusCode(); |
|
42
|
|
|
} |
|
43
|
|
|
if ($name === 'headers') { |
|
44
|
|
|
return $this->response->getHeaders(); |
|
45
|
|
|
} |
|
46
|
|
|
if ($name === 'body') { |
|
47
|
|
|
return $this->response->toArray(); |
|
48
|
|
|
} |
|
49
|
|
|
if ($name === 'view') { |
|
50
|
|
|
return $this->response->getContent(); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
throw new \InvalidArgumentException($name); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function __set(string $name, $value) : void |
|
57
|
|
|
{ |
|
58
|
|
|
throw new \InvalidArgumentException($name); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function __isset(string $name) : bool |
|
62
|
|
|
{ |
|
63
|
|
|
return isset($this->{$name}); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function __toString() : string |
|
67
|
|
|
{ |
|
68
|
|
|
return $this->response->getContent(); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
public function request(AbstractRequest $request) |
|
72
|
|
|
{ |
|
73
|
|
|
$uri = $request->resourceObject->uri; |
|
74
|
|
|
$method = strtoupper($uri->method); |
|
75
|
|
|
$options = ($method === 'GET') ? ['query' => $uri->query] : ['body' => $uri->query]; |
|
76
|
|
|
$clientOptions = isset($uri->query['_options']) && is_array($uri->query['_options']) ? $uri->query['_options'] : []; |
|
77
|
|
|
$options += $clientOptions; |
|
78
|
|
|
$this->response = $this->client->request($method, (string) $uri, $options); |
|
79
|
|
|
|
|
80
|
|
|
return $this; |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|