1 | <?php |
||
23 | final class RequestBuilder |
||
24 | { |
||
25 | /** |
||
26 | * The request method |
||
27 | * |
||
28 | * @var string |
||
29 | */ |
||
30 | private $method; |
||
31 | |||
32 | /** |
||
33 | * The request [@see Uri] object |
||
34 | * |
||
35 | * @var Uri |
||
36 | */ |
||
37 | private $uri; |
||
38 | |||
39 | /** |
||
40 | * An array of query strings that can be appended together |
||
41 | * |
||
42 | * @var array |
||
43 | */ |
||
44 | private $queries = []; |
||
45 | |||
46 | /** |
||
47 | * An array of headers in PSR-7 format |
||
48 | * |
||
49 | * @var array |
||
50 | */ |
||
51 | private $headers; |
||
52 | |||
53 | /** |
||
54 | * The request body |
||
55 | * |
||
56 | * @var StreamInterface |
||
57 | */ |
||
58 | private $body; |
||
59 | |||
60 | /** |
||
61 | * An array of request body fields that can be appended together |
||
62 | * |
||
63 | * @var array |
||
64 | */ |
||
65 | private $fields = []; |
||
66 | |||
67 | /** |
||
68 | * An array of arrays of multipart parts, each with name, content, headers, and filename |
||
69 | * |
||
70 | * @var array[] |
||
71 | */ |
||
72 | private $parts = []; |
||
73 | |||
74 | /** |
||
75 | * Constructor |
||
76 | * |
||
77 | * @param string $method |
||
78 | * @param string $baseUrl |
||
79 | * @param string $uri |
||
80 | * @param array $headers |
||
81 | */ |
||
82 | 65 | public function __construct(string $method, string $baseUrl, string $uri, array $headers) |
|
88 | |||
89 | /** |
||
90 | * Set the uri base url |
||
91 | * |
||
92 | * @param string $value |
||
93 | */ |
||
94 | 4 | public function setBaseUrl(string $value): void |
|
102 | |||
103 | /** |
||
104 | * Replace a url path placeholder with a value |
||
105 | * |
||
106 | * @param string $name |
||
107 | * @param string $value |
||
108 | */ |
||
109 | 4 | public function replacePath(string $name, string $value): void |
|
115 | |||
116 | /** |
||
117 | * Add a query string; if encoded, decodes to be encoded later |
||
118 | * |
||
119 | * @param string $name |
||
120 | * @param string $value |
||
121 | * @param bool $encoded |
||
122 | */ |
||
123 | 9 | public function addQuery(string $name, string $value, bool $encoded): void |
|
132 | |||
133 | /** |
||
134 | * Adds a query string without value; if encoded, decodes to be encoded later |
||
135 | * |
||
136 | * @param string $value |
||
137 | * @param bool $encoded |
||
138 | */ |
||
139 | 5 | public function addQueryName(string $value, bool $encoded): void |
|
147 | |||
148 | /** |
||
149 | * Add a header in PSR-7 format |
||
150 | * |
||
151 | * @param string $name |
||
152 | * @param string $value |
||
153 | */ |
||
154 | 8 | public function addHeader(string $name, string $value): void |
|
159 | |||
160 | /** |
||
161 | * Set the request body |
||
162 | * |
||
163 | * @param StreamInterface $body |
||
164 | */ |
||
165 | 6 | public function setBody(StreamInterface $body): void |
|
169 | |||
170 | /** |
||
171 | * Add a field; if not encoded, encodes first |
||
172 | * |
||
173 | * @param string $name |
||
174 | * @param string $value |
||
175 | * @param bool $encoded |
||
176 | */ |
||
177 | 8 | public function addField(string $name, string $value, bool $encoded): void |
|
186 | |||
187 | /** |
||
188 | * Add a multipart part |
||
189 | * |
||
190 | * @param string $name |
||
191 | * @param StreamInterface $contents |
||
192 | * @param array $headers |
||
193 | * @param null|string $filename |
||
194 | */ |
||
195 | 9 | public function addPart(string $name, StreamInterface $contents, array $headers = [], ?string $filename = null): void |
|
204 | |||
205 | /** |
||
206 | * Create a PSR-7 request |
||
207 | * |
||
208 | * @return Request |
||
209 | * @throws \LogicException |
||
210 | */ |
||
211 | 26 | public function build(): Request |
|
239 | } |
||
240 |