Completed
Push — master ( 9e0037...b2adab )
by Siad
12:57
created

XMLFragment::getDoc()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
ccs 2
cts 2
cp 1
crap 1
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
/**
21
 * Use this class as a nested element if you want to get a literal DOM
22
 * fragment of something nested into your task/type.
23
 *
24
 * <p>This is useful for tasks that want to deal with the "real" XML
25
 * from the build file instead of objects.</p>
26
 *
27
 * @author Siad Ardroumli <[email protected]>
28
 * @package phing.system.util
29
 */
30
class XMLFragment extends ProjectComponent implements CustomChildCreator
31
{
32
    /**
33
     * @var DOMDocument
34
     */
35
    private $doc;
36
37
    /**
38
     * @var DOMDocumentFragment
39
     */
40
    private $fragment;
41
42 2
    public function __construct()
43
    {
44 2
        parent::__construct();
45 2
        $this->doc = new DOMDocument('1.0', 'UTF-8');
46 2
        $this->fragment = $this->doc->createDocumentFragment();
47 2
    }
48
49
    /**
50
     * @inheritDoc
51
     */
52 1
    public function customChildCreator($elementName, Project $project)
53
    {
54 1
        $e = $this->doc->createElement($elementName);
55 1
        $this->fragment->appendChild($e);
56
57 1
        return new XMLChild($project, $this->doc, $this->fragment, $e);
58
    }
59
60
    /**
61
     * Add nested text, expanding properties as we go
62
     * @param string $s the text to add
63
     */
64
    public function addText(string $s)
65
    {
66
        $s = $this->getProject()->replaceProperties($s);
67
        //only text nodes that are non null after property expansion are added
68
        if ($s !== null && trim($s) !== '') {
69
            $t = $this->doc->createTextNode(trim($s));
70
            $this->fragment->appendChild($t);
71
        }
72
    }
73
74
    /**
75
     * @return DOMDocumentFragment
76
     */
77 2
    public function getFragment(): DOMDocumentFragment
78
    {
79 2
        return $this->fragment;
80
    }
81
82
    /**
83
     * @return DOMDocument
84
     */
85 1
    public function getDoc(): DOMDocument
86
    {
87 1
        return $this->doc;
88
    }
89
}
90