Passed
Push — main ( ddd1a4...b98cb8 )
by Michiel
08:47
created

XMLPDOResultFormatter::getPreferredOutfile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
ccs 0
cts 2
cp 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
/**
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the LGPL. For more information please see
17
 * <http://phing.info>.
18
 */
19
20
namespace Phing\Task\System\Pdo;
21
22
use DOMDocument;
23
use DOMElement;
24
use Phing\Io\File;
25
26
/**
27
 * XML formatter for PDO results.
28
 *
29
 * This class reprsents the output of a query using a simple XML schema.
30
 *
31
 * <results>
32
 *  <row>
33
 *   <col name="id">value</col>
34
 *   <col name="name">value2</col>
35
 *  </row>
36
 *  <row>
37
 *   <col name="id">value</col>
38
 *   <col name="name">value2</col>
39
 *  </row>
40
 * </results>
41
 *
42
 * The actual names of the colums will depend on the fetchmode that was used
43
 * with PDO.
44
 *
45
 * @author  Hans Lellelid <[email protected]>
46
 * @package phing.tasks.ext.pdo
47
 * @since   2.3.0
48
 */
49
class XMLPDOResultFormatter extends PDOResultFormatter
50
{
51
52
    /**
53
     * The XML document being created.
54
     *
55
     * @var DOMDocument
56
     */
57
    private $doc;
58
59
    /**
60
     * @var DOMElement
61
     */
62
    private $rootNode;
63
64
    /**
65
     * XML document encoding
66
     *
67
     * @var string
68
     */
69
    private $encoding;
70
71
    /**
72
     * @var boolean
73
     */
74
    private $formatOutput = true;
75
76
    /**
77
     * Set the DOM document encoding.
78
     *
79
     * @param string $v
80
     */
81
    public function setEncoding($v)
82
    {
83
        $this->encoding = $v;
84
    }
85
86
    /**
87
     * @param boolean $v
88
     */
89
    public function setFormatOutput($v)
90
    {
91
        $this->formatOutput = (bool) $v;
92
    }
93
94
    public function initialize()
95
    {
96
        $this->doc = new DOMDocument("1.0", $this->encoding);
97
        $this->rootNode = $this->doc->createElement('results');
98
        $this->doc->appendChild($this->rootNode);
99
        $this->doc->formatOutput = $this->formatOutput;
100
    }
101
102
    /**
103
     * Processes a specific row from PDO result set.
104
     *
105
     * @param array $row Row of PDO result set.
106
     */
107
    public function processRow($row)
108
    {
109
        $rowNode = $this->doc->createElement('row');
110
        $this->rootNode->appendChild($rowNode);
111
112
        foreach ($row as $columnName => $columnValue) {
113
            $colNode = $this->doc->createElement('column');
114
            $colNode->setAttribute('name', $columnName);
115
116
            if ($columnValue != null) {
117
                $columnValue = trim($columnValue);
118
                $colNode->nodeValue = $columnValue;
119
            }
120
            $rowNode->appendChild($colNode);
121
        }
122
    }
123
124
    /**
125
     * Gets a preferred filename for an output file.
126
     *
127
     * If no filename is specified, this is where the results will be placed
128
     * (unless usefile=false).
129
     *
130
     * @return string
131
     */
132
    public function getPreferredOutfile()
133
    {
134
        return new File('results.xml');
135
    }
136
137
    /**
138
     * Write XML to file and free the DOM objects.
139
     */
140
    public function close()
141
    {
142
        $this->out->write($this->doc->saveXML());
143
        $this->rootNode = null;
144
        $this->doc = null;
145
        parent::close();
146
    }
147
}
148