DefaultDocument::getNamespace()   A
last analyzed

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
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Inspirum\XML\Builder;
6
7
use DOMDocument;
8
use DOMException;
9
use Inspirum\XML\Exception\Handler;
10
use InvalidArgumentException;
11
use function array_key_exists;
12
use function sprintf;
13
14
final class DefaultDocument extends BaseNode implements Document
15
{
16
    /**
17
     * Map of registered namespaces
18
     *
19
     * @var array<string,string>
20
     */
21
    private array $namespaces = [];
22
23 102
    public function __construct(DOMDocument $document)
24
    {
25 102
        parent::__construct($document, null, $this);
26
    }
27
28 8
    public function validate(string $filename): void
29
    {
30 8
        Handler::withErrorHandlerForDOMDocument(function () use ($filename): void {
31 8
            $validated = $this->getDocument()->schemaValidate($filename);
32 4
            if ($validated === false) {
33 1
                throw new DOMException('\DOMDocument::schemaValidate() method failed');
34
            }
35 8
        });
36
    }
37
38 3
    public function save(string $filename, bool $formatOutput = false): void
39
    {
40 3
        Handler::withErrorHandlerForDOMDocument(function () use ($filename, $formatOutput): void {
41 3
            $this->getDocument()->formatOutput = $formatOutput;
42
43 3
            $saved = $this->getDocument()->save($filename);
44 3
            if ($saved === false) {
45 1
                throw new DOMException('\DOMDocument::save() method failed');
46
            }
47 3
        });
48
    }
49
50 27
    public function registerNamespace(string $localName, string $namespaceURI): void
51
    {
52 27
        $this->namespaces[$localName] = $namespaceURI;
53
    }
54
55 14
    public function hasNamespace(string $localName): bool
56
    {
57 14
        return array_key_exists($localName, $this->namespaces);
58
    }
59
60 15
    public function getNamespace(string $localName): string
61
    {
62 15
        return $this->namespaces[$localName] ?? throw new InvalidArgumentException(sprintf('Namespace [%s] does not exists', $localName));
63
    }
64
65
    /**
66
     * @inheritDoc
67
     */
68 5
    public function getNamespaces(): array
69
    {
70 5
        return $this->namespaces;
71
    }
72
}
73