1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace WsdlToPhp\PackageGenerator\File; |
6
|
|
|
|
7
|
|
|
use WsdlToPhp\PhpGenerator\Element\PhpFunctionParameter; |
8
|
|
|
use WsdlToPhp\PhpGenerator\Element\PhpMethod; |
9
|
|
|
|
10
|
|
|
final class Operation extends AbstractOperation |
11
|
|
|
{ |
12
|
46 |
|
public function getMainMethod(): PhpMethod |
13
|
|
|
{ |
14
|
46 |
|
$phpMethod = new PhpMethod($this->getMethod()->getMethodName(), []); |
15
|
46 |
|
$this |
16
|
46 |
|
->defineParameters($phpMethod) |
17
|
46 |
|
->defineBody($phpMethod) |
18
|
46 |
|
; |
19
|
|
|
|
20
|
46 |
|
return $phpMethod; |
21
|
|
|
} |
22
|
|
|
|
23
|
46 |
|
protected function defineParameters(PhpMethod $method): self |
24
|
|
|
{ |
25
|
46 |
|
return $this->defineParametersFromArray($method)->defineParametersFromModel($method)->defineParametersFromString($method); |
26
|
|
|
} |
27
|
|
|
|
28
|
46 |
|
protected function defineParametersFromArray(PhpMethod $method): self |
29
|
|
|
{ |
30
|
46 |
|
if ($this->isParameterTypeAnArray()) { |
31
|
42 |
|
$parameters = []; |
32
|
42 |
|
foreach ($this->getParameterTypeArrayTypes(true) as $parameterName => $parameterType) { |
33
|
42 |
|
$parameters[] = $this->getMethodParameter($this->getParameterName($parameterName), $parameterType); |
34
|
|
|
} |
35
|
42 |
|
$method->setParameters($parameters); |
36
|
|
|
} |
37
|
|
|
|
38
|
46 |
|
return $this; |
39
|
|
|
} |
40
|
|
|
|
41
|
46 |
|
protected function defineParametersFromModel(PhpMethod $method): self |
42
|
|
|
{ |
43
|
46 |
|
if ($this->isParameterTypeAModel()) { |
44
|
2 |
|
if ($this->getParameterTypeModel()->getAttributes(true, true)->count() > 0) { |
45
|
2 |
|
$method->setParameters([ |
46
|
2 |
|
$this->getMethodParameter($this->getParameterName($this->getParameterTypeModel()->getPackagedName()), $this->getParameterTypeModel()->getPackagedName(true)), |
47
|
2 |
|
]); |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
46 |
|
return $this; |
52
|
|
|
} |
53
|
|
|
|
54
|
46 |
|
protected function defineParametersFromString(PhpMethod $method): self |
55
|
|
|
{ |
56
|
46 |
|
if ($this->isParameterTypeAString() && !$this->isParameterTypeAModel()) { |
57
|
2 |
|
$method->setParameters([ |
58
|
2 |
|
$this->getMethodParameter($this->getParameterName($this->getMethod()->getParameterType())), |
59
|
2 |
|
]); |
60
|
|
|
} |
61
|
|
|
|
62
|
46 |
|
return $this; |
63
|
|
|
} |
64
|
|
|
|
65
|
46 |
|
protected function defineBody(PhpMethod $method): self |
66
|
|
|
{ |
67
|
46 |
|
$resultVariableName = sprintf('$result%s', ucfirst($this->getMethod()->getCleanName(false))); |
68
|
46 |
|
$method |
69
|
46 |
|
->addChild('try {') |
70
|
46 |
|
->addChild($method->getIndentedString(sprintf('$this->setResult(%s = $this->getSoapClient()->%s%s));', $resultVariableName, $this->getSoapCallName(), $this->getOperationCallParameters($method)), 1)) |
71
|
46 |
|
->addChild('') |
72
|
46 |
|
->addChild($method->getIndentedString(sprintf('return %s;', $resultVariableName), 1)) |
73
|
46 |
|
->addChild('} catch (SoapFault $soapFault) {') |
74
|
46 |
|
->addChild($method->getIndentedString('$this->saveLastError(__METHOD__, $soapFault);', 1)) |
75
|
46 |
|
->addChild('') |
76
|
46 |
|
->addChild($method->getIndentedString('return false;', 1)) |
77
|
46 |
|
->addChild('}') |
78
|
46 |
|
; |
79
|
|
|
|
80
|
46 |
|
return $this; |
81
|
|
|
} |
82
|
|
|
|
83
|
46 |
|
protected function getSoapCallName(): string |
84
|
|
|
{ |
85
|
46 |
|
return sprintf('%s(\'%s\'%s', self::SOAP_CALL_NAME, $this->getMethod()->getName(), $this->getOperationCallParametersStarting()); |
86
|
|
|
} |
87
|
|
|
|
88
|
46 |
|
protected function getOperationCallParameters(PhpMethod $method): string |
89
|
|
|
{ |
90
|
46 |
|
$parameters = []; |
91
|
46 |
|
foreach ($method->getParameters() as $parameter) { |
92
|
46 |
|
if (!$parameter instanceof PhpFunctionParameter) { |
93
|
|
|
continue; |
94
|
|
|
} |
95
|
|
|
|
96
|
46 |
|
$parameters[] = $this->getOperationCallParameterName($parameter, $method); |
97
|
|
|
} |
98
|
|
|
|
99
|
46 |
|
return sprintf('%s%s, [], [], $this->outputHeaders', implode('', $parameters), $this->isParameterTypeEmpty() ? '' : PhpMethod::BREAK_LINE_CHAR.']'); |
100
|
|
|
} |
101
|
|
|
|
102
|
46 |
|
protected function getOperationCallParametersStarting(): string |
103
|
|
|
{ |
104
|
46 |
|
return $this->isParameterTypeAnArray() ? ', [' : ($this->isParameterTypeEmpty() ? ', []' : ', ['); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
protected function getOperationCallParametersEnding(): string |
108
|
|
|
{ |
109
|
|
|
return sprintf('%s]', PhpMethod::BREAK_LINE_CHAR); |
110
|
|
|
} |
111
|
|
|
|
112
|
46 |
|
protected function getOperationCallParameterName(PhpFunctionParameter $parameter, PhpMethod $method): string |
113
|
|
|
{ |
114
|
46 |
|
$cloneParameter = clone $parameter; |
115
|
46 |
|
$cloneParameter->setType(null); |
116
|
|
|
|
117
|
46 |
|
return sprintf('%s%s', PhpMethod::BREAK_LINE_CHAR, $method->getIndentedString(sprintf('%s,', $cloneParameter->getPhpDeclaration()), 1)); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|