-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass.phpwsdlcomplex.php
378 lines (364 loc) · 10.9 KB
/
class.phpwsdlcomplex.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
<?php
/*
PhpWsdl - Generate WSDL from PHP
Copyright (C) 2011 Andreas Müller-Saala, wan24.de
This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation; either version 3 of the License, or (at your option) any later
version.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
this program; if not, see <http://www.gnu.org/licenses/>.
*/
if(basename($_SERVER['SCRIPT_FILENAME'])==basename(__FILE__))
exit;
PhpWsdl::RegisterHook('InterpretKeywordpw_complexHook','internal','PhpWsdlComplex::InterpretComplex');
PhpWsdl::RegisterHook('CreateObjectHook','internalcomplex','PhpWsdlComplex::CreateComplexTypeObject');
/**
* This class creates complex types (classes or arrays)
*
* @author Andreas Müller-Saala, wan24.de
*/
class PhpWsdlComplex extends PhpWsdlObject{
/**
* The array type
*
* @var string
*/
public $Type=null;
/**
* Name of the complex type to inherit from
*
* @var string
*/
public $Inherit=null;
/**
* A list of elements, if this type is a class
*
* @var PhpWsdlElement[]
*/
public $Elements;
/**
* Is this type an array?
*
* @var boolean
*/
public $IsArray;
/**
* Enable creating a PHP constructor that requires all parameters?
*
* @var boolean
*/
public $EnablePhpConstructor=false;
/**
* Disable definition of arrays with the "Array" postfix in the type name?
*
* @var boolean
*/
public static $DisableArrayPostfix=false;
/**
* Create a PHP constructor that requires all parameters per default?
*
* @var boolean
*/
public static $DefaultEnablePhpConstructor=false;
/**
* Constructor
*
* @param string $name The name
* @param PhpWsdlElement[] $el Optional a list of elements
* @param array $settings Optional the settings hash array (default: NULL)
*/
public function PhpWsdlComplex($name,$el=Array(),$settings=null){
PhpWsdl::Debug('New complex type '.$name);
parent::PhpWsdlObject($name,$settings);
if(!self::$DisableArrayPostfix){
$this->IsArray=substr($name,strlen($name)-5,5)=='Array';
if($this->IsArray)
$this->Type=substr($this->Name,0,strlen($this->Name)-5);
}
$this->Elements=$el;
$this->EnablePhpConstructor=self::$DefaultEnablePhpConstructor;
if(!is_null($settings)){
if(isset($settings['isarray']))
$this->IsArray=$settings['isarray'];
if(isset($settings['phpconstructor']))
$this->EnablePhpConstructor=$settings['phpconstructor']=='1'||$settings['phpconstructor']=='true';
if(isset($settings['type']))
$this->IsArray=$settings['type'];
if(isset($settings['inherit']))
$this->Inherit=$settings['inherit'];
}
}
/**
* Create WSDL for the type
*
* @param PhpWsdl $pw The PhpWsdl object
* @return string The WSDL
*/
public function CreateType($pw){
$res=Array();
$res[]='<s:complexType name="'.$this->Name.'">';
if($pw->IncludeDocs&&!$pw->Optimize&&!is_null($this->Docs)){
$res[]='<s:annotation>';
$res[]='<s:documentation><![CDATA['.$this->Docs.']]></s:documentation>';
$res[]='</s:annotation>';
}
if(!$this->IsArray){
PhpWsdl::Debug('Create WSDL definition for type '.$this->Name.' as type');
if(is_null($this->Inherit)){
$res[]='<s:sequence>';
$i=-1;
$len=sizeof($this->Elements);
while(++$i<$len)
$res[]=$this->Elements[$i]->CreateElement($pw);
$res[]='</s:sequence>';
}else{
PhpWsdl::Debug('Inherit from "'.$this->Inherit.'"');
$res[]='<s:complexContent>';
$res[]='<s:extension base="tns:'.$this->Inherit.'">';
$res[]='<s:sequence>';
$i=-1;
$len=sizeof($this->Elements);
while(++$i<$len)
$res[]=$this->Elements[$i]->CreateElement($pw);
$res[]='</s:sequence>';
$res[]='</s:extension>';
$res[]='</s:complexContent>';
}
}else{
PhpWsdl::Debug('Create WSDL definition for type '.$this->Name.' as array');
$res[]='<s:complexContent>';
$res[]='<s:restriction base="soapenc:Array">';
$res[]='<s:attribute ref="soapenc:arrayType" wsdl:arrayType="'.PhpWsdl::TranslateType($this->Type).'[]" />';
$res[]='</s:restriction>';
$res[]='</s:complexContent>';
}
$res[]='</s:complexType>';
return implode('',$res);
}
/**
* Find an element within this type
*
* @param string $name The name
* @return PhpWsdlElement The element or NULL, if not found
*/
public function GetElement($name){
PhpWsdl::Debug('Find element '.$name);
$i=-1;
$len=sizeof($this->Elements);
while(++$i<$len)
if($this->Elements[$i]->Name==$name){
PhpWsdl::Debug('Found element at index '.$i);
return $this->Elements[$i];
}
return null;
}
/**
* Create the HTML documentation for a complex type
*
* @param array $data
*/
public function CreateTypeHtml($data){
PhpWsdl::Debug('CreateTypeHtml for '.$data['type']->Name);
$server=$data['server'];
$res=&$data['res'];
$t=&$data['type'];
$res[]='<h3>'.$t->Name.'</h3>';
$res[]='<a name="'.$t->Name.'"></a>';
if(!is_null($t->Inherit))
$res[]='<p>This type inherits all properties from <a href="#'.$t->Inherit.'"><span class="lightBlue">'.$t->Inherit.'</span></a>.</p>';
$eLen=sizeof($t->Elements);
if($t->IsArray){
// Array type
$res[]='<p>This is an array type of <span class="pre">';
$o=sizeof($res)-1;
if(in_array($t->Type,PhpWsdl::$BasicTypes)){
$res[$o].='<span class="blue">'.$t->Type.'</span>';
}else{
$res[$o].='<a href="#'.$t->Type.'"><span class="lightBlue">'.$t->Type.'</span></a>';
}
$res[$o].='</span>.</p>';
if(!is_null($t->Docs))
$res[]='<p>'.nl2br(htmlentities($t->Docs)).'</p>';
}else if($eLen>0){
// Complex type with elements
if(!is_null($t->Docs))
$res[]='<p>'.nl2br(htmlentities($t->Docs)).'</p>';
$res[]='<ul class="pre">';
$j=-1;
while(++$j<$eLen)
$t->Elements[$j]->CreateElementHtml(array_merge(
$data,
Array(
'element' => $t->Elements[$j]
)
));
$res[]='</ul>';
}else{
// Complex type without elements
$res[]='<p>This type has no elements.</p>';
}
PhpWsdl::CallHook(
'CreateTypeHtmlHook',
$data
);
}
/**
* Create type PHP code
*
* @param array $data The event data
*/
public function CreateTypePhp($data){
if($this->IsArray)
return;
$server=$data['server'];
$res=&$data['res'];
$res[]="/**";
if(!is_null($this->Docs)){
$res[]=" * ".implode("\n * ",explode("\n",$this->Docs));
$res[]=" *";
}
$temp=Array();
$i=-1;
$eLen=sizeof($this->Elements);
while(++$i<$eLen){
$e=$this->Elements[$i];
$temp[]='$'.$e->Name;
$res[]=" * @pw_element ".$e->Type." \$".$e->Name.((!is_null($e->Docs))?' '.$e->Docs:'');
}
$res[]=" * @pw_complex ".$this->Name;
$res[]=" */";
$res[]="class ".$this->Name.((is_null($this->Inherit))?'':' extends '.$this->Inherit)."{";
if($eLen>0&&$this->EnablePhpConstructor){
$res[]="\t/**";
$res[]="\t * Constructor with parameters (all required!)";
$res[]="\t *";
$i=-1;
while(++$i<$eLen){
$e=$this->Elements[$i];
$res[]="\t * @param ".$e->Type." ".$temp[$i].((!is_null($e->Docs))?' '.$e->Docs:'');
}
if(!is_null($this->Inherit)){
$it=$server->GetType($this->Inherit);
if($it->EnablePhpConstructor){
$i=-1;
$tLen=sizeof($it->Elements);
while(++$i<$tLen){
$e=$it->Elements[$i];
$tempb[]='$'.$e->Name;
$res[]=" * @param ".$e->Type." \$".$e->Name.((!is_null($e->Docs))?' '.$e->Docs:'');
}
}
}
$res[]="\t */";
$res[]="\tpublic function ".$this->Name."(".implode(',',$temp)."){";
if(!is_null($this->Inherit)&&$it->EnablePhpConstructor)
$res[]="\t\tparent::".$this->Inherit."(".implode(',',$tempb).");";
$i=-1;
while(++$i<$eLen)
$res[]="\t\t\$this->".$this->Elements[$i]->Name."=".$temp[$i];
$res[]="\t}";
}
$i=-1;
while(++$i<$eLen){
$e=$this->Elements[$i];
$res[]="\t/**";
if(!is_null($e->Docs)){
$res[]="\t * ".implode("\n\t * ",explode("\n",$e->Docs));
$res[]="\t *";
}
$res[]="\t * @var ".$e->Type;
$res[]="\t */";
$res[]="\tpublic \$".$this->Elements[$i]->Name.";";
}
$res[]="}";
}
/**
* Interpret a complex type
*
* @param array $data The parser data
* @return boolean Response
*/
public static function InterpretComplex($data){
$info=explode(' ',$data['keyword'][1],3);
if(sizeof($info)<1)
return true;
$server=$data['server'];
$name=$info[0];
PhpWsdl::Debug('Interpret complex type "'.$name.'"');
$type=null;
$docs=null;
if(strpos($name,'[]')>-1){
if(sizeof($info)<2){
PhpWsdl::Debug('WARNING: Invalid array definition!');
return true;
}
$name=substr($name,0,strlen($name)-2);
if(!is_null($server->GetType($name))){
PhpWsdl::Debug('WARNING: Double type detected!');
return true;
}
$type=$info[1];
if($server->ParseDocs)
if(sizeof($info)>2)
$docs=$info[2];
PhpWsdl::Debug('Array "'.$name.'" type of "'.$type.'" definition');
}else{
if(!is_null($server->GetType($name))){
PhpWsdl::Debug('WARNING: Double type detected!');
return true;
}
if(!self::$DisableArrayPostfix&&substr($name,strlen($name)-5)=='Array'){
$type=substr($name,0,strlen($name)-5);
PhpWsdl::Debug('Array "'.$name.'" type of "'.$type.'" definition');
}else{
PhpWsdl::Debug('Complex type definition');
}
if($server->ParseDocs){
$temp=sizeof($info);
if($temp>1)
$docs=($temp>2)?$info[1].' '.$info[2]:$info[1];
}
}
$data['type']=Array(
'id' => 'complex',
'name' => $name,
'type' => $type,
'docs' => $docs
);
return false;
}
/**
* Create complex type object
*
* @param array $data The parser data
* @return boolean Response
*/
public static function CreateComplexTypeObject($data){
if($data['method']!='')
return true;
if(!is_null($data['obj']))
return true;
if(!is_array($data['type']))
return true;
if(!isset($data['type']['id']))
return true;
if($data['type']['id']!='complex')
return true;
if(!is_null($data['docs'])){
$data['settings']['docs']=$data['docs'];
}else{
$data['settings']['docs']=$data['type']['docs'];
}
PhpWsdl::Debug('Add complex type '.$data['type']['name']);
$data['settings']['isarray']=!is_null($data['type']['type']);
$data['obj']=new PhpWsdlComplex($data['type']['name'],$data['elements'],$data['settings']);
$data['obj']->Type=$data['type']['type'];
$data['settings']=Array();
$data['server']->Types[]=$data['obj'];
return true;
}
}