Passed
Push — master ( a3fda0...7c0590 )
by Tim
14:58 queued 13:28
created

NameIDType   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 25
dl 0
loc 104
rs 10
c 0
b 0
f 0
wmc 9
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\SAML2\XML\saml;
6
7
use DOMElement;
8
use SimpleSAML\SAML2\Assert\Assert;
9
use SimpleSAML\SAML2\Type\SAMLAnyURIValue;
10
use SimpleSAML\SAML2\Type\SAMLStringValue;
11
use SimpleSAML\XML\TypedTextContentTrait;
12
use SimpleSAML\XMLSchema\Exception\InvalidDOMElementException;
13
14
use function strval;
15
16
/**
17
 * SAML NameIDType abstract data type.
18
 *
19
 * @package simplesamlphp/saml2
20
 */
21
22
abstract class NameIDType extends AbstractSamlElement implements IdentifierInterface
23
{
24
    use IDNameQualifiersTrait;
25
    use TypedTextContentTrait;
26
27
28
    public const string TEXTCONTENT_TYPE = SAMLStringValue::class;
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_STRING, expecting '=' on line 28 at column 24
Loading history...
29
30
31
    /**
32
     * Initialize a saml:NameIDType from scratch
33
     *
34
     * @param \SimpleSAML\SAML2\Type\SAMLStringValue $value
35
     * @param \SimpleSAML\SAML2\Type\SAMLAnyURIValue|null $Format
36
     * @param \SimpleSAML\SAML2\Type\SAMLStringValue|null $SPProvidedID
37
     * @param \SimpleSAML\SAML2\Type\SAMLStringValue|null $NameQualifier
38
     * @param \SimpleSAML\SAML2\Type\SAMLStringValue|null $SPNameQualifier
39
     */
40
    protected function __construct(
41
        SAMLStringValue|SAMLAnyURIValue $value,
42
        protected ?SAMLStringValue $NameQualifier = null,
43
        protected ?SAMLStringValue $SPNameQualifier = null,
44
        protected ?SAMLAnyURIValue $Format = null,
45
        protected ?SAMLStringValue $SPProvidedID = null,
46
    ) {
47
        $this->setContent($value);
48
    }
49
50
51
    /**
52
     * Collect the value of the Format-property
53
     *
54
     * @return \SimpleSAML\SAML2\Type\SAMLAnyURIValue|null
55
     */
56
    public function getFormat(): ?SAMLAnyURIValue
57
    {
58
        return $this->Format;
59
    }
60
61
62
    /**
63
     * Collect the value of the SPProvidedID-property
64
     *
65
     * @return \SimpleSAML\SAML2\Type\SAMLStringValue|null
66
     */
67
    public function getSPProvidedID(): ?SAMLStringValue
68
    {
69
        return $this->SPProvidedID;
70
    }
71
72
73
    /**
74
     * Convert XML into an NameID
75
     *
76
     * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException
77
     *   if the qualified name of the supplied element is wrong
78
     */
79
    public static function fromXML(DOMElement $xml): static
80
    {
81
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
82
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
83
84
        return new static(
85
            SAMLStringValue::fromString($xml->textContent),
86
            self::getOptionalAttribute($xml, 'NameQualifier', SAMLStringValue::class, null),
87
            self::getOptionalAttribute($xml, 'SPNameQualifier', SAMLStringValue::class, null),
88
            self::getOptionalAttribute($xml, 'Format', SAMLAnyURIValue::class, null),
89
            self::getOptionalAttribute($xml, 'SPProvidedID', SAMLStringValue::class, null),
90
        );
91
    }
92
93
94
    /**
95
     * Convert this NameIDType to XML.
96
     */
97
    public function toXML(?DOMElement $parent = null): DOMElement
98
    {
99
        $e = $this->instantiateParentElement($parent);
100
        $e->textContent = strval($this->getContent());
101
102
        if ($this->getNameQualifier() !== null) {
103
            $e->setAttribute('NameQualifier', $this->getNameQualifier()->getValue());
104
        }
105
106
        if ($this->getSPNameQualifier() !== null) {
107
            $e->setAttribute('SPNameQualifier', $this->getSPNameQualifier()->getValue());
108
        }
109
110
        if ($this->getFormat() !== null) {
111
            $e->setAttribute('Format', $this->getFormat()->getValue());
112
        }
113
114
        if ($this->getSPProvidedID() !== null) {
115
            $e->setAttribute('SPProvidedID', $this->getSPProvidedID()->getValue());
116
        }
117
118
        return $e;
119
    }
120
}
121