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\RequestConditionToArrayConverter as RequestConditionToArrayConverterInterface; |
22
|
|
|
use Mcustiel\Phiremock\Domain\Condition\Conditions\FormFieldCondition; |
23
|
|
|
use Mcustiel\Phiremock\Domain\Condition\Conditions\HeaderCondition; |
24
|
|
|
use Mcustiel\Phiremock\Domain\Conditions; |
25
|
|
|
use Mcustiel\Phiremock\Domain\Http\FormFieldName; |
26
|
|
|
use Mcustiel\Phiremock\Domain\Http\HeaderName; |
27
|
|
|
|
28
|
|
|
class RequestConditionToArrayConverter implements RequestConditionToArrayConverterInterface |
29
|
|
|
{ |
30
|
3 |
|
public function convert(Conditions $request): array |
31
|
|
|
{ |
32
|
3 |
|
$requestArray = []; |
33
|
|
|
|
34
|
3 |
|
$this->convertMethod($request, $requestArray); |
35
|
3 |
|
$this->convertUrl($request, $requestArray); |
36
|
3 |
|
$this->convertBody($request, $requestArray); |
37
|
3 |
|
$this->convertHeaders($request, $requestArray); |
38
|
3 |
|
$this->convertFormData($request, $requestArray); |
39
|
|
|
|
40
|
3 |
|
return $requestArray; |
41
|
|
|
} |
42
|
|
|
|
43
|
8 |
|
protected function convertHeaders(Conditions $request, array &$requestArray): void |
44
|
|
|
{ |
45
|
8 |
|
$headers = $request->getHeaders(); |
46
|
8 |
|
if ($headers === null) { |
47
|
6 |
|
$requestArray['headers'] = null; |
48
|
|
|
} else { |
49
|
2 |
|
$headersArray = []; |
50
|
|
|
/** @var HeaderName $headerName */ |
51
|
|
|
/** @var HeaderCondition $headerCondition */ |
52
|
2 |
|
foreach ($headers as $headerName => $headerCondition) { |
53
|
2 |
|
$headersArray[$headerName->asString()] = [ |
54
|
2 |
|
$headerCondition->getMatcher()->getName() => $headerCondition->getValue()->asString(), |
55
|
|
|
]; |
56
|
|
|
} |
57
|
2 |
|
$requestArray['headers'] = $headersArray; |
58
|
|
|
} |
59
|
8 |
|
} |
60
|
|
|
|
61
|
8 |
|
protected function convertFormData(Conditions $request, array &$requestArray): void |
62
|
|
|
{ |
63
|
8 |
|
$formFields = $request->getFormFields(); |
64
|
8 |
|
if ($formFields === null) { |
65
|
6 |
|
$requestArray['formData'] = null; |
66
|
|
|
} else { |
67
|
2 |
|
$fieldsArray = []; |
68
|
|
|
/** @var FormFieldName $fieldName */ |
69
|
|
|
/** @var FormFieldCondition $fieldCondition */ |
70
|
2 |
|
foreach ($formFields as $fieldName => $fieldCondition) { |
71
|
2 |
|
$fieldsArray[$fieldName->asString()] = [ |
72
|
2 |
|
$fieldCondition->getMatcher()->getName() => $fieldCondition->getValue()->asString(), |
73
|
|
|
]; |
74
|
|
|
} |
75
|
2 |
|
$requestArray['formData'] = $fieldsArray; |
76
|
|
|
} |
77
|
8 |
|
} |
78
|
|
|
|
79
|
8 |
|
protected function convertBody(Conditions $request, array &$requestArray): void |
80
|
|
|
{ |
81
|
8 |
|
$body = $request->getBody(); |
82
|
8 |
|
$requestArray['body'] = null === $body ? null : [ |
83
|
4 |
|
$body->getMatcher()->getName() => $body->getValue()->asString(), |
84
|
|
|
]; |
85
|
8 |
|
} |
86
|
|
|
|
87
|
8 |
|
protected function convertUrl(Conditions $request, array &$requestArray): void |
88
|
|
|
{ |
89
|
8 |
|
$url = $request->getUrl(); |
90
|
8 |
|
$requestArray['url'] = null === $url ? null : [ |
91
|
2 |
|
$url->getMatcher()->getName() => $url->getValue()->asString(), |
92
|
|
|
]; |
93
|
8 |
|
} |
94
|
|
|
|
95
|
3 |
|
protected function convertMethod(Conditions $request, array &$requestArray): void |
96
|
|
|
{ |
97
|
3 |
|
$method = $request->getMethod(); |
98
|
3 |
|
$requestArray['method'] = null === $method ? null : $method->getValue()->asString(); |
99
|
3 |
|
} |
100
|
|
|
} |
101
|
|
|
|