Passed
Pull Request — master (#267)
by Tim
04:21
created

BaseID::setValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\SAML2\XML\saml;
6
7
use DOMElement;
8
use SimpleSAML\Assert\Assert;
9
use SimpleSAML\SAML2\Constants;
10
use SimpleSAML\SAML2\XML\IDNameQualifiersTrait;
11
use SimpleSAML\XML\Exception\InvalidDOMElementException;
12
use SimpleSAML\XML\XMLStringElementTrait;
13
14
/**
15
 * SAML BaseID data type.
16
 *
17
 * @package simplesamlphp/saml2
18
 */
19
class BaseID extends AbstractSamlElement implements BaseIdentifierInterface
20
{
21
    use IDNameQualifiersTrait;
22
    use XMLStringElementTrait;
0 ignored issues
show
introduced by
The trait SimpleSAML\XML\XMLStringElementTrait requires some properties which are not provided by SimpleSAML\SAML2\XML\saml\BaseID: $localName, $namespaceURI
Loading history...
23
24
    /** @var string */
25
    public const LOCALNAME = 'BaseID';
26
27
    /** @var string */
28
    protected string $type;
29
30
31
    /**
32
     * Initialize a saml:BaseID from scratch
33
     *
34
     * @param string $type
35
     * @param string $value
36
     * @param string|null $NameQualifier
37
     * @param string|null $SPNameQualifier
38
     */
39
    protected function __construct(
40
        string $type,
41
        string $value,
42
        ?string $NameQualifier = null,
43
        ?string $SPNameQualifier = null
44
    ) {
45
        $this->setType($type);
46
        $this->setContent($value);
47
        $this->setNameQualifier($NameQualifier);
48
        $this->setSPNameQualifier($SPNameQualifier);
49
    }
50
51
52
    /**
53
     * Get the type of this BaseID (expressed in the xsi:type attribute).
54
     *
55
     * @return string
56
     */
57
    public function getType(): string
58
    {
59
        return $this->type;
60
    }
61
62
63
    /**
64
     * Set the type of this BaseID (in the xsi:type attribute)
65
     *
66
     * @param string $type
67
     */
68
    protected function setType(string $type): void
69
    {
70
        Assert::notWhitespaceOnly($type, 'The "xsi:type" attribute of an identifier cannot be empty.');
71
72
        $this->type = $type;
73
    }
74
75
76
    /**
77
     * Validate the content of the element.
78
     *
79
     * @param string $content  The value to go in the XML textContent
80
     * @throws \Exception on failure
81
     * @return void
82
     */
83
    protected function validateContent(string $content): void
84
    {
85
        Assert::notWhitespaceOnly($content);
86
    }
87
88
89
    /**
90
     * Convert XML into an BaseID
91
     *
92
     * @param \DOMElement $xml The XML element we should load
93
     *
94
     * @return \SimpleSAML\SAML2\XML\saml\BaseID
95
     *
96
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException if the qualified name of the supplied element is wrong
97
     */
98
    public static function fromXML(DOMElement $xml): object
99
    {
100
        Assert::same($xml->localName, 'BaseID', InvalidDOMElementException::class);
101
        Assert::notNull($xml->namespaceURI, InvalidDOMElementException::class);
102
        Assert::same($xml->namespaceURI, BaseID::NS, InvalidDOMElementException::class);
103
        Assert::true(
104
            $xml->hasAttributeNS(Constants::NS_XSI, 'type'),
105
            'Missing required xsi:type in <saml:BaseID> element.',
106
            InvalidDOMElementException::class
107
        );
108
109
        $type = $xml->getAttributeNS(Constants::NS_XSI, 'type');
110
111
        return new self(
112
            $type,
113
            trim($xml->textContent),
114
            self::getAttribute($xml, 'NameQualifier', null),
115
            self::getAttribute($xml, 'SPNameQualifier', null)
116
        );
117
    }
118
119
120
    /**
121
     * Convert this BaseID to XML.
122
     *
123
     * @param \DOMElement $parent The element we are converting to XML.
124
     * @return \DOMElement The XML element after adding the data corresponding to this BaseID.
125
     */
126
    public function toXML(DOMElement $parent = null): DOMElement
127
    {
128
        $element = $this->instantiateParentElement($parent);
129
130
        if ($this->NameQualifier !== null) {
131
            $element->setAttribute('NameQualifier', $this->NameQualifier);
132
        }
133
134
        if ($this->SPNameQualifier !== null) {
135
            $element->setAttribute('SPNameQualifier', $this->SPNameQualifier);
136
        }
137
138
        $element->textContent = $this->content;
139
140
        $element->setAttributeNS(Constants::NS_XSI, 'xsi:type', $this->type);
141
142
        return $element;
143
    }
144
}
145