1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace Zend\Soap\Server;
use ReflectionObject;
use Zend\Soap\Exception;
/**
* Wraps WSDL Document/Literal Style service objects to hide SOAP request
* message abstraction from the actual service object.
*
* When using the document/literal SOAP message pattern you end up with one
* object passed to your service methods that contains all the parameters of
* the method. This obviously leads to a problem since Zend\Soap\Wsdl tightly
* couples method parameters to request message parameters.
*
* Example:
*
* <code>
* class MyCalculatorService
* {
* /**
* * @param int $x
* * @param int $y
* * @return int
* *
* public function add($x, $y)
* {
* }
* }
* </code>
*
* The document/literal wrapper pattern would lead php ext/soap to generate a
* single "request" object that contains $x and $y properties. To solve this a
* wrapper service is needed that extracts the properties and delegates a
* proper call to the underlying service.
*
* The input variable from a document/literal SOAP-call to the client
* MyCalculatorServiceClient#add(10, 20) would lead PHP ext/soap to create
* the following request object:
*
* <code>
* $addRequest = new \stdClass;
* $addRequest->x = 10;
* $addRequest->y = 20;
* </code>
*
* This object does not match the signature of the server-side
* MyCalculatorService and lead to failure.
*
* Also the response object in this case is supposed to be an array
* or object with a property "addResult":
*
* <code>
* $addResponse = new \stdClass;
* $addResponse->addResult = 30;
* </code>
*
* To keep your service object code free from this implementation detail
* of SOAP this wrapper service handles the parsing between the formats.
*
* @example
* <code>
* $service = new MyCalculatorService();
* $soap = new \Zend\Soap\Server($wsdlFile);
* $soap->setObject(new \Zend\Soap\Server\DocumentLiteralWrapper($service));
* $soap->handle();
* </code>
*/
class DocumentLiteralWrapper
{
/**
* @var object
*/
protected $object;
/**
* @var ReflectionObject
*/
protected $reflection;
/**
* Pass Service object to the constructor
*
* @param object $object
*/
public function __construct($object)
{
$this->object = $object;
$this->reflection = new ReflectionObject($this->object);
}
/**
* Proxy method that does the heavy document/literal decomposing.
*
* @param string $method
* @param array $args
* @return mixed
*/
public function __call($method, $args)
{
$this->assertOnlyOneArgument($args);
$this->assertServiceDelegateHasMethod($method);
$delegateArgs = $this->parseArguments($method, $args[0]);
$ret = call_user_func_array([$this->object, $method], $delegateArgs);
return $this->getResultMessage($method, $ret);
}
/**
* Parse the document/literal wrapper into arguments to call the real
* service.
*
* @param string $method
* @param object $document
* @return array
* @throws Exception\UnexpectedValueException
*/
protected function parseArguments($method, $document)
{
$reflMethod = $this->reflection->getMethod($method);
$params = [];
foreach ($reflMethod->getParameters() as $param) {
$params[$param->getName()] = $param;
}
$delegateArgs = [];
foreach (get_object_vars($document) as $argName => $argValue) {
if (! isset($params[$argName])) {
throw new Exception\UnexpectedValueException(sprintf(
"Received unknown argument %s which is not an argument to %s::%s",
$argName,
get_class($this->object),
$method
));
}
$delegateArgs[$params[$argName]->getPosition()] = $argValue;
}
return $delegateArgs;
}
/**
* Returns result message content
*
* @param string $method
* @param mixed $ret
* @return array
*/
protected function getResultMessage($method, $ret)
{
return [$method . 'Result' => $ret];
}
/**
* @param string $method
* @throws Exception\BadMethodCallException
*/
protected function assertServiceDelegateHasMethod($method)
{
if (! $this->reflection->hasMethod($method)) {
throw new Exception\BadMethodCallException(sprintf(
"Method %s does not exist on delegate object %s",
$method,
get_class($this->object)
));
}
}
/**
* @param array $args
* @throws Exception\UnexpectedValueException
*/
protected function assertOnlyOneArgument(array $args)
{
if (count($args) != 1) {
throw new Exception\UnexpectedValueException(sprintf(
"Expecting exactly one argument that is the document/literal wrapper, got %d",
count($args)
));
}
}
}