1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Franjid\ApiWrapperBundle\Api; |
4
|
|
|
|
5
|
|
|
use Psr\Http\Message\RequestInterface; |
6
|
|
|
use GuzzleHttp\Psr7\Request; |
7
|
|
|
use GuzzleHttp\Psr7\Uri; |
8
|
|
|
use GuzzleHttp\RequestOptions; |
9
|
|
|
|
10
|
|
|
class ApiRequest implements ApiRequestInterface |
11
|
|
|
{ |
12
|
|
|
/** @var RequestInterface $request */ |
13
|
|
|
protected $request; |
14
|
|
|
|
15
|
|
|
/** array $options */ |
16
|
|
|
protected $options = []; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* ApiRequest constructor. |
20
|
|
|
* |
21
|
|
|
* @param string $method |
22
|
|
|
* @param string $uri |
23
|
|
|
*/ |
24
|
8 |
|
public function __construct($method = '', $uri = '') |
25
|
|
|
{ |
26
|
8 |
|
$this->request = new Request($method, $uri); |
27
|
8 |
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* {@inheritdoc} |
31
|
|
|
*/ |
32
|
2 |
|
public function getRequest() |
33
|
|
|
{ |
34
|
2 |
|
return $this->request; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* {@inheritdoc} |
39
|
|
|
*/ |
40
|
1 |
|
public function setMethod($method) |
41
|
|
|
{ |
42
|
1 |
|
$this->request = $this->request->withMethod($method); |
43
|
1 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* {@inheritdoc} |
47
|
|
|
*/ |
48
|
2 |
|
public function getMethod() |
49
|
|
|
{ |
50
|
2 |
|
return $this->request->getMethod(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* {@inheritdoc} |
55
|
|
|
*/ |
56
|
1 |
|
public function setHeader($name, $value) |
57
|
|
|
{ |
58
|
1 |
|
$this->request = $this->request->withHeader($name, $value); |
59
|
1 |
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* {@inheritdoc} |
63
|
|
|
*/ |
64
|
1 |
|
public function setHeaders(array $headers) |
65
|
|
|
{ |
66
|
1 |
|
foreach ($headers as $key => $value) { |
67
|
1 |
|
$this->setHeader($key, $value); |
68
|
1 |
|
} |
69
|
1 |
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* {@inheritdoc} |
73
|
|
|
*/ |
74
|
1 |
|
public function getHeaders() |
75
|
|
|
{ |
76
|
1 |
|
return $this->request->getHeaders(); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* {@inheritdoc} |
81
|
|
|
*/ |
82
|
1 |
|
public function setUri($uri) |
83
|
|
|
{ |
84
|
1 |
|
$this->request = $this->request->withUri(new Uri($uri)); |
85
|
1 |
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* {@inheritdoc} |
89
|
|
|
*/ |
90
|
2 |
|
public function getUri() |
91
|
|
|
{ |
92
|
2 |
|
return $this->request->getUri()->__toString(); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* {@inheritdoc} |
97
|
|
|
*/ |
98
|
1 |
|
public function setBody($body) |
99
|
|
|
{ |
100
|
1 |
|
$this->setOptions([RequestOptions::BODY => $body]); |
101
|
1 |
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* {@inheritdoc} |
105
|
|
|
*/ |
106
|
1 |
|
public function getBody() |
107
|
|
|
{ |
108
|
1 |
|
return isset($this->options[RequestOptions::BODY]) ? $this->options[RequestOptions::BODY] : ''; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* {@inheritdoc} |
113
|
|
|
*/ |
114
|
1 |
|
public function setOptions(array $options) |
115
|
|
|
{ |
116
|
1 |
|
$this->options = array_merge($this->options, $options); |
117
|
1 |
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* {@inheritdoc} |
121
|
|
|
*/ |
122
|
2 |
|
public function getOptions() |
123
|
|
|
{ |
124
|
2 |
|
return $this->options; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|