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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
|
<?PHP
/* vim: set expandtab tabstop=4 shiftwidth=4: */
// +----------------------------------------------------------------------+
// | PHP Version 4 |
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2002 The PHP Group |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.0 of the PHP license, |
// | that is bundled with this package in the file LICENSE, and is |
// | available at through the world-wide-web at |
// | http://www.php.net/license/2_02.txt. |
// | If you did not receive a copy of the PHP license and are unable to |
// | obtain it through the world-wide-web, please send a note to |
// | [email protected] so we can mail you a copy immediately. |
// +----------------------------------------------------------------------+
// | Authors: Stephan Schmidt <[email protected]> |
// +----------------------------------------------------------------------+
//
// $Id: Serializer.php,v 1.14 2003/10/26 19:38:21 schst Exp $
/**
* uses PEAR error management
*/
require_once 'PEAR.php';
/**
* uses XML_Util to create XML tags
*/
require_once 'XML/Util.php';
/**
* error code for no serialization done
*/
define("XML_SERIALIZER_ERROR_NO_SERIALIZATION", 51);
/**
* XML_Serializer
* class that serializes various structures into an XML document
*
* this class can be used in two modes:
*
* 1. create an XML document from an array or object that is processed by other
* applications. That means, you can create a RDF document from an array in the
* following format:
*
* $data = array(
* "channel" => array(
* "title" => "Example RDF channel",
* "link" => "http://www.php-tools.de",
* "image" => array(
* "title" => "Example image",
* "url" => "http://www.php-tools.de/image.gif",
* "link" => "http://www.php-tools.de"
* ),
* array(
* "title" => "Example item",
* "link" => "http://example.com"
* ),
* array(
* "title" => "Another Example item",
* "link" => "http://example.org"
* )
* )
* );
*
* to create a RDF document from this array do the following:
*
* require_once 'XML/Serializer.php';
*
* $options = array(
* "indent" => "\t", // indent with tabs
* "linebreak" => "\n", // use UNIX line breaks
* "rootName" => "rdf:RDF", // root tag
* "defaultTagName" => "item" // tag for values with numeric keys
* );
*
* $serializer = new XML_Serializer($options);
* $rdf = $serializer->serialize($data);
*
* You will get a complete XML document that can be processed like any RDF document.
*
*
* 2. this classes can be used to serialize any data structure in a way that it can
* later be unserialized again.
* XML_Serializer will store the type of the value and additional meta information
* in attributes of the surrounding tag. This meat information can later be used
* to restore the original data structure in PHP. If you want XML_Serializer
* to add meta information to the tags, add
*
* "typeHints" => true
*
* to the options array in the constructor.
*
* Future versions of this package will include an XML_Unserializer, that does
* the unserialization automatically for you.
*
* @category XML
* @package XML_Serializer
* @version 0.9.1
* @author Stephan Schmidt <[email protected]>
* @uses XML_Util
*/
class XML_Serializer extends PEAR {
/**
* default options for the serialization
* @access private
* @var array $_defaultOptions
*/
var $_defaultOptions = array(
"indent" => "", // string used for indentation
"linebreak" => "\n", // string used for newlines
"typeHints" => false, // automatically add type hin attributes
"addDecl" => false, // add an XML declaration
"defaultTagName" => "XML_Serializer_Tag", // tag used for indexed arrays or invalid names
"keyAttribute" => "_originalKey", // attribute where original key is stored
"typeAttribute" => "_type", // attribute for type (only if typeHints => true)
"classAttribute" => "_class", // attribute for class of objects (only if typeHints => true)
"scalarAsAttributes" => false, // scalar values (strings, ints,..) will be serialized as attribute
"prependAttributes" => "", // prepend string for attributes
"indentAttributes" => false, // indent the attributes, if set to '_auto', it will indent attributes so they all start at the same column
"mode" => 'default', // use 'simplexml' to use parent name as tagname if transforming an indexed array
"addDoctype" => false, // add a doctype declaration
"doctype" => null, // supply a string or an array with id and uri ({@see XML_Util::getDoctypeDeclaration()}
"rootAttributes" => array() // attributes of the root tag
);
/**
* options for the serialization
* @access private
* @var array $options
*/
var $options = array();
/**
* current tag depth
* @var integer $_tagDepth
*/
var $_tagDepth = 0;
/**
* serilialized representation of the data
* @var string $_serializedData
*/
var $_serializedData = null;
/**
* constructor
*
* @access public
* @param mixed $options array containing options for the serialization
*/
function XML_Serializer( $options = null )
{
$this->PEAR();
if (is_array($options)) {
$this->options = array_merge($this->_defaultOptions, $options);
} else {
$this->options = $this->_defaultOptions;
}
}
/**
* return API version
*
* @access public
* @static
* @return string $version API version
*/
function apiVersion()
{
return "0.9";
}
/**
* reset all options to default options
*
* @access public
* @see setOption(), XML_Unserializer()
*/
function resetOptions()
{
$this->options = $this->_defaultOptions;
}
/**
* set an option
*
* You can use this method if you do not want to set all options in the constructor
*
* @access public
* @see resetOption(), XML_Serializer()
*/
function setOption($name, $value)
{
$this->options[$name] = $value;
}
/**
* serialize data
*
* @access public
* @param mixed $data data to serialize
* @return boolean true on success, pear error on failure
*/
function serialize($data, $options = null)
{
// if options have been specified, use them instead
// of the previously defined ones
if (is_array($options)) {
$optionsBak = $this->options;
if (isset($options["overrideOptions"]) && $options["overrideOptions"] == true) {
$this->options = array_merge($this->_defaultOptions, $options);
} else {
$this->options = array_merge($this->options, $options);
}
}
else {
$optionsBak = null;
}
// maintain BC
if (isset($this->options["tagName"])) {
$this->options["rootName"] = $this->options["tagName"];
}
// start depth is zero
$this->_tagDepth = 0;
$this->_serializedData = "";
// serialize an array
if (is_array($data)) {
if (isset($this->options["rootName"])) {
$tagName = $this->options["rootName"];
} else {
$tagName = "array";
}
$this->_serializedData .= $this->_serializeArray($data, $tagName, $this->options["rootAttributes"]);
}
// serialize an object
elseif (is_object($data)) {
if (isset($this->options["rootName"])) {
$tagName = $this->options["rootName"];
} else {
$tagName = get_class($data);
}
$this->_serializedData .= $this->_serializeObject($data, $tagName, $this->options["rootAttributes"]);
}
// add doctype declaration
if ($this->options["addDoctype"] === true) {
$this->_serializedData = XML_Util::getDoctypeDeclaration($tagName, $this->options["doctype"])
. $this->options["linebreak"]
. $this->_serializedData;
}
// build xml declaration
if ($this->options["addDecl"]) {
$atts = array();
if (isset($this->options["encoding"]) ) {
$encoding = $this->options["encoding"];
} else {
$encoding = null;
}
$this->_serializedData = XML_Util::getXMLDeclaration("1.0", $encoding)
. $this->options["linebreak"]
. $this->_serializedData;
}
if ($optionsBak !== null) {
$this->options = $optionsBak;
}
return true;
}
/**
* get the result of the serialization
*
* @access public
* @return string $serializedData
*/
function getSerializedData()
{
if ($this->_serializedData == null ) {
return $this->raiseError("No serialized data available. Use XML_Serializer::serialize() first.", XML_SERIALIZER_ERROR_NO_SERIALIZATION);
}
return $this->_serializedData;
}
/**
* serialize any value
*
* This method checks for the type of the value and calls the appropriate method
*
* @access private
* @param mixed $value
* @param string $tagName
* @param array $attributes
* @return string
*/
function _serializeValue($value, $tagName = null, $attributes = array())
{
if (is_array($value)) {
$xml = $this->_serializeArray($value, $tagName, $attributes);
} elseif (is_object($value)) {
$xml = $this->_serializeObject($value, $tagName);
} else {
$tag = array(
"qname" => $tagName,
"attributes" => $attributes,
"content" => $value
);
$xml = $this->_createXMLTag($tag);
}
return $xml;
}
/**
* serialize an array
*
* @access private
* @param array $array array to serialize
* @param string $tagName name of the root tag
* @param array $attributes attributes for the root tag
* @return string $string serialized data
* @uses XML_Util::isValidName() to check, whether key has to be substituted
*/
function _serializeArray(&$array, $tagName = null, $attributes = array())
{
if (is_array($array) && $this->options["mode"] == "simplexml") {
$indexed = true;
foreach ($array as $key => $val) {
if (!is_int($key)) {
$indexed = false;
break;
}
}
if ($indexed) {
$string = "";
foreach ($array as $key => $val) {
$string .= $this->_serializeValue( $val, $tagName, $attributes);
$string .= $this->options["linebreak"];
// do indentation
if ($this->options["indent"]!==null && $this->_tagDepth>0) {
$string .= str_repeat($this->options["indent"], $this->_tagDepth);
}
}
return rtrim($string);
}
}
$this->_tagDepth++;
if ($this->options["scalarAsAttributes"] === true) {
foreach ($array as $key => $value) {
if (is_scalar($value) && (XML_Util::isValidName($key) === true)) {
unset($array[$key]);
$attributes[$this->options["prependAttributes"].$key] = $value;
}
}
}
// check for empty array => create empty tag
if (empty($array)) {
}
$tmp = $this->options["linebreak"];
foreach ($array as $key => $value) {
// do indentation
if ($this->options["indent"]!==null && $this->_tagDepth>0) {
$tmp .= str_repeat($this->options["indent"], $this->_tagDepth);
}
// copy key
$origKey = $key;
// key cannot be used as tagname => use default tag
$valid = XML_Util::isValidName($key);
if (PEAR::isError($valid)) {
$key = $this->options["defaultTagName"];
}
$atts = array();
if ($this->options["typeHints"] === true) {
$atts[$this->options["typeAttribute"]] = gettype($value);
if ($key !== $origKey) {
$atts[$this->options["keyAttribute"]] = (string)$origKey;
}
}
$tmp .= $this->_createXMLTag(array(
"qname" => $key,
"attributes" => $atts,
"content" => $value )
);
$tmp .= $this->options["linebreak"];
}
$this->_tagDepth--;
if ($this->options["indent"]!==null && $this->_tagDepth>0) {
$tmp .= str_repeat($this->options["indent"], $this->_tagDepth);
}
if (trim($tmp) === '') {
$tmp = null;
}
$tag = array(
"qname" => $tagName,
"content" => $tmp,
"attributes" => $attributes
);
if ($this->options["typeHints"] === true) {
if (!isset($tag["attributes"][$this->options["typeAttribute"]])) {
$tag["attributes"][$this->options["typeAttribute"]] = "array";
}
}
$string = $this->_createXMLTag($tag, false);
return $string;
}
/**
* serialize an object
*
* @access private
* @param object $object object to serialize
* @return string $string serialized data
*/
function _serializeObject(&$object, $tagName = null, $attributes = array())
{
// check for magic function
if (method_exists($object, "__sleep")) {
$object->__sleep();
}
$tmp = $this->options["linebreak"];
$properties = get_object_vars($object);
if (empty($tagName)) {
$tagName = get_class($object);
}
// typehints activated?
if ($this->options["typeHints"] === true) {
$attributes[$this->options["typeAttribute"]] = "object";
$attributes[$this->options["classAttribute"]] = get_class($object);
}
$string = $this->_serializeArray($properties, $tagName, $attributes);
return $string;
}
/**
* create a tag from an array
* this method awaits an array in the following format
* array(
* "qname" => $tagName,
* "attributes" => array(),
* "content" => $content, // optional
* "namespace" => $namespace // optional
* "namespaceUri" => $namespaceUri // optional
* )
*
* @access private
* @param array $tag tag definition
* @param boolean $replaceEntities whether to replace XML entities in content or not
* @return string $string XML tag
*/
function _createXMLTag( $tag, $replaceEntities = true )
{
if ($this->options["indentAttributes"] !== false) {
$multiline = true;
$indent = str_repeat($this->options["indent"], $this->_tagDepth);
if ($this->options["indentAttributes"] == "_auto") {
$indent .= str_repeat(" ", (strlen($tag["qname"])+2));
} else {
$indent .= $this->options["indentAttributes"];
}
} else {
$multiline = false;
$indent = false;
}
if ((string)$tag["content"] == '') {
$tag["content"] = '';
}
if (is_scalar($tag["content"])) {
$tag = XML_Util::createTagFromArray($tag, $replaceEntities, $multiline, $indent, $this->options["linebreak"]);
} elseif (is_array($tag["content"])) {
$tag = $this->_serializeArray($tag["content"], $tag["qname"], $tag["attributes"]);
} elseif (is_object($tag["content"])) {
$tag = $this->_serializeObject($tag["content"], $tag["qname"], $tag["attributes"]);
} elseif (is_resource($tag["content"])) {
settype($tag["content"], "string");
$tag = XML_Util::createTagFromArray($tag, $replaceEntities);
}
return $tag;
}
}
?>
|