Passed
Pull Request — master (#374)
by Tim
02:35
created

NameIDType::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\SAML2\Assert\Assert;
9
use SimpleSAML\SAML2\Type\{SAMLAnyURIValue, SAMLStringValue};
10
use SimpleSAML\XML\Exception\InvalidDOMElementException;
11
use SimpleSAML\XML\TypedTextContentTrait;
12
13
use function strval;
14
15
/**
16
 * SAML NameIDType abstract data type.
17
 *
18
 * @package simplesamlphp/saml2
19
 */
20
21
abstract class NameIDType extends AbstractSamlElement implements IdentifierInterface
22
{
23
    use IDNameQualifiersTrait;
24
    use TypedTextContentTrait;
0 ignored issues
show
introduced by
The trait SimpleSAML\XML\TypedTextContentTrait requires some properties which are not provided by SimpleSAML\SAML2\XML\saml\NameIDType: $localName, $namespaceURI
Loading history...
25
26
    /** @var string */
27
    public const TEXTCONTENT_TYPE = SAMLStringValue::class;
28
29
30
    /**
31
     * Initialize a saml:NameIDType from scratch
32
     *
33
     * @param \SimpleSAML\SAML2\Type\SAMLStringValue $value
34
     * @param \SimpleSAML\SAML2\Type\SAMLAnyURIValue|null $Format
35
     * @param \SimpleSAML\SAML2\Type\SAMLStringValue|null $SPProvidedID
36
     * @param \SimpleSAML\SAML2\Type\SAMLStringValue|null $NameQualifier
37
     * @param \SimpleSAML\SAML2\Type\SAMLStringValue|null $SPNameQualifier
38
     */
39
    protected function __construct(
40
        SAMLStringValue|SAMLAnyURIValue $value,
41
        protected ?SAMLStringValue $NameQualifier = null,
42
        protected ?SAMLStringValue $SPNameQualifier = null,
43
        protected ?SAMLAnyURIValue $Format = null,
44
        protected ?SAMLStringValue $SPProvidedID = null,
45
    ) {
46
        $this->setContent($value);
47
    }
48
49
50
    /**
51
     * Collect the value of the Format-property
52
     *
53
     * @return \SimpleSAML\SAML2\Type\SAMLAnyURIValue|null
54
     */
55
    public function getFormat(): ?SAMLAnyURIValue
56
    {
57
        return $this->Format;
58
    }
59
60
61
    /**
62
     * Collect the value of the SPProvidedID-property
63
     *
64
     * @return \SimpleSAML\SAML2\Type\SAMLStringValue|null
65
     */
66
    public function getSPProvidedID(): ?SAMLStringValue
67
    {
68
        return $this->SPProvidedID;
69
    }
70
71
72
    /**
73
     * Convert XML into an NameID
74
     *
75
     * @param \DOMElement $xml The XML element we should load
76
     * @return static
77
     *
78
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
79
     *   if the qualified name of the supplied element is wrong
80
     */
81
    public static function fromXML(DOMElement $xml): static
82
    {
83
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
84
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
85
86
        return new static(
87
            SAMLStringValue::fromString($xml->textContent),
88
            self::getOptionalAttribute($xml, 'NameQualifier', SAMLStringValue::class, null),
89
            self::getOptionalAttribute($xml, 'SPNameQualifier', SAMLStringValue::class, null),
90
            self::getOptionalAttribute($xml, 'Format', SAMLAnyURIValue::class, null),
91
            self::getOptionalAttribute($xml, 'SPProvidedID', SAMLStringValue::class, null),
92
        );
93
    }
94
95
96
    /**
97
     * Convert this NameIDType to XML.
98
     *
99
     * @param \DOMElement $parent The element we are converting to XML.
100
     * @return \DOMElement The XML element after adding the data corresponding to this NameIDType.
101
     */
102
    public function toXML(?DOMElement $parent = null): DOMElement
103
    {
104
        $e = $this->instantiateParentElement($parent);
105
        $e->textContent = strval($this->getContent());
106
107
        if ($this->getNameQualifier() !== null) {
108
            $e->setAttribute('NameQualifier', $this->getNameQualifier()->getValue());
109
        }
110
111
        if ($this->getSPNameQualifier() !== null) {
112
            $e->setAttribute('SPNameQualifier', $this->getSPNameQualifier()->getValue());
113
        }
114
115
        if ($this->getFormat() !== null) {
116
            $e->setAttribute('Format', $this->getFormat()->getValue());
117
        }
118
119
        if ($this->getSPProvidedID() !== null) {
120
            $e->setAttribute('SPProvidedID', $this->getSPProvidedID()->getValue());
121
        }
122
123
        return $e;
124
    }
125
}
126