|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This software may be modified and distributed under the terms |
|
7
|
|
|
* of the MIT license. See the LICENSE file for details. |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace FAPI\Boilerplate; |
|
11
|
|
|
|
|
12
|
|
|
use Http\Discovery\MessageFactoryDiscovery; |
|
13
|
|
|
use Http\Message\MultipartStream\MultipartStreamBuilder; |
|
14
|
|
|
use Http\Message\RequestFactory; |
|
15
|
|
|
use Psr\Http\Message\RequestInterface; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @author Tobias Nyholm <[email protected]> |
|
19
|
|
|
* |
|
20
|
|
|
* @internal This class should not be used outside of the API Client, it is not part of the BC promise. |
|
21
|
|
|
*/ |
|
22
|
|
|
final class RequestBuilder |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* @var RequestFactory |
|
26
|
|
|
*/ |
|
27
|
|
|
private $requestFactory; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var MultipartStreamBuilder |
|
31
|
|
|
*/ |
|
32
|
|
|
private $multipartStreamBuilder; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @param RequestFactory $requestFactory |
|
36
|
|
|
* @param MultipartStreamBuilder $multipartStreamBuilder |
|
37
|
|
|
*/ |
|
38
|
|
|
public function __construct( |
|
39
|
|
|
RequestFactory $requestFactory = null, |
|
40
|
|
|
MultipartStreamBuilder $multipartStreamBuilder = null |
|
41
|
|
|
) { |
|
42
|
|
|
$this->requestFactory = $requestFactory ?: MessageFactoryDiscovery::find(); |
|
43
|
|
|
$this->multipartStreamBuilder = $multipartStreamBuilder ?: new MultipartStreamBuilder(); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Creates a new PSR-7 request. |
|
48
|
|
|
* |
|
49
|
|
|
* @param string $method |
|
50
|
|
|
* @param string $uri |
|
51
|
|
|
* @param array $headers |
|
52
|
|
|
* @param array|string|null $body Request body. If body is an array we will send a as multipart stream request. |
|
53
|
|
|
* If array, each array *item* MUST look like: |
|
54
|
|
|
* array ( |
|
55
|
|
|
* 'content' => string|resource|StreamInterface, |
|
56
|
|
|
* 'name' => string, |
|
57
|
|
|
* 'filename'=> string (optional) |
|
58
|
|
|
* 'headers' => array (optinal) ['header-name' => 'header-value'] |
|
59
|
|
|
* ) |
|
60
|
|
|
* |
|
61
|
|
|
* @return RequestInterface |
|
62
|
|
|
*/ |
|
63
|
|
|
public function create(string $method, string $uri, array $headers = [], $body = null): RequestInterface |
|
64
|
|
|
{ |
|
65
|
|
|
if (!is_array($body)) { |
|
66
|
|
|
return $this->requestFactory->createRequest($method, $uri, $headers, $body); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
foreach ($body as $item) { |
|
70
|
|
|
$name = $item['name']; |
|
71
|
|
|
$content = $item['content']; |
|
72
|
|
|
unset($item['name']); |
|
73
|
|
|
unset($item['content']); |
|
74
|
|
|
|
|
75
|
|
|
$this->multipartStreamBuilder->addResource($name, $content, $item); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
$multipartStream = $this->multipartStreamBuilder->build(); |
|
79
|
|
|
$boundary = $this->multipartStreamBuilder->getBoundary(); |
|
80
|
|
|
|
|
81
|
|
|
$headers['Content-Type'] = 'multipart/form-data; boundary='.$boundary; |
|
82
|
|
|
$this->multipartStreamBuilder->reset(); |
|
83
|
|
|
|
|
84
|
|
|
return $this->requestFactory->createRequest($method, $uri, $headers, $multipartStream); |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|