1 | <?php |
||
12 | trait SoapManager |
||
13 | { |
||
14 | use ArrayManager; |
||
15 | |||
16 | /** |
||
17 | * URL of WSDL service to consume. |
||
18 | * |
||
19 | * @var string|null $wsdl |
||
20 | */ |
||
21 | private $wsdl; |
||
22 | /** |
||
23 | * Set of options for SOAP request. |
||
24 | * |
||
25 | * @var array |
||
26 | */ |
||
27 | private $options = []; |
||
28 | /** |
||
29 | * Response of SOAP method. |
||
30 | * |
||
31 | * @var mixed |
||
32 | */ |
||
33 | private $response = []; |
||
34 | /** |
||
35 | * Last SOAP response. |
||
36 | * |
||
37 | * @var string |
||
38 | */ |
||
39 | private $rawResponse = ''; |
||
40 | /** |
||
41 | * The URIs of the namespaces. |
||
42 | * |
||
43 | * @var string[] |
||
44 | */ |
||
45 | private $namespaces = []; |
||
46 | /** |
||
47 | * Latest exception thrown out during SOAP call. |
||
48 | * |
||
49 | * @var null|\SoapFault |
||
50 | */ |
||
51 | private $exception; |
||
52 | |||
53 | /** |
||
54 | * Make SOAP call to a function with params. |
||
55 | * |
||
56 | * @link http://php.net/manual/en/soapclient.getlastrequest.php#example-5896 |
||
57 | * |
||
58 | * @param string $function |
||
59 | * SOAP function name to execute. Use MethodNameIsIgnored if function name is in the XML body. |
||
60 | * @param array $arguments |
||
61 | * Arguments array to pass to soap call function. |
||
62 | */ |
||
63 | public function sendRequest($function, array $arguments) |
||
64 | { |
||
65 | // These values can be easily overridden inside of configuration file. |
||
66 | $this->options += [ |
||
67 | // Important for raw response steps. |
||
68 | 'trace' => 1, |
||
69 | 'exceptions' => true, |
||
70 | 'cache_wsdl' => WSDL_CACHE_NONE, |
||
71 | ]; |
||
72 | |||
73 | try { |
||
74 | $client = new \SoapClient($this->wsdl, $this->options); |
||
75 | |||
76 | $this->response = $client->__soapCall($function, $arguments); |
||
77 | $this->rawResponse = $client->__getLastResponse(); |
||
78 | } catch (\SoapFault $e) { |
||
79 | $this->exception = $e; |
||
80 | } |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * Extracts first value matching provided XPATH expression. |
||
85 | * |
||
86 | * @param string $query |
||
87 | * XPATH expression used to extract value from $this->rawResponse |
||
88 | * |
||
89 | * @return \DOMNode|bool |
||
90 | */ |
||
91 | protected function extractResponseValueMatchingXPATH($query) |
||
92 | { |
||
93 | // @todo: Allow users to ignore namespaces via config or steps. |
||
94 | // @example: $this->rawResponse = str_replace('xmlns=', 'ns=', $this->rawResponse); |
||
95 | $dom = new \DOMDocument(); |
||
96 | $dom->loadXML($this->rawResponse); |
||
97 | $xpath = new \DOMXpath($dom); |
||
98 | |||
99 | foreach ($this->namespaces as $prefix => $uri) { |
||
100 | $xpath->registerNamespace($prefix, $uri); |
||
101 | } |
||
102 | |||
103 | $nodeList = $xpath->query($query); |
||
104 | |||
105 | return $nodeList->length > 0 ? $nodeList->item(0)->nodeValue : false; |
||
106 | } |
||
107 | |||
108 | /** |
||
109 | * Helper to extract a property value from the response. |
||
110 | * |
||
111 | 4 | * @param string $property |
|
112 | * |
||
113 | * @return mixed |
||
114 | 4 | */ |
|
115 | protected function extractResponseProperty($property) |
||
116 | 4 | { |
|
117 | return static::arrayValue(static::objectToArray($this->response), explode('][', $property)); |
||
118 | } |
||
119 | 4 | ||
120 | 4 | /** |
|
121 | * @return null|\SoapFault |
||
122 | 4 | */ |
|
123 | protected function getException() |
||
132 | |||
133 | /** |
||
134 | * @param string $wsdl |
||
135 | */ |
||
136 | protected function setWSDL($wsdl) |
||
148 | |||
149 | /** |
||
150 | * @param array $options |
||
151 | */ |
||
152 | protected function setOptions(array $options = null) |
||
162 | |||
163 | /** |
||
164 | * @param string $option |
||
165 | * @param mixed $value |
||
166 | */ |
||
167 | protected function setOption($option, $value) |
||
168 | { |
||
169 | $this->options[$option] = is_string($value) && defined($value) ? constant($value) : $value; |
||
170 | } |
||
171 | |||
172 | /** |
||
173 | * @param array $namespaces |
||
174 | */ |
||
175 | protected function setNamespaces(array $namespaces = null) |
||
185 | |||
186 | /** |
||
187 | * @param string $prefix |
||
188 | * @param string $uri |
||
189 | */ |
||
190 | protected function setNamespace($prefix, $uri) |
||
194 | |||
195 | /** |
||
196 | * @return mixed |
||
197 | */ |
||
198 | public function getResponse() { |
||
201 | |||
202 | /** |
||
203 | * @return string |
||
204 | */ |
||
205 | public function getRawResponse() { |
||
208 | } |
||
209 |