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\Common\Utils\V1\RequestConditionToArrayConverter as RequestConditionToArrayConverterV1; |
22
|
|
|
use Mcustiel\Phiremock\Domain\Condition\MatchersEnum; |
23
|
|
|
use Mcustiel\Phiremock\Domain\Conditions; |
24
|
|
|
|
25
|
|
|
class RequestConditionToArrayConverter extends RequestConditionToArrayConverterV1 |
26
|
15 |
|
{ |
27
|
|
|
public function convert(Conditions $request): array |
28
|
15 |
|
{ |
29
|
|
|
$requestArray = []; |
30
|
15 |
|
|
31
|
15 |
|
$this->convertScenarioState($request, $requestArray); |
32
|
15 |
|
$this->convertMethod($request, $requestArray); |
33
|
15 |
|
$this->convertUrl($request, $requestArray); |
34
|
15 |
|
$this->convertBody($request, $requestArray); |
35
|
15 |
|
$this->convertHeaders($request, $requestArray); |
36
|
|
|
$this->convertFormData($request, $requestArray); |
37
|
15 |
|
$this->convertJsonPath($request, $requestArray); |
38
|
|
|
|
39
|
|
|
return $requestArray; |
40
|
15 |
|
} |
41
|
|
|
|
42
|
15 |
|
protected function convertMethod(Conditions $request, array &$requestArray): void |
43
|
15 |
|
{ |
44
|
9 |
|
$method = $request->getMethod(); |
45
|
|
|
$requestArray['method'] = null === $method ? null : [ |
46
|
15 |
|
$method->getMatcher()->getName() => $method->getValue()->asString(), |
47
|
|
|
]; |
48
|
15 |
|
} |
49
|
|
|
|
50
|
15 |
|
private function convertScenarioState(Conditions $request, array &$requestArray): void |
51
|
3 |
|
{ |
52
|
12 |
|
$requestArray['scenarioStateIs'] = $request->hasScenarioState() |
53
|
15 |
|
? $request->getScenarioState()->asString() |
54
|
|
|
: null; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
protected function convertJsonPath(Conditions $request, array &$requestArray): void |
58
|
|
|
{ |
59
|
|
|
$jsonPaths = $request->getJsonPath(); |
60
|
|
|
if ($jsonPaths === null) { |
61
|
|
|
$requestArray['jsonPath'] = null; |
62
|
|
|
} else { |
63
|
|
|
$pathsArray = []; |
64
|
|
|
/** @var JsonPathName $pathName */ |
65
|
|
|
/** @var JsonPathCondition $pathCondition */ |
66
|
|
|
foreach ($jsonPaths as $pathName => $pathCondition) { |
67
|
|
|
$pathsArray[$pathName->asString()] = [ |
68
|
|
|
$pathCondition->getMatcher()->getName() => |
69
|
|
|
$pathCondition->getMatcher()->getName() === MatchersEnum::EQUAL_TO |
70
|
|
|
? $pathCondition->getValue()->get() |
71
|
|
|
: $pathCondition->getValue()->asString(), |
72
|
|
|
]; |
73
|
|
|
} |
74
|
|
|
$requestArray['jsonPath'] = $pathsArray; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|