Passed
Pull Request — master (#267)
by Tim
07:30
created

BaseID::validateContent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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