1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author stev leibelt <[email protected]> |
4
|
|
|
* @since 2015-12-08 |
5
|
|
|
*/ |
6
|
|
|
namespace Net\Bazzline\Component\Curl\Builder; |
7
|
|
|
|
8
|
|
|
use Exception; |
9
|
|
|
use Net\Bazzline\Component\Curl\HeaderLine\HeaderLineInterface; |
10
|
|
|
use Net\Bazzline\Component\Curl\HeaderLine\ContentTypeIsJson; |
11
|
|
|
use Net\Bazzline\Component\Curl\Option\OptionInterface; |
12
|
|
|
use Net\Bazzline\Component\Curl\Request\Request; |
13
|
|
|
use Net\Bazzline\Component\Curl\Response\Response; |
14
|
|
|
use Net\Bazzline\Component\Curl\ResponseBehaviour\ConvertJsonToArrayBehaviour; |
15
|
|
|
use Net\Bazzline\Component\Curl\ResponseBehaviour\ResponseBehaviourInterface; |
16
|
|
|
use Net\Bazzline\Component\Toolbox\HashMap\Merge; |
17
|
|
|
use RuntimeException; |
18
|
|
|
|
19
|
|
|
class Builder |
20
|
|
|
{ |
21
|
|
|
const METHOD_DELETE = 0; |
22
|
|
|
const METHOD_GET = 1; |
23
|
|
|
const METHOD_PATCH = 2; |
24
|
|
|
const METHOD_POST = 3; |
25
|
|
|
const METHOD_PUT = 4; |
26
|
|
|
|
27
|
|
|
/** @var boolean */ |
28
|
|
|
private $asJson; |
29
|
|
|
|
30
|
|
|
/** @var null|string|array */ |
31
|
|
|
private $data; |
32
|
|
|
|
33
|
|
|
/** @var array|ResponseBehaviourInterface[] */ |
34
|
|
|
private $defaultResponseBehaviours; |
35
|
|
|
|
36
|
|
|
/** @var Merge */ |
37
|
|
|
private $merge; |
38
|
|
|
|
39
|
|
|
/** @var int */ |
40
|
|
|
private $method; |
41
|
|
|
|
42
|
|
|
/** @var array */ |
43
|
|
|
private $parameters; |
44
|
|
|
|
45
|
|
|
/** @var Request */ |
46
|
|
|
private $request; |
47
|
|
|
|
48
|
|
|
/** @var array|ResponseBehaviourInterface[] */ |
49
|
|
|
private $responseBehaviours; |
50
|
|
|
|
51
|
|
|
/** @var string */ |
52
|
|
|
private $url; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param Request $request |
56
|
|
|
* @param Merge $merge |
57
|
|
|
* @param array|ResponseBehaviourInterface[] $defaultResponseBehaviours |
58
|
|
|
*/ |
59
|
|
|
public function __construct(Request $request, Merge $merge, array $defaultResponseBehaviours = array()) |
60
|
|
|
{ |
61
|
|
|
$this->defaultResponseBehaviours = $defaultResponseBehaviours; |
62
|
|
|
$this->merge = $merge; |
63
|
|
|
$this->request = $request; |
64
|
|
|
$this->reset(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return Response |
69
|
|
|
* @throws Exception|RuntimeException |
70
|
|
|
*/ |
71
|
|
|
public function andFetchTheResponse() |
72
|
|
|
{ |
73
|
|
|
$asJson = $this->asJson; |
74
|
|
|
$merge = $this->merge; |
75
|
|
|
/** @var ResponseBehaviourInterface[] $behaviours */ |
76
|
|
|
$behaviours = $merge($this->responseBehaviours, $this->defaultResponseBehaviours); |
77
|
|
|
$data = $this->data; |
78
|
|
|
$method = $this->method; |
79
|
|
|
$parameters = $this->parameters; |
80
|
|
|
$request = $this->request; |
81
|
|
|
$url = $this->url; |
82
|
|
|
|
83
|
|
|
if ($asJson) { |
84
|
|
|
$data = json_encode($data); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
switch ($method) { |
88
|
|
|
case self::METHOD_DELETE: |
89
|
|
|
$response = $request->delete($url, $parameters); |
90
|
|
|
break; |
91
|
|
|
case self::METHOD_GET: |
92
|
|
|
$response = $request->get($url, $parameters); |
93
|
|
|
break; |
94
|
|
|
case self::METHOD_PATCH: |
95
|
|
|
$response = $request->patch($url, $parameters, $data); |
96
|
|
|
break; |
97
|
|
|
case self::METHOD_POST: |
98
|
|
|
$response = $request->post($url, $parameters, $data); |
99
|
|
|
break; |
100
|
|
|
case self::METHOD_PUT: |
101
|
|
|
$response = $request->put($url, $parameters, $data); |
102
|
|
|
break; |
103
|
|
|
default: |
104
|
|
|
throw new RuntimeException( |
105
|
|
|
'no http method set' |
106
|
|
|
); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
foreach ($behaviours as $behaviour) { |
110
|
|
|
$response = $behaviour->behave($response); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return $response; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @return $this |
118
|
|
|
*/ |
119
|
|
|
public function asJson() |
120
|
|
|
{ |
121
|
|
|
$this->asJson = true; |
122
|
|
|
$this->request->addHeaderLine(new ContentTypeIsJson()); |
123
|
|
|
$this->responseBehaviours[] = new ConvertJsonToArrayBehaviour(); |
124
|
|
|
|
125
|
|
|
return $this; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @param bool $alsoTheDefaults |
130
|
|
|
* @return $this |
131
|
|
|
*/ |
132
|
|
|
public function reset($alsoTheDefaults = false) |
133
|
|
|
{ |
134
|
|
|
$this->asJson = false; |
135
|
|
|
$this->data = null; |
136
|
|
|
$this->parameters = array(); |
137
|
|
|
$this->request->reset($alsoTheDefaults); |
138
|
|
|
$this->responseBehaviours = array(); |
139
|
|
|
$this->url = null; |
140
|
|
|
|
141
|
|
|
return $this; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @param string $url |
146
|
|
|
* @return $this |
147
|
|
|
*/ |
148
|
|
|
public function onTheUrl($url) |
149
|
|
|
{ |
150
|
|
|
$this->url = $url; |
151
|
|
|
|
152
|
|
|
return $this; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @param null|string|array $data |
157
|
|
|
* @return $this |
158
|
|
|
*/ |
159
|
|
|
public function withTheData($data) |
160
|
|
|
{ |
161
|
|
|
$this->data = $data; |
162
|
|
|
|
163
|
|
|
return $this; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @param HeaderLineInterface $line |
168
|
|
|
* @return $this |
169
|
|
|
*/ |
170
|
|
|
public function withTheHeaderLine(HeaderLineInterface $line) |
171
|
|
|
{ |
172
|
|
|
$this->request->addHeaderLine($line); |
173
|
|
|
|
174
|
|
|
return $this; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* @param OptionInterface $option |
179
|
|
|
* @return $this |
180
|
|
|
*/ |
181
|
|
|
public function withTheOption(OptionInterface $option) |
182
|
|
|
{ |
183
|
|
|
$this->request->addOption($option); |
184
|
|
|
|
185
|
|
|
return $this; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* @param array $parameters |
190
|
|
|
* @return $this |
191
|
|
|
*/ |
192
|
|
|
public function withTheParameters(array $parameters) |
193
|
|
|
{ |
194
|
|
|
$this->parameters = $parameters; |
195
|
|
|
|
196
|
|
|
return $this; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* @param string $line |
201
|
|
|
* @return $this |
202
|
|
|
*/ |
203
|
|
|
public function withTheRawHeaderLine($line) |
204
|
|
|
{ |
205
|
|
|
$this->request->addRawHeaderLine($line); |
206
|
|
|
|
207
|
|
|
return $this; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* @param string $key |
212
|
|
|
* @param string $value |
213
|
|
|
* @return $this |
214
|
|
|
*/ |
215
|
|
|
public function withTheRawOption($key, $value) |
216
|
|
|
{ |
217
|
|
|
$this->request->addRawOption($key, $value); |
218
|
|
|
|
219
|
|
|
return $this; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* @param ResponseBehaviourInterface $behaviour |
224
|
|
|
* @return $this |
225
|
|
|
*/ |
226
|
|
|
public function withTheResponseBehaviour(ResponseBehaviourInterface $behaviour) |
227
|
|
|
{ |
228
|
|
|
$this->responseBehaviours[] = $behaviour; |
229
|
|
|
|
230
|
|
|
return $this; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
//@todo better nameing? |
234
|
|
|
// callDelete |
235
|
|
|
/** |
236
|
|
|
* @return $this |
237
|
|
|
*/ |
238
|
|
|
public function useDelete() |
239
|
|
|
{ |
240
|
|
|
$this->method = self::METHOD_DELETE; |
241
|
|
|
|
242
|
|
|
return $this; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* @return $this |
247
|
|
|
*/ |
248
|
|
|
public function useGet() |
249
|
|
|
{ |
250
|
|
|
$this->method = self::METHOD_GET; |
251
|
|
|
|
252
|
|
|
return $this; |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* @return $this |
257
|
|
|
*/ |
258
|
|
|
public function usePatch() |
259
|
|
|
{ |
260
|
|
|
$this->method = self::METHOD_PATCH; |
261
|
|
|
|
262
|
|
|
return $this; |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* @return $this |
267
|
|
|
*/ |
268
|
|
|
public function usePost() |
269
|
|
|
{ |
270
|
|
|
$this->method = self::METHOD_POST; |
271
|
|
|
|
272
|
|
|
return $this; |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* @return $this |
277
|
|
|
*/ |
278
|
|
|
public function usePut() |
279
|
|
|
{ |
280
|
|
|
$this->method = self::METHOD_PUT; |
281
|
|
|
|
282
|
|
|
return $this; |
283
|
|
|
} |
284
|
|
|
} |
285
|
|
|
|