forked from mikaelcom/WsdlToPhp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WsdlToPhpFunction.php
298 lines (298 loc) · 11.2 KB
/
WsdlToPhpFunction.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
<?php
/**
* File for WsdlToPhpFunction
* @package WsdlToPhpGenerator
* @date 19/12/2012
*/
/**
* Class WsdlToPhpFunction stands for an available operation described in the WSDL
* @package WsdlToPhpGenerator
* @date 19/12/2012
*/
class WsdlToPhpFunction extends WsdlToPhpModel
{
/**
* Type of the parameter for the operation
* @var string
*/
private $parameterType;
/**
* Type of the return value for the operation
* @var string
*/
private $returnType;
/**
* Main constructor
* @see WsdlToPhpModel::__construct()
* @uses WsdlToPhpFunction::setParameterType()
* @uses WsdlToPhpFunction::setReturnType()
* @uses WsdlToPhpModel::setOwner()
* @param string $_name the function name
* @param string $_parameterType the type/name of the parameter
* @param string $_returnType the type/name of the return value
* @param WsdlToPhpService $_wsdlToPhpService defines the struct which owns this value
* @return WsdlToPhpFunction
*/
public function __construct($_name,$_parameterType,$_returnType,WsdlToPhpService $_wsdlToPhpService)
{
parent::__construct($_name);
$this->setParameterType($_parameterType);
$this->setReturnType($_returnType);
$this->setOwner($_wsdlToPhpService);
}
/**
* Returns the commment lines for this function
* @see WsdlToPhpModel::getComment()
* @uses WsdlToPhpStructAttribute::getGetterName()
* @uses WsdlToPhpFunction::getParameterType()
* @uses WsdlToPhpFunction::getReturnType()
* @uses WsdlToPhpStruct::getAttributes()
* @uses WsdlToPhpStruct::getIsStruct()
* @uses WsdlToPhpStruct::getIsRestriction()
* @uses WsdlToPhpModel::getPackagedName()
* @uses WsdlToPhpModel::getModelByName()
* @uses WsdlToPhpModel::addMetaComment()
* @uses WsdlToPhpModel::getDocumentation()
* @uses WsdlToPhpModel::getGenericWsdlClassName()
* @uses WsdlToPhpModel::cleanString()
* @param int $_part comment part
* @return array
*/
public function getComment($_part = '')
{
$comments = array();
array_push($comments,'Method to call the operation originally named ' . $this->getName());
if($this->getDocumentation() != '')
array_push($comments,'Documentation : ' . $this->getDocumentation());
$this->addMetaComment($comments,false,true);
/**
* @Uses and @Param
*/
if(WsdlToPhpGenerator::getOptionGenerateWsdlClassFile())
{
array_push($comments,'@uses ' . self::getGenericWsdlClassName() . '::getSoapClient()');
array_push($comments,'@uses ' . self::getGenericWsdlClassName() . '::setResult()');
array_push($comments,'@uses ' . self::getGenericWsdlClassName() . '::getResult()');
array_push($comments,'@uses ' . self::getGenericWsdlClassName() . '::saveLastError()');
}
if(is_string($this->getParameterType()))
{
$model = self::getModelByName($this->getParameterType());
if($model && $model->getIsStruct() && !$model->getIsRestriction())
{
$attributes = $model->getAttributes(true);
if(count($attributes))
{
foreach($attributes as $attributeData)
array_push($comments,'@uses ' . $attributeData['model']->getPackagedName() . '::' . $attributeData['attribute']->getGetterName() . '()');
array_push($comments,'@param ' . $model->getPackagedName() . ' $_' . lcfirst($model->getPackagedName()));
}
}
else
array_push($comments,'@param ' . $this->getParameterType() . ' $_' . lcfirst($this->getParameterType()));
}
elseif(is_array($this->getParameterType()))
{
foreach($this->getParameterType() as $parameterName=>$parameterType)
{
$model = self::getModelByName($parameterType);
if($model && $model->getIsStruct() && !$model->getIsRestriction())
array_push($comments,'@param ' . $model->getPackagedName() . ' $_' . lcfirst($model->getPackagedName()));
else
array_push($comments,'@param ' . $parameterType . ' $_' . lcfirst(self::cleanString($parameterName)));
}
}
/**
* @Return
*/
$model = self::getModelByName($this->getReturnType());
if($model && $model->getIsStruct() && !$model->getIsRestriction())
array_push($comments,'@return ' . $model->getPackagedName());
else
array_push($comments,'@return ' . $this->getReturnType());
unset($model);
return $comments;
}
/**
* Set the function body
* @uses WsdlToPhpModel::getName()
* @uses WsdlToPhpModel::getModelByName()
* @uses WsdlToPhpModel::getCleanName()
* @uses WsdlToPhpModel::nameIsClean()
* @uses WsdlToPhpModel::cleanString()
* @uses WsdlToPhpModel::uniqueName()
* @uses WsdlToPhpModel::getOwner()
* @uses WsdlToPhpModel::replaceReservedPhpKeyword()
* @uses WsdlToPhpFunction::getParameterType()
* @uses WsdlToPhpStruct::getAttributes()
* @uses WsdlToPhpStruct::getIsStruct()
* @uses WsdlToPhpStructAttribute::getGetterName()
* @uses WsdlToPhpGenerator::getOptionResponseAsWsdlObject()
* @uses WsdlToPhpGenerator::getOptionSendParametersAsArray()
* @uses WsdlToPhpGenerator::getOptionSendArrayAsParameter()
* @uses WsdlToPhpGenerator::getOptionSendArrayAsParameter()
* @param array
* @return void
*/
public function getBody(&$_body)
{
$parameterModel = self::getModelByName($this->getParameterType());
$parameterModel = ($parameterModel && $parameterModel->getIsStruct() && !$parameterModel->getIsRestriction())?$parameterModel:null;
$returnModel = self::getModelByName($this->getReturnType());
$returnModel = ($returnModel && $returnModel->getIsStruct() && !$returnModel->getIsRestriction())?$returnModel:null;
if($parameterModel)
{
if(count($parameterModel->getAttributes(true)))
{
$parameterName = '$_' . lcfirst($parameterModel->getPackagedName());
$parameter = $parameterModel->getPackagedName() . ' ' . $parameterName;
}
else
$parameterName = $parameter = '';
}
elseif(is_string($this->getParameterType()))
$parameterName = $parameter = '$_' . lcfirst(self::cleanString($this->getParameterType()));
elseif(is_array($this->getParameterType()))
{
$parameters = array();
foreach($this->getParameterType() as $parameterName=>$parameterType)
{
$model = self::getModelByName($parameterType);
if($model && $model->getIsStruct() && !$model->getIsRestriction())
$parameterName = $model->getPackagedName();
else
$parameterName = self::cleanString($parameterName);
array_push($parameters,(($model && $model->getIsStruct() && !$model->getIsRestriction())?$model->getPackagedName() . ' ':'') . '$_' . lcfirst($parameterName));
}
$parameterName = $parameter = implode(',',$parameters);
}
else
$parameterName = $parameter = '';
array_push($_body,'public function ' . self::uniqueName(self::replaceReservedPhpKeyword($this->getCleanName(),$this->getName()),$this->getOwner()->getName()) . '(' . $parameter . ')');
array_push($_body,'{');
array_push($_body,'try');
array_push($_body,'{');
/**
* Response
*/
$responseAsObjStart = ((WsdlToPhpGenerator::getOptionResponseAsWsdlObject() && $returnModel)?'new ' . $returnModel->getPackagedName() . '(':'');
$responseAsObjEnd = ((WsdlToPhpGenerator::getOptionResponseAsWsdlObject() && $returnModel)?')':'');
/**
* Soap parameters
*/
if($parameterModel)
{
$soapParametersEnd = array();
$attributes = $parameterModel->getAttributes(true);
if(count($attributes))
{
foreach($attributes as $attributeData)
array_push($soapParametersEnd,(WsdlToPhpGenerator::getOptionSendArrayAsParameter()?'\'' . addslashes($attributeData['attribute']->getName()) . '\'=>':'') . $parameterName . '->' . $attributeData['attribute']->getGetterName() . '()');
$soapParametersStart = implode(',',$soapParametersEnd);
$soapParametersEnd = '';
}
else
$soapParametersStart = $soapParametersEnd = '';
}
elseif(is_string($this->getParameterType()))
{
$soapParametersStart = WsdlToPhpGenerator::getOptionSendArrayAsParameter()?'\'' . addslashes($this->getParameterType()) . '\'=>':'';
$soapParametersEnd = '$_' . lcfirst(self::cleanString($this->getParameterType()));
}
elseif(is_array($this->getParameterType()))
{
$soapParametersStart = array();
$soapParametersEnd = '';
foreach($this->getParameterType() as $parameterName=>$parameterType)
{
$model = self::getModelByName($parameterType);
if($model && $model->getIsStruct() && !$model->getIsRestriction())
$paramName = $model->getPackagedName();
else
$paramName = self::cleanString($parameterName);
array_push($soapParametersStart,(WsdlToPhpGenerator::getOptionSendArrayAsParameter()?'\'' . addslashes($parameterName) . '\'=>':'') . '$_' . lcfirst($paramName));
unset($model);
}
$soapParametersStart = implode(',',$soapParametersStart);
}
else
$soapParametersStart = $soapParametersEnd = '';
/**
* Soap call
*/
$soapCallStart = 'self::getSoapClient()->' . ($this->nameIsClean()?$this->getName() . '(':'__soapCall(\'' . $this->getName() . '\'' . ((!empty($soapParametersStart) || !empty($soapParametersEnd))?',array(':''));
$soapCallEnd = ((!$this->nameIsClean() && (!empty($soapParametersStart) || !empty($soapParametersEnd)))?')':'') . ')' . (WsdlToPhpGenerator::getOptionSendParametersAsArray()?'->parameters':'');
/**
* Send parameters in parameters array
*/
if(!empty($soapParametersStart) && $this->nameIsClean())
{
$sendParametersAsArrayStart = (WsdlToPhpGenerator::getOptionSendParametersAsArray()?'array(\'parameters\'=>':'');
$sendParametersAsArrayEnd = (WsdlToPhpGenerator::getOptionSendParametersAsArray()?')':'');
}
else
$sendParametersAsArrayStart = $sendParametersAsArrayEnd = '';
/**
* Send an array
*/
if(!empty($soapParametersStart) && $this->nameIsClean())
{
$sendArrayAsParameterStart = (WsdlToPhpGenerator::getOptionSendArrayAsParameter()?'array(':'');
$sendArrayAsParameterEnd = (WsdlToPhpGenerator::getOptionSendArrayAsParameter()?')':'');
}
else
$sendArrayAsParameterStart = $sendArrayAsParameterEnd = '';
array_push($_body,'$this->setResult(' . $responseAsObjStart . $soapCallStart . $sendParametersAsArrayStart . $sendArrayAsParameterStart . $soapParametersStart . $soapParametersEnd . $sendArrayAsParameterEnd . $sendParametersAsArrayEnd . $soapCallEnd . $responseAsObjEnd . ');');
array_push($_body,'}');
array_push($_body,'catch(SoapFault $soapFault)');
array_push($_body,'{');
array_push($_body,'return !$this->saveLastError(__METHOD__,$soapFault);');
array_push($_body,'}');
array_push($_body,'return $this->getResult();');
array_push($_body,'}');
unset($parameterModel,$parameter,$responseAsObjStart,$responseAsObjEnd,$soapCallStart,$soapCallEnd,$sendParametersAsArrayStart,$sendParametersAsArrayEnd,$sendParametersAsArrayStart,$sendArrayAsParameterEnd,$soapParametersStart,$soapParametersEnd);
}
/**
* Returns the parameter type
* @return string
*/
public function getParameterType()
{
return $this->parameterType;
}
/**
* Set the parameter type
* @param string
* @return string
*/
public function setParameterType($_parameterType)
{
return ($this->parameterType = $_parameterType);
}
/**
* Returns the retrun type
* @return string
*/
public function getReturnType()
{
return $this->returnType;
}
/**
* Set the retrun type
* @param string
* @return string
*/
public function setReturnType($_returnType)
{
return ($this->returnType = $_returnType);
}
/**
* Return class name
* @return string __CLASS__
*/
public function __toString()
{
return __CLASS__;
}
}
?>