Output::getOutput()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 1
Metric Value
c 4
b 0
f 1
dl 0
loc 17
rs 9.4285
cc 2
eloc 10
nc 2
nop 1
1
<?php
2
3
namespace Thepixeldeveloper\Sitemap;
4
5
use XMLWriter;
6
7
/**
8
 * Class Output
9
 *
10
 * @package Thepixeldeveloper\Sitemap
11
 */
12
class Output
13
{
14
    /**
15
     * Is the output indented.
16
     *
17
     * @var boolean
18
     */
19
    protected $indented = true;
20
21
    /**
22
     * What string is used for indentation.
23
     *
24
     * @var string
25
     */
26
    protected $indentString = '    ';
27
28
    /**
29
     * Processing instructions.
30
     *
31
     * @var array
32
     */
33
    protected $processingInstructions = [];
34
35
    /**
36
     * Renders the Sitemap as an XML string.
37
     *
38
     * @param OutputInterface $collection
39
     *
40
     * @return string
41
     */
42
    public function getOutput(OutputInterface $collection)
43
    {
44
        $xmlWriter = new XMLWriter();
45
        $xmlWriter->openMemory();
46
        $xmlWriter->setIndent($this->isIndented());
47
        $xmlWriter->startDocument('1.0', 'UTF-8');
48
        
49
        foreach ($this->processingInstructions as $target => $content) {
50
            $xmlWriter->writePi($target, $content);
51
        }
52
        
53
        $xmlWriter->setIndentString($this->getIndentString());
54
55
        $collection->generateXML($xmlWriter);
56
57
        return trim($xmlWriter->flush(true));
58
    }
59
60
    /**
61
     * Output indented?
62
     *
63
     * @return boolean
64
     */
65
    public function isIndented()
66
    {
67
        return $this->indented;
68
    }
69
70
    /**
71
     * Indent the output?
72
     *
73
     * @param boolean $indented
74
     *
75
     * @return $this
76
     */
77
    public function setIndented($indented)
78
    {
79
        $this->indented = $indented;
80
81
        return $this;
82
    }
83
84
    /**
85
     * String used for indentation.
86
     *
87
     * @return string
88
     */
89
    public function getIndentString()
90
    {
91
        return $this->indentString;
92
    }
93
94
    /**
95
     * Set the string used for indentation.
96
     * 
97
     * @param string $indentString
98
     *
99
     * @return $this
100
     */
101
    public function setIndentString($indentString)
102
    {
103
        $this->indentString = $indentString;
104
105
        return $this;
106
    }
107
108
    /**
109
     * Adds a processing instruction.
110
     *
111
     * @param string $target
112
     * @param string $content
113
     *
114
     * @return $this
115
     */
116
    public function addProcessingInstruction($target, $content)
117
    {
118
        $this->processingInstructions[$target] = $content;
119
120
        return $this;
121
    }
122
}
123