Passed
Push — master ( 0aff4f...e61fad )
by Tim
02:24
created

AdditionalMetadataLocation::toXML()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\SAML2\XML\md;
6
7
use DOMElement;
8
use Exception;
9
use SimpleSAML\Assert\Assert;
10
use SimpleSAML\XML\Exception\InvalidDOMElementException;
11
use SimpleSAML\XML\Exception\SchemaViolationException;
12
use SimpleSAML\XML\XMLURIElementTrait;
13
14
use function trim;
15
16
/**
17
 * Class representing SAML 2 metadata AdditionalMetadataLocation element.
18
 *
19
 * @package simplesamlphp/saml2
20
 */
21
final class AdditionalMetadataLocation extends AbstractMdElement
22
{
23
    use XMLURIElementTrait;
24
25
    /**
26
     * The namespace of this metadata.
27
     *
28
     * @var string
29
     */
30
    protected string $namespace;
31
32
33
    /**
34
     * Create a new instance of AdditionalMetadataLocation
35
     *
36
     * @param string $namespace
37
     * @param string $location
38
     */
39
    public function __construct(string $namespace, string $location)
40
    {
41
        $this->setNamespace($namespace);
42
        $this->setContent($location);
43
    }
44
45
46
    /**
47
     * Collect the value of the namespace-property
48
     *
49
     * @return string
50
     */
51
    public function getNamespace(): string
52
    {
53
        return $this->namespace;
54
    }
55
56
57
    /**
58
     * Set the value of the namespace-property
59
     *
60
     * @param string $namespace
61
     * @throws \SimpleSAML\Assert\AssertionFailedException
62
     */
63
    protected function setNamespace(string $namespace): void
64
    {
65
        Assert::validURI($namespace, SchemaViolationException::class); // Covers the empty string
66
        $this->namespace = $namespace;
67
    }
68
69
70
    /**
71
     * Initialize an AdditionalMetadataLocation element.
72
     *
73
     * @param \DOMElement $xml The XML element we should load.
74
     * @return self
75
     *
76
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException if the qualified name of the supplied element is wrong
77
     * @throws \SimpleSAML\XML\Exception\MissingAttributeException if the supplied element is missing any of the mandatory attributes
78
     */
79
    public static function fromXML(DOMElement $xml): object
80
    {
81
        Assert::same($xml->localName, 'AdditionalMetadataLocation', InvalidDOMElementException::class);
82
        Assert::same($xml->namespaceURI, AdditionalMetadataLocation::NS, InvalidDOMElementException::class);
83
84
        $namespace = self::getAttribute($xml, 'namespace');
85
86
        return new self($namespace, trim($xml->textContent));
87
    }
88
89
90
    /**
91
     * Convert this AdditionalMetadataLocation to XML.
92
     *
93
     * @param \DOMElement|null $parent The element we should append to.
94
     * @return \DOMElement This AdditionalMetadataLocation-element.
95
     */
96
    public function toXML(DOMElement $parent = null): DOMElement
97
    {
98
        $e = $this->instantiateParentElement($parent);
99
        $e->textContent = $this->content;
100
        $e->setAttribute('namespace', $this->namespace);
101
102
        return $e;
103
    }
104
}
105