|
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 $dom |
|
27
|
|
|
* @param $data |
|
28
|
|
|
* @return bool|\DOMElement |
|
29
|
|
|
*/ |
|
30
|
|
|
protected function generateXmlElement( \DOMDocument $dom, $data ) |
|
31
|
|
|
{ |
|
32
|
|
|
if ( empty( $data['name'] ) ) |
|
33
|
|
|
return false; |
|
34
|
|
|
|
|
35
|
|
|
// Create the element |
|
36
|
|
|
$element_value = ( ! empty( $data['value'] ) ) ? $data['value'] : null; |
|
37
|
|
|
$element = $dom->createElement( $data['name'], $element_value ); |
|
38
|
|
|
|
|
39
|
|
|
// Add any attributes |
|
40
|
|
|
if ( ! empty( $data['attributes'] ) && is_array( $data['attributes'] ) ) { |
|
41
|
|
|
foreach ( $data['attributes'] as $attribute_key => $attribute_value ) { |
|
42
|
|
|
$element->setAttribute( $attribute_key, $attribute_value ); |
|
43
|
|
|
} |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
// Any other items in the data array should be child elements |
|
47
|
|
|
foreach ( $data as $data_key => $child_data ) { |
|
48
|
|
|
if ( ! is_numeric( $data_key ) ) |
|
49
|
|
|
continue; |
|
50
|
|
|
|
|
51
|
|
|
$child = $this->generateXmlElement( $dom, $child_data ); |
|
52
|
|
|
if ( $child ) |
|
53
|
|
|
$element->appendChild( $child ); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
return $element; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @param array $content |
|
61
|
|
|
* @return self |
|
62
|
|
|
* @codeCoverageIgnore |
|
63
|
|
|
*/ |
|
64
|
|
|
public function setContent($content = []) |
|
65
|
|
|
{ |
|
66
|
|
|
$this->setResponseHeader(['Content-Type' => 'text/xml']); |
|
67
|
|
|
|
|
68
|
|
|
$doc = new \DOMDocument(); |
|
69
|
|
|
$doc->xmlVersion = '1.0'; |
|
70
|
|
|
$doc->encoding = 'UTF-8'; |
|
71
|
|
|
$child = $this->generateXmlElement($doc, $content); |
|
72
|
|
|
|
|
73
|
|
|
if ( $child ) { |
|
74
|
|
|
$doc->appendChild($child); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
$doc->formatOutput = true; // Add whitespace to make easier to read XML |
|
78
|
|
|
$xml = $doc->saveXML(); |
|
79
|
|
|
|
|
80
|
|
|
$result = $xml; |
|
81
|
|
|
|
|
82
|
|
|
$this->content = $result; |
|
|
|
|
|
|
83
|
|
|
return $this; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
} |
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..