Passed
Push — master ( 25fd26...1cb796 )
by Jaime Pérez
02:17
created

BaseID::setValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SAML2\XML\saml;
6
7
use DOMElement;
8
use SAML2\Constants;
9
use SAML2\XML\IDNameQualifiersTrait;
10
use Webmozart\Assert\Assert;
11
12
/**
13
 * SAML BaseID data type.
14
 *
15
 * @author Tim van Dijen, <[email protected]>
16
 * @package simplesamlphp/saml2
17
 */
18
class BaseID extends AbstractSamlElement implements BaseIdentifierInterface
19
{
20
    use IDNameQualifiersTrait;
21
22
    /** @var string */
23
    protected $value;
24
25
    /** @var string */
26
    protected $type;
27
28
29
    /**
30
     * Initialize a saml:BaseID from scratch
31
     *
32
     * @param string $type
33
     * @param string $value
34
     * @param string|null $NameQualifier
35
     * @param string|null $SPNameQualifier
36
     */
37
    protected function __construct(
38
        string $type,
39
        string $value,
40
        ?string $NameQualifier = null,
41
        ?string $SPNameQualifier = null
42
    ) {
43
        $this->setType($type);
44
        $this->setValue($value);
45
        $this->setNameQualifier($NameQualifier);
46
        $this->setSPNameQualifier($SPNameQualifier);
47
    }
48
49
50
    /**
51
     * @inheritDoc
52
     */
53
    final public function getLocalName(): string
54
    {
55
        // All descendants of this class are supposed to be <saml:BaseID /> elements and shouldn't define a new element
56
        return 'BaseID';
57
    }
58
59
60
    /**
61
     * Get the type of this BaseID (expressed in the xsi:type attribute).
62
     *
63
     * @return string
64
     */
65
    public function getType(): string
66
    {
67
        return $this->type;
68
    }
69
70
71
    /**
72
     * Set the type of this BaseID (in the xsi:type attribute)
73
     *
74
     * @param string $type
75
     * @return void
76
     */
77
    protected function setType(string $type): void
78
    {
79
        Assert::notEmpty($type, 'The "xsi:type" attribute of an identifier cannot be empty.');
80
        $this->type = $type;
81
    }
82
83
84
    /**
85
     * Get the string value of this BaseID.
86
     *
87
     * @return string
88
     */
89
    public function getValue(): string
90
    {
91
        return $this->value;
92
    }
93
94
95
    /**
96
     * Set the string value of this BaseID.
97
     *
98
     * @param string $value
99
     * @return void
100
     */
101
    protected function setValue(string $value): void
102
    {
103
        $this->value = $value;
104
    }
105
106
107
    /**
108
     * Convert XML into an BaseID
109
     *
110
     * @param \DOMElement $xml The XML element we should load
111
     *
112
     * @return \SAML2\XML\saml\BaseID
113
     * @throws \InvalidArgumentException  If xsi:type is not defined or does not implement IdentifierInterface
114
     */
115
    public static function fromXML(DOMElement $xml): object
116
    {
117
        Assert::same($xml->localName, 'BaseID');
118
        Assert::notNull($xml->namespaceURI);
119
        Assert::same($xml->namespaceURI, BaseID::NS);
120
        Assert::true($xml->hasAttributeNS(Constants::NS_XSI, 'type'), 'Missing required xsi:type in <saml:BaseID> element.');
121
122
        $type = $xml->getAttributeNS(Constants::NS_XSI, 'type');
123
124
        return new self(
125
            $type,
126
            trim($xml->textContent),
127
            $xml->hasAttribute('NameQualifier') ? $xml->getAttribute('NameQualifier') : null,
128
            $xml->hasAttribute('SPNameQualifier') ? $xml->getAttribute('SPNameQualifier') : null
129
        );
130
    }
131
132
133
    /**
134
     * Convert this BaseID to XML.
135
     *
136
     * @param \DOMElement $parent The element we are converting to XML.
137
     * @return \DOMElement The XML element after adding the data corresponding to this NameIDType.
138
     */
139
    public function toXML(DOMElement $parent = null): DOMElement
140
    {
141
        $element = $this->instantiateParentElement($parent);
142
143
        if ($this->NameQualifier !== null) {
144
            $element->setAttribute('NameQualifier', $this->NameQualifier);
145
        }
146
147
        if ($this->SPNameQualifier !== null) {
148
            $element->setAttribute('SPNameQualifier', $this->SPNameQualifier);
149
        }
150
151
        $element->textContent = $this->value;
152
153
        $element->setAttributeNS(Constants::NS_XSI, 'xsi:type', $this->type);
154
155
        return $element;
156
    }
157
}
158