-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMessage.php
More file actions
294 lines (270 loc) · 6.42 KB
/
Message.php
File metadata and controls
294 lines (270 loc) · 6.42 KB
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
<?php
/**
* This file is part of the Zimbra API in PHP library.
*
* © Nguyen Van Nguyen <nguyennv1981@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Zimbra\Soap;
use Zimbra\Common\SimpleXML;
/**
* Soap message class
*
* @package Zimbra
* @category Soap
* @author Nguyen Van Nguyen - nguyennv1981@gmail.com
* @copyright Copyright © 2013 by Nguyen Van Nguyen.
*/
class Message
{
/**
* Soap 1.1 const.
*/
const SOAP_1_1 = '1.1';
/**
* Soap 1.2 const.
*/
const SOAP_1_2 = '1.2';
/**
* Soap 1.1 namespace.
*/
const NS_SOAP_1_1 = 'http://schemas.xmlsoap.org/soap/envelope';
/**
* Soap 1.2 namespace.
*/
const NS_SOAP_1_2 = 'http://www.w3.org/2003/05/soap-envelope';
/**
* Soap headers
* @var array
*/
private $_headers = [];
/**
* Soap request
* @var Request
*/
private $_request;
/**
* Soap request body
* @var SimpleXML
*/
private $_body;
/**
* The xml namespaces
* @var array
*/
private $_namespaces = ['urn:zimbra'];
/**
* The soap version
* @var string
*/
private $_version;
/**
* Content types for SOAP versions.
* @var array
*/
static protected $contentTypeMap = [
'1.1' => 'text/xml; charset=utf-8',
'1.2' => 'application/soap+xml; charset=utf-8'
];
/**
* Message constructor
*
* @param string $version The soap version.
* @return self
*/
public function __construct($version = self::SOAP_1_2)
{
$this->_version = in_array($version, [self::SOAP_1_2, self::SOAP_1_1]) ? $version : self::SOAP_1_2;
}
/**
* Gets soap request
*
* @return Request
*/
public function getRequest()
{
return $this->_request;
}
/**
* Set soap request
*
* @param Request $request
* @return self
*/
public function setRequest(Request $request)
{
$this->_request = $request;
$this->addNamespace($this->_request->getXmlNamespace());
$this->_body = $request->toXml();
$namespaces = array_values($this->_body->getDocNamespaces(true));
$this->addNamespace($namespaces);
return $this;
}
/**
* Add namespace.
*
* @param string|array $namespace
* @return self
*/
public function addNamespace($namespace)
{
if(is_array($namespace))
{
foreach ($namespace as $ns)
{
$this->addNamespace($ns);
}
}
else
{
if(!in_array($namespace, $this->_namespaces) && !empty($namespace))
{
$this->_namespaces[] = (string) $namespace;
}
}
return $this;
}
/**
* Add header.
*
* @param string|array $name
* @param string $value
* @return self
*/
public function addHeader($name, $value = null)
{
if(is_array($name))
{
foreach ($name as $n => $v)
{
$this->addHeader($n, $v);
}
}
else
{
$this->_headers[$name] = $value;
}
return $this;
}
/**
* Get soap header.
*
* @param string $name
* @return string|array
*/
public function getHeader($name = null)
{
if(null === $name)
{
return $this->_headers;
}
else
{
return isset($this->_headers[$name]) ? $this->_headers[$name] : null;
}
}
/**
* Get all soap headers.
*
* @return array
*/
public function getHeaders()
{
return $this->_headers;
}
/**
* Gets content type
*
* @param string $version Soap version
* @return string
*/
public function getContentType($version = null)
{
$version = in_array($version, [self::SOAP_1_2, self::SOAP_1_1]) ? $version : $this->_version;
return self::$contentTypeMap[$version];
}
/**
* Gets soap version
*
* @param string $version
* @return string
*/
public function getVersion()
{
return $this->_version;
}
/**
* Returns the json encoded string representation of this class
*
* @return string
*/
public function toJson()
{
$array = [];
if(count($this->_headers))
{
$array['Header'] = [
'context' => [
'_jsns' => 'urn:zimbra',
],
];
foreach ($this->_headers as $name => $value)
{
$array['Header']['context'][$name] = $value;
}
}
if($this->_request instanceof Request)
{
$reqArray = $this->_request->toArray();
$reqName = $this->_request->requestName();
$array['Body'][$reqName] = $reqArray[$reqName];
}
return json_encode((object) $array);
}
/**
* Method returning the xml representation of this class
*
* @return SimpleXML
*/
public function toXml()
{
$soapNamespace = ($this->_version === self::SOAP_1_2) ? self::NS_SOAP_1_2 : self::NS_SOAP_1_1;
$nsString = 'xmlns:env="'.$soapNamespace.'"';
foreach ($this->_namespaces as $key => $ns)
{
if($key > 0)
{
$nsString .= sprintf(' xmlns:urn%d="%s"', $key, $ns);
}
else
{
$nsString .= sprintf(' xmlns:urn="%s"', $ns);
}
}
$message = sprintf('<env:Envelope %s></env:Envelope>', $nsString);
$xml = new SimpleXML($message);
if(count($this->_headers))
{
$header = $xml->addChild('Header')
->addChild('context', null, 'urn:zimbra');
foreach ($this->_headers as $name => $value)
{
$header->addChild($name, $value);
}
}
$body = $xml->addChild('Body');
$body->append($this->_body, $this->_request->getXmlNamespace());
return $xml;
}
/**
* Return a well-formed XML string.
*
* @return string Xml string
*/
public function __toString()
{
return trim($this->toXml()->asXml());
}
}