1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Class XmlResponse | XmlResponse.php |
4
|
|
|
* @package Faulancer\Http |
5
|
|
|
* @author Florian Knapp <[email protected]> |
6
|
|
|
*/ |
7
|
|
|
namespace Faulancer\Http; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class XmlResponse |
11
|
|
|
*/ |
12
|
|
|
class XmlResponse extends Response |
13
|
|
|
{ |
14
|
|
|
|
15
|
|
|
public function __construct ($content = []) |
16
|
|
|
{ |
17
|
|
|
parent::__construct($content); |
18
|
|
|
|
19
|
|
|
$this->setContent($content); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** @var array */ |
23
|
|
|
protected $content = []; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param array $data |
27
|
|
|
* @param \SimpleXMLElement $xml |
28
|
|
|
* @codeCoverageIgnore |
29
|
|
|
*/ |
30
|
|
|
private function convertArrayToXml(\SimpleXMLElement &$xml, $data) |
31
|
|
|
{ |
32
|
|
|
foreach ($data as $key=>$value) { |
33
|
|
|
|
34
|
|
|
if(is_numeric($key)) { |
35
|
|
|
$key = 'item' . $key; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
if(!empty($value['value']) && is_array($value['value'])) { |
39
|
|
|
|
40
|
|
|
$node = $this->generateNode($xml, $key, $value); |
41
|
|
|
$this->convertArrayToXml($node, $value['value']); |
|
|
|
|
42
|
|
|
|
43
|
|
|
} else { |
44
|
|
|
$this->generateNode($xml, $key, $value); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param \SimpleXMLElement $xml |
52
|
|
|
* @param string $key |
53
|
|
|
* @param null|array|string $value |
54
|
|
|
* @return null|\SimpleXMLElement |
55
|
|
|
* @codeCoverageIgnore |
56
|
|
|
*/ |
57
|
|
|
private function generateNode(\SimpleXMLElement &$xml, $key, $value = null) |
58
|
|
|
{ |
59
|
|
|
$node = null; |
|
|
|
|
60
|
|
|
|
61
|
|
|
if (in_array('@attributes', array_keys($value))) { |
62
|
|
|
|
63
|
|
|
$attributes = $value['@attributes']; |
64
|
|
|
|
65
|
|
|
if (!empty($value['value']) && !is_array($value['value'])) { |
66
|
|
|
|
67
|
|
|
$node = $xml->addChild($key, htmlspecialchars($value)); |
68
|
|
|
|
69
|
|
|
foreach ($attributes as $attr => $val) { |
70
|
|
|
$node->addAttribute($attr, $val); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
} else { |
74
|
|
|
|
75
|
|
|
$node = $this->convertArrayToXml($xml, $value); |
|
|
|
|
76
|
|
|
|
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
} else if (!empty($value['value']) && is_array($value['value'])) { |
80
|
|
|
|
81
|
|
|
$node = $this->convertArrayToXml($xml, $value['value']); |
|
|
|
|
82
|
|
|
|
83
|
|
|
} else { |
84
|
|
|
|
85
|
|
|
$node = $xml->addChild($key, htmlspecialchars($value)); |
86
|
|
|
|
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
|
90
|
|
|
|
91
|
|
|
return $node; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param array $content |
96
|
|
|
* @return self |
97
|
|
|
* @codeCoverageIgnore |
98
|
|
|
*/ |
99
|
|
|
public function setContent($content = []) |
100
|
|
|
{ |
101
|
|
|
$this->setResponseHeader(['Content-Type' => 'text/xml']); |
102
|
|
|
|
103
|
|
|
$xml = new \SimpleXMLElement('<?xml version="1.0"?><root></root>'); |
104
|
|
|
$this->convertArrayToXml($xml, $content); |
105
|
|
|
$result = $xml->asXml(); |
106
|
|
|
|
107
|
|
|
$this->content = $result; |
|
|
|
|
108
|
|
|
return $this; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
} |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: