1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Afonso\Soapi; |
4
|
|
|
|
5
|
|
|
use League\Pipeline\Pipeline; |
6
|
|
|
use SoapClient as NativeSoapClient; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* A enhanced, drop-in replacement for PHP's native SoapClient class. |
10
|
|
|
* |
11
|
|
|
* This client includes input and output pipelines to aribtrarily modify |
12
|
|
|
* requests and responses. |
13
|
|
|
*/ |
14
|
|
|
class SoapClient extends NativeSoapClient |
15
|
|
|
{ |
16
|
|
|
use ProcessesWithPipelines; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Creates and returns a new SoapClient with empty inbound and outbound |
20
|
|
|
* pipelines. |
21
|
|
|
* |
22
|
|
|
* @param string|null $wsdl |
23
|
|
|
* @param array $options |
24
|
|
|
*/ |
25
|
|
|
public function __construct($wsdl, array $options = array()) |
26
|
|
|
{ |
27
|
|
|
parent::__construct($wsdl, $options); |
|
|
|
|
28
|
|
|
|
29
|
|
|
// initialize in and outbound pipelines |
30
|
|
|
$this->inboundPipeline = new Pipeline(); |
31
|
|
|
$this->outboundPipeline = new Pipeline(); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Performs a SOAP request. |
36
|
|
|
* |
37
|
|
|
* This function delegates on PHP's native SoapClient __doRequest(), but |
38
|
|
|
* processes the payload through the oubtound and inbound pipelines. This |
39
|
|
|
* allows for arbitrary manipulation of requests and responses, such as |
40
|
|
|
* encryption or logging. |
41
|
|
|
* |
42
|
|
|
* @see http://php.net/manual/en/soapclient.dorequest.php |
43
|
|
|
* @param string $soapRequest |
44
|
|
|
* @param string $location |
45
|
|
|
* @param string $action |
46
|
|
|
* @param int $version |
47
|
|
|
* @param int $oneWay |
48
|
|
|
*/ |
49
|
|
|
public function __doRequest($soapRequest, $location, $action, $version, $oneWay = 0) |
50
|
|
|
{ |
51
|
|
|
/* |
52
|
|
|
* Run the request XML through the outbound pipeline. |
53
|
|
|
*/ |
54
|
|
|
$soapRequest = $this->outboundPipeline->process($soapRequest); |
55
|
|
|
|
56
|
|
|
/* |
57
|
|
|
* Do the actual request. |
58
|
|
|
*/ |
59
|
|
|
$soapResponse = parent::__doRequest($soapRequest, $location, $action, $version, $oneWay); |
60
|
|
|
|
61
|
|
|
/* |
62
|
|
|
* Then run the response through the inbound pipeline. |
63
|
|
|
*/ |
64
|
|
|
$soapResponse = $this->inboundPipeline->process($soapResponse); |
65
|
|
|
return $soapResponse; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: