Passed
Push — master ( c6a4f9...858274 )
by Tim
02:18
created

AdditionalMetadataLocation::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
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\XMLStringElementTrait;
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 XMLStringElementTrait;
0 ignored issues
show
introduced by
The trait SimpleSAML\XML\XMLStringElementTrait requires some properties which are not provided by SimpleSAML\SAML2\XML\md\AdditionalMetadataLocation: $localName, $namespaceURI
Loading history...
24
25
26
    /**
27
     * The namespace of this metadata.
28
     *
29
     * @var string
30
     */
31
    protected string $namespace;
32
33
34
    /**
35
     * Create a new instance of AdditionalMetadataLocation
36
     *
37
     * @param string $namespace
38
     * @param string $location
39
     */
40
    public function __construct(string $namespace, string $location)
41
    {
42
        $this->setNamespace($namespace);
43
        $this->setContent($location);
44
    }
45
46
47
    /**
48
     * Collect the value of the namespace-property
49
     *
50
     * @return string
51
     */
52
    public function getNamespace(): string
53
    {
54
        return $this->namespace;
55
    }
56
57
58
    /**
59
     * Set the value of the namespace-property
60
     *
61
     * @param string $namespace
62
     * @throws \SimpleSAML\Assert\AssertionFailedException
63
     */
64
    protected function setNamespace(string $namespace): void
65
    {
66
        Assert::validURI($namespace, SchemaViolationException::class); // Covers the empty string
67
        $this->namespace = $namespace;
68
    }
69
70
71
    /**
72
     * Validate the content of the element.
73
     *
74
     * @param string $content  The value to go in the XML textContent
75
     * @throws \Exception on failure
76
     * @return void
77
     */
78
    protected function validateContent(string $content): void
79
    {
80
        Assert::validURI($content, SchemaViolationException::class); // Covers the empty string
81
    }
82
83
84
    /**
85
     * Initialize an AdditionalMetadataLocation element.
86
     *
87
     * @param \DOMElement $xml The XML element we should load.
88
     * @return self
89
     *
90
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException if the qualified name of the supplied element is wrong
91
     * @throws \SimpleSAML\XML\Exception\MissingAttributeException if the supplied element is missing any of the mandatory attributes
92
     */
93
    public static function fromXML(DOMElement $xml): object
94
    {
95
        Assert::same($xml->localName, 'AdditionalMetadataLocation', InvalidDOMElementException::class);
96
        Assert::same($xml->namespaceURI, AdditionalMetadataLocation::NS, InvalidDOMElementException::class);
97
98
        $namespace = self::getAttribute($xml, 'namespace');
99
100
        return new self($namespace, trim($xml->textContent));
101
    }
102
103
104
    /**
105
     * Convert this AdditionalMetadataLocation to XML.
106
     *
107
     * @param \DOMElement|null $parent The element we should append to.
108
     * @return \DOMElement This AdditionalMetadataLocation-element.
109
     */
110
    public function toXML(DOMElement $parent = null): DOMElement
111
    {
112
        $e = $this->instantiateParentElement($parent);
113
        $e->textContent = $this->content;
114
        $e->setAttribute('namespace', $this->namespace);
115
116
        return $e;
117
    }
118
}
119