Test Failed
Pull Request — master (#19)
by Flo
03:56
created

XmlResponse   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 12
lcom 1
cbo 1
dl 0
loc 79
rs 10
c 2
b 1
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
D generateXmlElement() 0 28 9
A setContent() 0 21 2
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;
0 ignored issues
show
Documentation Bug introduced by
It seems like $result of type string is incompatible with the declared type array of property $content.

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..

Loading history...
87
        return $this;
88
    }
89
90
}