1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of Phiremock. |
4
|
|
|
* |
5
|
|
|
* Phiremock is free software: you can redistribute it and/or modify |
6
|
|
|
* it under the terms of the GNU Lesser General Public License as published by |
7
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
8
|
|
|
* (at your option) any later version. |
9
|
|
|
* |
10
|
|
|
* Phiremock is distributed in the hope that it will be useful, |
11
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
12
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13
|
|
|
* GNU General Public License for more details. |
14
|
|
|
* |
15
|
|
|
* You should have received a copy of the GNU General Public License |
16
|
|
|
* along with Phiremock. If not, see <http://www.gnu.org/licenses/>. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
namespace Mcustiel\Phiremock\Common\Utils\V1; |
20
|
|
|
|
21
|
|
|
use Mcustiel\Phiremock\Common\Utils\BodyHelper; |
22
|
|
|
use Mcustiel\Phiremock\Domain\Http\Body; |
23
|
|
|
use Mcustiel\Phiremock\Domain\Http\Header; |
24
|
|
|
use Mcustiel\Phiremock\Domain\Http\HeaderName; |
25
|
|
|
use Mcustiel\Phiremock\Domain\Http\HeadersCollection; |
26
|
|
|
use Mcustiel\Phiremock\Domain\Http\HeaderValue; |
27
|
|
|
use Mcustiel\Phiremock\Domain\Http\StatusCode; |
28
|
|
|
use Mcustiel\Phiremock\Domain\HttpResponse; |
29
|
|
|
use Mcustiel\Phiremock\Domain\Options\Delay; |
30
|
|
|
use Mcustiel\Phiremock\Domain\Options\ScenarioState; |
31
|
|
|
use Mcustiel\Phiremock\Domain\Response; |
32
|
|
|
|
33
|
|
|
class ArrayToHttpResponseConverter extends ArrayToResponseConverter |
34
|
|
|
{ |
35
|
|
|
const ALLOWED_OPTIONS = [ |
36
|
|
|
'statusCode' => null, |
37
|
|
|
'body' => null, |
38
|
|
|
'headers' => null, |
39
|
|
|
'delayMillis'=> null, |
40
|
|
|
]; |
41
|
|
|
const STRING_START = 0; |
42
|
|
|
|
43
|
3 |
|
protected function convertResponse( |
44
|
|
|
array $responseArray, |
45
|
|
|
?Delay $delay, |
46
|
|
|
?ScenarioState $newScenarioState |
47
|
|
|
): Response { |
48
|
3 |
|
if (!isset($responseArray['response']['statusCode'])) { |
49
|
1 |
|
$responseArray['response']['statusCode'] = 200; |
50
|
|
|
} |
51
|
|
|
|
52
|
3 |
|
return new HttpResponse( |
53
|
3 |
|
new StatusCode((int) $responseArray['response']['statusCode']), |
54
|
3 |
|
$this->getBody($responseArray['response']), |
55
|
3 |
|
$this->getHeaders($responseArray['response']), |
56
|
3 |
|
$delay, |
57
|
3 |
|
$newScenarioState |
58
|
|
|
); |
59
|
|
|
} |
60
|
|
|
|
61
|
3 |
|
private function getHeaders(array $responseArray): ?HeadersCollection |
62
|
|
|
{ |
63
|
3 |
|
if (isset($responseArray['headers'])) { |
64
|
1 |
|
$headers = $responseArray['headers']; |
65
|
1 |
|
if (!empty($headers)) { |
66
|
1 |
|
if (!\is_array($headers)) { |
67
|
|
|
throw new \InvalidArgumentException('Response headers are invalid: ' . var_export($headers, true)); |
68
|
|
|
} |
69
|
|
|
|
70
|
1 |
|
return $this->convertHeaders($headers); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
2 |
|
return null; |
75
|
|
|
} |
76
|
|
|
|
77
|
3 |
|
private function getBody(array $responseArray): ?Body |
78
|
|
|
{ |
79
|
3 |
|
if (isset($responseArray['body'])) { |
80
|
1 |
|
$body = $responseArray['body']; |
81
|
1 |
|
if (\is_array($body)) { |
82
|
|
|
$body = json_encode($body); |
83
|
|
|
} |
84
|
|
|
|
85
|
1 |
|
return BodyHelper::getBodyObject($body); |
86
|
|
|
} |
87
|
|
|
|
88
|
2 |
|
return null; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param array $headers |
93
|
|
|
* |
94
|
|
|
* @throws \InvalidArgumentException |
95
|
|
|
* |
96
|
|
|
* @return \Mcustiel\Phiremock\Domain\Http\HeadersCollection |
97
|
|
|
*/ |
98
|
1 |
|
private function convertHeaders(array $headers): HeadersCollection |
99
|
|
|
{ |
100
|
1 |
|
$headerCollection = new HeadersCollection(); |
101
|
1 |
|
foreach ($headers as $headerName => $headerValue) { |
102
|
1 |
|
$headerCollection->setHeader( |
103
|
1 |
|
new Header( |
104
|
1 |
|
new HeaderName($headerName), |
105
|
1 |
|
new HeaderValue($headerValue) |
106
|
|
|
) |
107
|
|
|
); |
108
|
|
|
} |
109
|
|
|
|
110
|
1 |
|
return $headerCollection; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|