-
Notifications
You must be signed in to change notification settings - Fork 0
/
class.phpwsdlenum.php
258 lines (243 loc) · 7.2 KB
/
class.phpwsdlenum.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
<?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_enumHook','internal','PhpWsdlEnum::InterpretEnum');
PhpWsdl::RegisterHook('CreateObjectHook','internalenum','PhpWsdlEnum::CreateEnumTypeObject');
/**
* This class creates enumerations
*
* @author Andreas Müller-Saala, wan24.de
*/
class PhpWsdlEnum extends PhpWsdlObject{
/**
* The enumeration base type
*
* @var string
*/
public $Type;
/**
* A list of elements
*
* @var array
*/
public $Elements;
/**
* An XML string encoding mapping for attributes
*
* @var array
*/
public static $XmlAttributeEntities=Array(
'&' => '&',
'<' => '<',
'>' => '>',
'"' => '"'
);
/**
* Constructor
*
* @param string $name The name
* @param string $type The type name
* @param array $el Optional a list of elements
* @param array $settings Optional the settings hash array (default: NULL)
*/
public function PhpWsdlEnum($name,$type,$el=Array(),$settings=null){
PhpWsdl::Debug('New enumeration type '.$name.' of '.$type);
if($type=='boolean')
throw(new Exception('Boolean enumeration type is not valid'));
parent::PhpWsdlObject($name,$settings);
$this->Type=$type;
$this->Elements=$el;
}
/**
* Create WSDL for the type
*
* @param PhpWsdl $pw The PhpWsdl object
* @return string The WSDL
*/
public function CreateType($pw){
PhpWsdl::Debug('Create WSDL definition for enumeration '.$this->Name.' as '.$this->Type);
$res=Array();
$res[]='<s:simpleType 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>';
}
$res[]='<s:restriction base="'.PhpWsdl::TranslateType($this->Type).'">';
$i=-1;
$len=sizeof($this->Elements);
while(++$i<$len){
$temp=explode('=',$this->Elements[$i],2);
//TODO Is there really no common way to provide a label for an integer value f.e.?
$res[]='<s:enumeration value="'.self::EncodeXmlAttribute($temp[0]).'" />';
}
$res[]='</s:restriction>';
$res[]='</s:simpleType>';
return implode('',$res);
}
/**
* Find an element within this type
*
* @param string $value The value to search for
* @return mixed The element or NULL, if not found
*/
public function GetElement($value){
PhpWsdl::Debug('Find element '.$value);
$i=-1;
$len=sizeof($this->Elements);
while(++$i<$len)
if($this->Elements[$i]==$value){
PhpWsdl::Debug('Found element at index '.$i);
return $this->Elements[$i];
}
return null;
}
/**
* Create the HTML documentation for an enumeration
*
* @param array $data
*/
public function CreateTypeHtml($data){
PhpWsdl::Debug('CreateTypeHtml for enumeration '.$data['type']->Name);
$res=&$data['res'];
$t=&$data['type'];
$res[]='<h3>'.$t->Name.'</h3>';
$res[]='<a name="'.$t->Name.'"></a>';
if(!is_null($t->Docs))
$res[]='<p>'.nl2br(htmlentities($t->Docs)).'</p>';
$res[]='<p>Possible ';
$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].=' values:</p>';
$res[]='<ul class="pre">';
$j=-1;
$eLen=sizeof($t->Elements);
while(++$j<$eLen)
$res[]='<li>'.nl2br(htmlentities($this->Elements[$j])).'</li>';
$res[]='</ul>';
PhpWsdl::CallHook(
'CreateTypeHtmlHook',
$data
);
}
/**
* Create enumeration PHP code
*
* @param array $data The event data
*/
public function CreateTypePhp($data){
$server=$data['server'];
$res=&$data['res'];
$res[]="/**";
if(!is_null($this->Docs)){
$res[]=" * ".implode("\n * ",explode("\n",$this->Docs));
$res[]=" *";
}
$res[]=" * @pw_enum ".$this->Type." ".$this->Name." ".implode(',',$this->Elements);
$res[]=" */";
$res[]="abstract class ".$this->Name."{";
$i=-1;
$eLen=sizeof($this->Elements);
while(++$i<$eLen){
$temp=explode('=',$this->Elements[$i],2);
if(sizeof($temp)==1) $temp[]=$temp[0];// Use the key as string value, if no value was given
$res[]="\t/**";
$res[]="\t * @var ".$this->Type;
$res[]="\t */";
$res[]="\tconst \$".$temp[0]."=\"".addslashes($temp[1])."\";";
}
$res[]="}";
}
/**
* Interpret an enumeration
*
* @param array $data The parser data
* @return boolean Response
*/
public static function InterpretEnum($data){
$info=explode(' ',$data['keyword'][1],4);
if(sizeof($info)<3)
return true;
$server=$data['server'];
$name=$info[1];
PhpWsdl::Debug('Interpret enumeration "'.$name.'"');
$type=$info[0];
$el=explode(',',$info[2]);
$docs=null;
if($server->ParseDocs)
if(sizeof($info)>3)
$docs=$info[3];
PhpWsdl::Debug('Enumeration "'.$name.'" type of "'.$type.'" definition');
$data['type']=Array(
'id' => 'enum',
'name' => $name,
'type' => $type,
'elements' => $el,
'docs' => $docs
);
return false;
}
/**
* Create enumeration object
*
* @param array $data The parser data
* @return boolean Response
*/
public static function CreateEnumTypeObject($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']!='enum')
return true;
if(!isset($data['type']['elements']))
return true;
if(!is_array($data['type']['elements']))
return true;
if(!is_null($data['docs'])){
$data['settings']['docs']=$data['docs'];
}else{
$data['settings']['docs']=$data['type']['docs'];
}
PhpWsdl::Debug('Add enumeration '.$data['type']['name']);
$data['obj']=new PhpWsdlEnum($data['type']['name'],$data['type']['type'],$data['type']['elements'],$data['settings']);
$data['settings']=Array();
$data['server']->Types[]=$data['obj'];
return true;
}
/**
* Encode a string for use as XML attribute
*
* @param string $str The string
* @return string The encoded string
*/
private static function EncodeXmlAttribute($str){
$keys=array_keys(self::$XmlAttributeEntities);
$i=-1;
$len=sizeof($keys);
while(++$i<$len)
$str=str_replace($keys[$i],self::$XmlAttributeEntities[$keys[$i]],$str);
return $str;
}
}