|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* |
|
5
|
|
|
* This file is part of the Apix Project. |
|
6
|
|
|
* |
|
7
|
|
|
* (c) Franck Cassedanne <franck at ouarz.net> |
|
8
|
|
|
* |
|
9
|
|
|
* @license http://opensource.org/licenses/BSD-3-Clause New BSD License |
|
10
|
|
|
* |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace Apix\Output\Xml; |
|
14
|
|
|
|
|
15
|
|
|
use Apix\Output\Xml; |
|
16
|
|
|
|
|
17
|
|
|
class SimpleXml extends Xml |
|
18
|
|
|
{ |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* SimpleXML handler |
|
22
|
|
|
* {@inheritdoc} |
|
23
|
|
|
*/ |
|
24
|
|
|
public function encode(array $data, $rootNode='root') |
|
25
|
|
|
{ |
|
26
|
|
|
$str = sprintf('<?xml version="%s" encoding="%s"?><%s />', |
|
27
|
|
|
$this->version, |
|
28
|
|
|
$this->encoding, |
|
29
|
|
|
$rootNode |
|
30
|
|
|
); |
|
31
|
|
|
|
|
32
|
|
|
$x = new \SimpleXMLElement($str); |
|
33
|
|
|
$this->arrayToSimpleXml($x, $data); |
|
34
|
|
|
|
|
35
|
|
|
return $x->asXML(); |
|
|
|
|
|
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Array to Simple XML |
|
40
|
|
|
* |
|
41
|
|
|
* @param \SimpleXMLElement $xml |
|
|
|
|
|
|
42
|
|
|
* @param array $array |
|
43
|
|
|
*/ |
|
44
|
|
View Code Duplication |
protected function arrayToSimpleXml(\SimpleXMLElement $x, array $array) |
|
|
|
|
|
|
45
|
|
|
{ |
|
46
|
|
|
foreach ($array as $k => $v) { |
|
47
|
|
|
if (is_array($v)) { |
|
48
|
|
|
if (is_int($k)) { |
|
49
|
|
|
$k = $this->items_key; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
// @codeCoverageIgnoreStart |
|
53
|
|
|
// Attributes needs to be reviewd!!! |
|
54
|
|
|
#if ($k == '@attributes') { |
|
|
|
|
|
|
55
|
|
|
# foreach ($v as $k => $v) { |
|
|
|
|
|
|
56
|
|
|
# $x->addAttribute($k, $v); |
|
|
|
|
|
|
57
|
|
|
# } |
|
58
|
|
|
// @codeCoverageIgnoreEnd |
|
59
|
|
|
#} else { |
|
60
|
|
|
$child = $x->addChild($k); |
|
61
|
|
|
$this->arrayToSimpleXml($child, $v); |
|
62
|
|
|
#} |
|
63
|
|
|
} else { |
|
64
|
|
|
if (is_int($k)) { |
|
65
|
|
|
$k = $this->item_key; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
// $xml->addAttribute( |
|
69
|
|
|
// $k, |
|
70
|
|
|
// htmlspecialchars($v, ENT_QUOTES, $this->encoding) |
|
|
|
|
|
|
71
|
|
|
// ) |
|
72
|
|
|
$x->addChild($k, $this->specialChars($v)); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Decodes any special characters entities. |
|
79
|
|
|
* XmlWriter does this automatically. |
|
80
|
|
|
*/ |
|
81
|
|
|
public function specialChars($var) |
|
82
|
|
|
{ |
|
83
|
|
|
return htmlspecialchars($var, ENT_COMPAT, $this->encoding); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
} |
|
87
|
|
|
|