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
|
|
|
/** |
16
|
|
|
* XmlResponse constructor. |
17
|
|
|
* |
18
|
|
|
* @param array $content |
19
|
|
|
*/ |
20
|
|
|
public function __construct ($content = []) |
21
|
|
|
{ |
22
|
|
|
parent::__construct($content); |
23
|
|
|
$this->setContent($content); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** @var array */ |
27
|
|
|
protected $content = []; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param $dom |
31
|
|
|
* @param $data |
32
|
|
|
* @return bool|\DOMElement |
33
|
|
|
*/ |
34
|
|
|
protected function generateXmlElement(\DOMDocument $dom, $data ) |
35
|
|
|
{ |
36
|
|
|
if ( empty( $data['name'] ) ) |
37
|
|
|
return false; |
38
|
|
|
|
39
|
|
|
// Create the element |
40
|
|
|
$element_value = ( ! empty( $data['value'] ) ) ? $data['value'] : null; |
41
|
|
|
$element = $dom->createElement( $data['name'], $element_value ); |
42
|
|
|
|
43
|
|
|
// Add any attributes |
44
|
|
|
if ( ! empty( $data['attributes'] ) && is_array( $data['attributes'] ) ) { |
45
|
|
|
foreach ( $data['attributes'] as $attribute_key => $attribute_value ) { |
46
|
|
|
$element->setAttribute( $attribute_key, $attribute_value ); |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
// Any other items in the data array should be child elements |
51
|
|
|
foreach ( $data as $data_key => $child_data ) { |
52
|
|
|
if ( ! is_numeric( $data_key ) ) |
53
|
|
|
continue; |
54
|
|
|
|
55
|
|
|
$child = $this->generateXmlElement( $dom, $child_data ); |
56
|
|
|
if ( $child ) |
57
|
|
|
$element->appendChild( $child ); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return $element; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param array $content |
65
|
|
|
* @return self |
66
|
|
|
* @codeCoverageIgnore |
67
|
|
|
*/ |
68
|
|
|
public function setContent($content = []) |
69
|
|
|
{ |
70
|
|
|
$this->setResponseHeader(['Content-Type' => 'text/xml']); |
71
|
|
|
|
72
|
|
|
$doc = new \DOMDocument(); |
73
|
|
|
$doc->xmlVersion = '1.0'; |
74
|
|
|
$doc->encoding = 'UTF-8'; |
75
|
|
|
$child = $this->generateXmlElement($doc, $content); |
76
|
|
|
|
77
|
|
|
if ( $child ) { |
78
|
|
|
$doc->appendChild($child); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$doc->formatOutput = true; // Add whitespace to make easier to read XML |
82
|
|
|
$xml = $doc->saveXML(); |
83
|
|
|
|
84
|
|
|
$result = $xml; |
85
|
|
|
|
86
|
|
|
$this->content = $result; |
|
|
|
|
87
|
|
|
return $this; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
} |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..