|
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\V2; |
|
20
|
|
|
|
|
21
|
|
|
use Mcustiel\Phiremock\Domain\Http\Header; |
|
22
|
|
|
use Mcustiel\Phiremock\Domain\HttpResponse; |
|
23
|
|
|
use Mcustiel\Phiremock\Domain\Response; |
|
24
|
|
|
|
|
25
|
|
|
class HttpResponseToArrayConverter extends ResponseToArrayConverter |
|
26
|
|
|
{ |
|
27
|
15 |
|
public function convert(Response $response): array |
|
28
|
|
|
{ |
|
29
|
|
|
/** @var HttpResponse $response */ |
|
30
|
15 |
|
$responseArray = []; |
|
31
|
15 |
|
$responseArray['statusCode'] = $response->getStatusCode()->asInt(); |
|
32
|
15 |
|
$body = $response->getBody(); |
|
33
|
15 |
|
$responseArray['body'] = $body === null ? null : $body->asString(); |
|
34
|
15 |
|
$headers = $response->getHeaders(); |
|
35
|
15 |
|
if ($headers && !$headers->isEmpty()) { |
|
36
|
3 |
|
$responseArray['headers'] = $this->convertHeaders($response); |
|
37
|
|
|
} else { |
|
38
|
12 |
|
$responseArray['headers'] = null; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
15 |
|
return array_merge(parent::convert($response), ['response' => $responseArray]); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
3 |
|
private function convertHeaders(HttpResponse $response): array |
|
45
|
|
|
{ |
|
46
|
3 |
|
$headers = $response->getHeaders(); |
|
47
|
3 |
|
$headersArray = []; |
|
48
|
|
|
/** @var Header $header */ |
|
49
|
3 |
|
foreach ($headers as $header) { |
|
50
|
3 |
|
$headersArray[$header->getName()->asString()] = $header->getValue()->asString(); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
3 |
|
return $headersArray; |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
|