1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* amadeus-ws-client |
4
|
|
|
* |
5
|
|
|
* Copyright 2015 Amadeus Benelux NV |
6
|
|
|
* |
7
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
8
|
|
|
* you may not use this file except in compliance with the License. |
9
|
|
|
* You may obtain a copy of the License at |
10
|
|
|
* |
11
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
12
|
|
|
* |
13
|
|
|
* Unless required by applicable law or agreed to in writing, software |
14
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
15
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
16
|
|
|
* See the License for the specific language governing permissions and |
17
|
|
|
* limitations under the License. |
18
|
|
|
* |
19
|
|
|
* @package Amadeus |
20
|
|
|
* @license https://opensource.org/licenses/Apache-2.0 Apache 2.0 |
21
|
|
|
*/ |
22
|
|
|
|
23
|
|
|
namespace Amadeus\Client; |
24
|
|
|
|
25
|
|
|
use Psr\Log; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Amadeus Web Services SoapClient class |
29
|
|
|
* |
30
|
|
|
* This SoapClient will perform an XSLT transformation on the request to be sent to the amadeus server: |
31
|
|
|
* it will remove any empty XML tags except for some specific tags that may be required to be present and empty. |
32
|
|
|
* |
33
|
|
|
* If you do not remove empty XML tags, you will get SoapFaults back from the server when unexpected empty elements |
34
|
|
|
* are present in your request. |
35
|
|
|
* |
36
|
|
|
* @package Amadeus\Client |
37
|
|
|
* @author Dieter Devlieghere <[email protected]> |
38
|
|
|
*/ |
39
|
|
|
class SoapClient extends \SoapClient implements Log\LoggerAwareInterface |
40
|
|
|
{ |
41
|
|
|
use Log\LoggerAwareTrait; |
42
|
|
|
|
43
|
|
|
const REMOVE_EMPTY_XSLT_LOCATION = 'SoapClient/removeempty.xslt'; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Construct a new SoapClient |
47
|
|
|
* |
48
|
|
|
* @param string $wsdl Location of WSDL file |
49
|
|
|
* @param array $options initialisation options |
50
|
|
|
* @param Log\LoggerInterface|null $logger Error logging object |
51
|
|
|
*/ |
52
|
15 |
|
public function __construct($wsdl, $options, Log\LoggerInterface $logger = null) |
53
|
|
|
{ |
54
|
15 |
|
if (!($logger instanceof Log\LoggerInterface)) { |
55
|
9 |
|
$logger = new Log\NullLogger(); |
56
|
|
|
} |
57
|
15 |
|
$this->setLogger($logger); |
58
|
|
|
|
59
|
15 |
|
parent::__construct($wsdl, $options); |
|
|
|
|
60
|
15 |
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* __doRequest override of SoapClient |
64
|
|
|
* |
65
|
|
|
* @param string $request The XML SOAP request. |
66
|
|
|
* @param string $location The URL to request. |
67
|
|
|
* @param string $action The SOAP action. |
68
|
|
|
* @param int $version The SOAP version. |
69
|
|
|
* @param int|null $oneWay |
70
|
|
|
* @uses parent::__doRequest |
71
|
|
|
* @return string The XML SOAP response. |
72
|
|
|
* @throws Exception When PHP XSL extension is not enabled or WSDL file isn't readable. |
73
|
|
|
*/ |
74
|
|
|
public function __doRequest($request, $location, $action, $version, $oneWay = null) |
75
|
|
|
{ |
76
|
|
|
if (!extension_loaded('xsl')) { |
77
|
|
|
throw new Exception('PHP XSL extension is not enabled.'); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$newRequest = $this->transformIncomingRequest($request); |
81
|
|
|
|
82
|
|
|
return parent::__doRequest($newRequest, $location, $action, $version, $oneWay); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param string $request |
87
|
|
|
* @return string |
88
|
|
|
* @throws Exception when XSLT file isn't readable |
89
|
|
|
*/ |
90
|
9 |
|
protected function transformIncomingRequest($request) |
91
|
|
|
{ |
92
|
9 |
|
$newRequest = null; |
|
|
|
|
93
|
|
|
|
94
|
9 |
|
$xsltFile = dirname(__FILE__).DIRECTORY_SEPARATOR.self::REMOVE_EMPTY_XSLT_LOCATION; |
95
|
9 |
|
if (!is_readable($xsltFile)) { |
96
|
|
|
throw new Exception('XSLT file "'.$xsltFile.'" is not readable!'); |
97
|
|
|
} |
98
|
|
|
|
99
|
9 |
|
$dom = new \DOMDocument('1.0', 'UTF-8'); |
100
|
9 |
|
$dom->loadXML($request); |
101
|
9 |
|
$xslt = new \DOMDocument('1.0', 'UTF-8'); |
102
|
|
|
|
103
|
9 |
|
$xslt->load($xsltFile); |
104
|
|
|
|
105
|
9 |
|
$processor = new \XSLTProcessor(); |
106
|
9 |
|
$processor->importStylesheet($xslt); |
107
|
9 |
|
$transform = $processor->transformToXml($dom); |
108
|
9 |
|
if ($transform === false) { |
109
|
|
|
//On transform error: usually when modifying the XSLT transformation incorrectly... |
110
|
|
|
$this->logger->log( |
111
|
|
|
Log\LogLevel::ERROR, |
112
|
|
|
__METHOD__."__doRequest(): XSLTProcessor::transformToXml " |
113
|
|
|
. "returned FALSE: could not perform transformation!!" |
114
|
|
|
); |
115
|
|
|
$newRequest = $request; |
116
|
|
|
} else { |
117
|
9 |
|
$newDom = new \DOMDocument('1.0', 'UTF-8'); |
118
|
9 |
|
$newDom->preserveWhiteSpace = false; |
119
|
9 |
|
$newDom->loadXML($transform); |
120
|
|
|
|
121
|
9 |
|
$newRequest = $newDom->saveXML(); |
122
|
|
|
} |
123
|
|
|
|
124
|
9 |
|
unset($processor, $xslt, $dom, $transform); |
125
|
|
|
|
126
|
9 |
|
return $newRequest; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
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: