1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* Copyright (C) 2013-2016 Mailgun |
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 Mailgun; |
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
|
|
|
class RequestBuilder |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var RequestFactory |
24
|
|
|
*/ |
25
|
|
|
private $requestFactory; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var MultipartStreamBuilder |
29
|
|
|
*/ |
30
|
|
|
private $multipartStreamBuilder; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Creates a new PSR-7 request. |
34
|
|
|
* |
35
|
|
|
* @param string $method |
36
|
|
|
* @param string $uri |
37
|
|
|
* @param array $headers |
38
|
|
|
* @param array|string|null $body Request body. If body is an array we will send a as multipart stream request. |
39
|
|
|
* If array, each array *item* MUST look like: |
40
|
|
|
* array ( |
41
|
|
|
* 'content' => string|resource|StreamInterface, |
42
|
|
|
* 'name' => string, |
43
|
|
|
* 'filename'=> string (optional) |
44
|
|
|
* 'headers' => array (optinal) ['header-name' => 'header-value'] |
45
|
|
|
* ) |
46
|
|
|
* |
47
|
|
|
* @return RequestInterface |
48
|
|
|
*/ |
49
|
23 |
|
public function create($method, $uri, array $headers = [], $body = null) |
50
|
|
|
{ |
51
|
23 |
|
if (!is_array($body)) { |
52
|
8 |
|
return $this->getRequestFactory()->createRequest($method, $uri, $headers, $body); |
53
|
|
|
} |
54
|
|
|
|
55
|
15 |
|
$builder = $this->getMultipartStreamBuilder(); |
56
|
15 |
|
foreach ($body as $item) { |
57
|
9 |
|
$name = $item['name']; |
58
|
9 |
|
$content = $item['content']; |
59
|
9 |
|
unset($item['name']); |
60
|
9 |
|
unset($item['content']); |
61
|
|
|
|
62
|
9 |
|
$builder->addResource($name, $content, $item); |
63
|
15 |
|
} |
64
|
|
|
|
65
|
15 |
|
$multipartStream = $builder->build(); |
66
|
15 |
|
$boundary = $builder->getBoundary(); |
67
|
15 |
|
$builder->reset(); |
68
|
|
|
|
69
|
15 |
|
$headers['Content-Type'] = 'multipart/form-data; boundary="'.$boundary.'"'; |
70
|
|
|
|
71
|
15 |
|
return $this->getRequestFactory()->createRequest($method, $uri, $headers, $multipartStream); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @return RequestFactory |
76
|
|
|
*/ |
77
|
23 |
|
private function getRequestFactory() |
78
|
|
|
{ |
79
|
23 |
|
if ($this->requestFactory === null) { |
80
|
23 |
|
$this->requestFactory = MessageFactoryDiscovery::find(); |
81
|
23 |
|
} |
82
|
|
|
|
83
|
23 |
|
return $this->requestFactory; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param RequestFactory $requestFactory |
88
|
|
|
* |
89
|
|
|
* @return RequestBuilder |
90
|
|
|
*/ |
91
|
|
|
public function setRequestFactory($requestFactory) |
92
|
|
|
{ |
93
|
|
|
$this->requestFactory = $requestFactory; |
94
|
|
|
|
95
|
|
|
return $this; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @return MultipartStreamBuilder |
100
|
|
|
*/ |
101
|
15 |
|
private function getMultipartStreamBuilder() |
102
|
|
|
{ |
103
|
15 |
|
if ($this->multipartStreamBuilder === null) { |
104
|
15 |
|
$this->multipartStreamBuilder = new MultipartStreamBuilder(); |
105
|
15 |
|
} |
106
|
|
|
|
107
|
15 |
|
return $this->multipartStreamBuilder; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param MultipartStreamBuilder $multipartStreamBuilder |
112
|
|
|
* |
113
|
|
|
* @return RequestBuilder |
114
|
|
|
*/ |
115
|
|
|
public function setMultipartStreamBuilder($multipartStreamBuilder) |
116
|
|
|
{ |
117
|
|
|
$this->multipartStreamBuilder = $multipartStreamBuilder; |
118
|
|
|
|
119
|
|
|
return $this; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|