-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathAbstractSoapClientBase.php
452 lines (408 loc) · 15.4 KB
/
AbstractSoapClientBase.php
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
<?php
declare(strict_types=1);
namespace WsdlToPhp\PackageBase;
use DOMDocument;
use SoapClient;
use SoapFault;
use SoapHeader;
abstract class AbstractSoapClientBase implements SoapClientInterface
{
/**
* SoapClient called to communicate with the actual SOAP Service
* @var SoapClient|null
*/
private ?SoapClient $soapClient = null;
/**
* Contains Soap call result
* @var mixed
*/
private $result;
/**
* Contains last errors
* @var array
*/
private array $lastError = [];
/**
* Contains output headers
* @var array
*/
protected array $outputHeaders = [];
public function __construct(array $wsdlOptions = [])
{
$this->initSoapClient($wsdlOptions);
}
public function getSoapClient(): ?SoapClient
{
return $this->soapClient;
}
public function setSoapClient(SoapClient $soapClient): SoapClient
{
return ($this->soapClient = $soapClient);
}
public function initSoapClient(array $options): void
{
$wsdlOptions = [];
$defaultWsdlOptions = static::getDefaultWsdlOptions();
foreach ($defaultWsdlOptions as $optionName => $optionValue) {
if (array_key_exists($optionName, $options) && !is_null($options[$optionName])) {
$wsdlOptions[str_replace(self::OPTION_PREFIX, '', $optionName)] = $options[$optionName];
} elseif (!is_null($optionValue)) {
$wsdlOptions[str_replace(self::OPTION_PREFIX, '', $optionName)] = $optionValue;
}
}
if (self::canInstantiateSoapClientWithOptions($wsdlOptions)) {
$wsdlUrl = null;
if (array_key_exists(str_replace(self::OPTION_PREFIX, '', self::WSDL_URL), $wsdlOptions)) {
$wsdlUrl = $wsdlOptions[str_replace(self::OPTION_PREFIX, '', self::WSDL_URL)];
unset($wsdlOptions[str_replace(self::OPTION_PREFIX, '', self::WSDL_URL)]);
}
$soapClientClassName = $this->getSoapClientClassName();
$this->setSoapClient(new $soapClientClassName($wsdlUrl, $wsdlOptions));
}
}
/**
* Checks if the provided options are sufficient to instantiate a SoapClient:
* - WSDL-mode : only the WSDL is required
* - non-WSDL-mode : URI and LOCATION are required, WSDL url can be empty then
* @param array $wsdlOptions
* @return bool
*/
protected static function canInstantiateSoapClientWithOptions(array $wsdlOptions): bool
{
return (
array_key_exists(str_replace(self::OPTION_PREFIX, '', self::WSDL_URL), $wsdlOptions) ||
(
array_key_exists(str_replace(self::OPTION_PREFIX, '', self::WSDL_URI), $wsdlOptions) &&
array_key_exists(str_replace(self::OPTION_PREFIX, '', self::WSDL_LOCATION), $wsdlOptions)
)
);
}
/**
* Returns the SoapClient class name to use to create the instance of the SoapClient.
* Be sure that this class inherits from the native PHP SoapClient class and this class has been loaded or can be loaded.
* The goal is to allow the override of the SoapClient without having to modify this generated class.
* Then the overriding SoapClient class can override for example the SoapClient::__doRequest() method if it is needed.
* @param string|null $soapClientClassName
* @return string
*/
public function getSoapClientClassName(?string $soapClientClassName = null): string
{
$className = static::DEFAULT_SOAP_CLIENT_CLASS;
if (!empty($soapClientClassName) && is_subclass_of($soapClientClassName, SoapClient::class)) {
$className = $soapClientClassName;
}
return $className;
}
/**
* Method returning all default SoapClient options values
* @return array
*/
public static function getDefaultWsdlOptions(): array
{
return [
self::WSDL_AUTHENTICATION => null,
self::WSDL_CACHE_WSDL => WSDL_CACHE_NONE,
self::WSDL_CLASSMAP => null,
self::WSDL_COMPRESSION => null,
self::WSDL_CONNECTION_TIMEOUT => null,
self::WSDL_ENCODING => null,
self::WSDL_EXCEPTIONS => true,
self::WSDL_FEATURES => SOAP_SINGLE_ELEMENT_ARRAYS | SOAP_USE_XSI_ARRAY_TYPE,
self::WSDL_LOCAL_CERT => null,
self::WSDL_LOCATION => null,
self::WSDL_LOGIN => null,
self::WSDL_PASSPHRASE => null,
self::WSDL_PASSWORD => null,
self::WSDL_PROXY_HOST => null,
self::WSDL_PROXY_LOGIN => null,
self::WSDL_PROXY_PASSWORD => null,
self::WSDL_PROXY_PORT => null,
self::WSDL_SOAP_VERSION => null,
self::WSDL_SSL_METHOD => null,
self::WSDL_STREAM_CONTEXT => null,
self::WSDL_STYLE => null,
self::WSDL_TRACE => true,
self::WSDL_TYPEMAP => null,
self::WSDL_URL => null,
self::WSDL_URI => null,
self::WSDL_USE => null,
self::WSDL_USER_AGENT => null,
];
}
/**
* Allows to set the SoapClient location to call
* @param string $location
* @return AbstractSoapClientBase
*/
public function setLocation(string $location): self
{
if ($this->getSoapClient() instanceof SoapClient) {
$this->getSoapClient()->__setLocation($location);
}
return $this;
}
/**
* Returns the last request content as a DOMDocument or as a formatted XML String
* @param bool $asDomDocument
* @return DOMDocument|string|null
*/
public function getLastRequest(bool $asDomDocument = false)
{
return $this->getLastXml('__getLastRequest', $asDomDocument);
}
/**
* Returns the last response content as a DOMDocument or as a formatted XML String
* @param bool $asDomDocument
* @return DOMDocument|string|null
*/
public function getLastResponse(bool $asDomDocument = false)
{
return $this->getLastXml('__getLastResponse', $asDomDocument);
}
/**
* @param string $method
* @param bool $asDomDocument
* @return DOMDocument|string|null
*/
protected function getLastXml(string $method, bool $asDomDocument = false)
{
$xml = null;
if ($this->getSoapClient() instanceof SoapClient) {
$xml = static::getFormattedXml($this->getSoapClient()->$method(), $asDomDocument);
}
return $xml;
}
/**
* Returns the last request headers used by the SoapClient object as the original value or an array
* @param bool $asArray allows to get the headers in an associative array
* @return null|string|string[]
*/
public function getLastRequestHeaders(bool $asArray = false)
{
return $this->getLastHeaders('__getLastRequestHeaders', $asArray);
}
/**
* Returns the last response headers used by the SoapClient object as the original value or an array
* @param bool $asArray allows to get the headers in an associative array
* @return null|string|string[]
*/
public function getLastResponseHeaders(bool $asArray = false)
{
return $this->getLastHeaders('__getLastResponseHeaders', $asArray);
}
/**
* @param string $method
* @param bool $asArray allows to get the headers in an associative array
* @return null|string|string[]
*/
protected function getLastHeaders(string $method, bool $asArray)
{
$headers = $this->getSoapClient() instanceof SoapClient ? $this->getSoapClient()->$method() : null;
if (is_string($headers) && $asArray) {
return static::convertStringHeadersToArray($headers);
}
return $headers;
}
/**
* Returns a XML string content as a DOMDocument or as a formatted XML string
* @param string|null $string
* @param bool $asDomDocument
* @return DOMDocument|string|null
*/
public static function getFormattedXml(?string $string, bool $asDomDocument = false)
{
return Utils::getFormattedXml($string, $asDomDocument);
}
/**
* Returns an associative array between the headers name and their respective values
* @param string $headers
* @return string[]
*/
public static function convertStringHeadersToArray(string $headers): array
{
$lines = explode("\r\n", $headers);
$headers = [];
foreach ($lines as $line) {
if (strpos($line, ':')) {
$headerParts = explode(':', $line);
$headers[$headerParts[0]] = trim(implode(':', array_slice($headerParts, 1)));
}
}
return $headers;
}
/**
* Sets a SoapHeader to send
* For more information, please read the online documentation on {@link http://www.php.net/manual/en/class.soapheader.php}
* @param string $namespace SoapHeader namespace
* @param string $name SoapHeader name
* @param mixed $data SoapHeader data
* @param bool $mustUnderstand
* @param string|null $actor
* @return AbstractSoapClientBase
*/
public function setSoapHeader(string $namespace, string $name, $data, bool $mustUnderstand = false, ?string $actor = null): self
{
if ($this->getSoapClient()) {
$defaultHeaders = (isset($this->getSoapClient()->__default_headers) && is_array($this->getSoapClient()->__default_headers)) ? $this->getSoapClient()->__default_headers : [];
foreach ($defaultHeaders as $index => $soapHeader) {
if ($soapHeader->name === $name) {
unset($defaultHeaders[$index]);
break;
}
}
$this->getSoapClient()->__setSoapheaders(null);
if (!empty($actor)) {
array_push($defaultHeaders, new SoapHeader($namespace, $name, $data, $mustUnderstand, $actor));
} else {
array_push($defaultHeaders, new SoapHeader($namespace, $name, $data, $mustUnderstand));
}
$this->getSoapClient()->__setSoapheaders($defaultHeaders);
}
return $this;
}
/**
* Sets the SoapClient Stream context HTTP Header name according to its value
* If a context already exists, it tries to modify it
* It the context does not exist, it then creates it with the header name and its value
* @param string $headerName
* @param mixed $headerValue
* @return bool
*/
public function setHttpHeader(string $headerName, $headerValue): bool
{
$state = false;
if ($this->getSoapClient() && !empty($headerName)) {
$streamContext = $this->getStreamContext();
if ($streamContext === null) {
$options = [];
$options['http'] = [];
$options['http']['header'] = '';
} else {
$options = stream_context_get_options($streamContext);
if (!array_key_exists('http', $options) || !is_array($options['http'])) {
$options['http'] = [];
$options['http']['header'] = '';
} elseif (!array_key_exists('header', $options['http'])) {
$options['http']['header'] = '';
}
}
if (count($options) && array_key_exists('http', $options) && is_array($options['http']) && array_key_exists('header', $options['http']) && is_string($options['http']['header'])) {
$lines = explode("\r\n", $options['http']['header']);
/**
* Ensure there is only one header entry for this header name
*/
$newLines = [];
foreach ($lines as $line) {
if (!empty($line) && strpos($line, $headerName) === false) {
array_push($newLines, $line);
}
}
/**
* Add new header entry
*/
array_push($newLines, "$headerName: $headerValue");
/**
* Set the context http header option
*/
$options['http']['header'] = implode("\r\n", $newLines);
/**
* Create context if it does not exist
*/
if ($streamContext === null) {
$state = is_resource($this->getSoapClient()->_stream_context = stream_context_create($options));
} else {
/**
* Set the new context http header option
*/
$state = stream_context_set_option($this->getSoapClient()->_stream_context, 'http', 'header', $options['http']['header']);
}
}
}
return $state;
}
/**
* Returns current SoapClient::_stream_context resource or null
* @return resource|null
*/
public function getStreamContext()
{
return ($this->getSoapClient() && isset($this->getSoapClient()->_stream_context) && is_resource($this->getSoapClient()->_stream_context)) ? $this->getSoapClient()->_stream_context : null;
}
/**
* Returns current SoapClient::_stream_context resource options or empty array
* @return array
*/
public function getStreamContextOptions(): array
{
$options = [];
$context = $this->getStreamContext();
if ($context !== null) {
$options = stream_context_get_options($context);
if (isset($options['http']['header']) && is_string($options['http']['header'])) {
$options['http']['header'] = array_filter(array_map('trim', explode(PHP_EOL, $options['http']['header'])));
}
}
return $options;
}
/**
* Method returning last errors occurred during the calls
* @return array
*/
public function getLastError(): array
{
return $this->lastError;
}
/**
* Method saving the last error returned by the SoapClient
* @param string $methodName the method called when the error occurred
* @param SoapFault $soapFault the fault
* @return AbstractSoapClientBase
*/
public function saveLastError(string $methodName, SoapFault $soapFault): SoapClientInterface
{
$this->lastError[$methodName] = $soapFault;
return $this;
}
/**
* Method getting the last error for a certain method
* @param string $methodName method name to get error from
* @return SoapFault|null
*/
public function getLastErrorForMethod(string $methodName): ?SoapFault
{
return array_key_exists($methodName, $this->lastError) ? $this->lastError[$methodName] : null;
}
/**
* Method returning current result from Soap call
* @return mixed
*/
public function getResult()
{
return $this->result;
}
/**
* Method setting current result from Soap call
* @param mixed $result
* @return AbstractSoapClientBase
*/
public function setResult($result): SoapClientInterface
{
$this->result = $result;
return $this;
}
/**
* @return array
*/
public function getOutputHeaders(): array
{
return $this->outputHeaders;
}
/**
* Default string representation of current object. Don't want to expose any sensible data
* @return string
*/
public function __toString(): string
{
return get_called_class();
}
}