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 Report extends File
14{
15    public function __construct($name)
16    {
17        $this->dom = new \DOMDocument;
18        $this->dom->loadXML('<?xml version="1.0" ?><phpunit xmlns="http://schema.phpunit.de/coverage/1.0"><file /></phpunit>');
19
20        $this->contextNode = $this->dom->getElementsByTagNameNS(
21            'http://schema.phpunit.de/coverage/1.0',
22            'file'
23        )->item(0);
24
25        $this->setName($name);
26    }
27
28    private function setName($name)
29    {
30        $this->contextNode->setAttribute('name', $name);
31    }
32
33    public function asDom()
34    {
35        return $this->dom;
36    }
37
38    public function getFunctionObject($name)
39    {
40        $node = $this->contextNode->appendChild(
41            $this->dom->createElementNS(
42                'http://schema.phpunit.de/coverage/1.0',
43                'function'
44            )
45        );
46
47        return new Method($node, $name);
48    }
49
50    public function getClassObject($name)
51    {
52        return $this->getUnitObject('class', $name);
53    }
54
55    public function getTraitObject($name)
56    {
57        return $this->getUnitObject('trait', $name);
58    }
59
60    private function getUnitObject($tagName, $name)
61    {
62        $node = $this->contextNode->appendChild(
63            $this->dom->createElementNS(
64                'http://schema.phpunit.de/coverage/1.0',
65                $tagName
66            )
67        );
68
69        return new Unit($node, $name);
70    }
71}
72