1<?php
2/*
3 * This file is part of the php-code-coverage package.
4 *
5 * (c) Sebastian Bergmann <sebastian@phpunit.de>
6 *
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
9 */
10
11namespace SebastianBergmann\CodeCoverage\Report\Xml;
12
13class Node
14{
15    /**
16     * @var \DOMDocument
17     */
18    private $dom;
19
20    /**
21     * @var \DOMElement
22     */
23    private $contextNode;
24
25    public function __construct(\DOMElement $context)
26    {
27        $this->setContextNode($context);
28    }
29
30    protected function setContextNode(\DOMElement $context)
31    {
32        $this->dom         = $context->ownerDocument;
33        $this->contextNode = $context;
34    }
35
36    public function getDom()
37    {
38        return $this->dom;
39    }
40
41    protected function getContextNode()
42    {
43        return $this->contextNode;
44    }
45
46    public function getTotals()
47    {
48        $totalsContainer = $this->getContextNode()->firstChild;
49
50        if (!$totalsContainer) {
51            $totalsContainer = $this->getContextNode()->appendChild(
52                $this->dom->createElementNS(
53                    'http://schema.phpunit.de/coverage/1.0',
54                    'totals'
55                )
56            );
57        }
58
59        return new Totals($totalsContainer);
60    }
61
62    public function addDirectory($name)
63    {
64        $dirNode = $this->getDom()->createElementNS(
65            'http://schema.phpunit.de/coverage/1.0',
66            'directory'
67        );
68
69        $dirNode->setAttribute('name', $name);
70        $this->getContextNode()->appendChild($dirNode);
71
72        return new Directory($dirNode);
73    }
74
75    public function addFile($name, $href)
76    {
77        $fileNode = $this->getDom()->createElementNS(
78            'http://schema.phpunit.de/coverage/1.0',
79            'file'
80        );
81
82        $fileNode->setAttribute('name', $name);
83        $fileNode->setAttribute('href', $href);
84        $this->getContextNode()->appendChild($fileNode);
85
86        return new File($fileNode);
87    }
88}
89