Passed
Pull Request — master (#374)
by Tim
02:33
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;
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;
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...
26
27
28
    /** @var string */
29
    public const TEXTCONTENT_TYPE = SAMLStringValue::class;
30
31
32
    /**
33
     * Initialize a saml:NameIDType from scratch
34
     *
35
     * @param \SimpleSAML\SAML2\Type\SAMLStringValue $value
36
     * @param \SimpleSAML\SAML2\Type\SAMLAnyURIValue|null $Format
37
     * @param \SimpleSAML\SAML2\Type\SAMLStringValue|null $SPProvidedID
38
     * @param \SimpleSAML\SAML2\Type\SAMLStringValue|null $NameQualifier
39
     * @param \SimpleSAML\SAML2\Type\SAMLStringValue|null $SPNameQualifier
40
     */
41
    protected function __construct(
42
        SAMLStringValue|SAMLAnyURIValue $value,
43
        protected ?SAMLStringValue $NameQualifier = null,
44
        protected ?SAMLStringValue $SPNameQualifier = null,
45
        protected ?SAMLAnyURIValue $Format = null,
46
        protected ?SAMLStringValue $SPProvidedID = null,
47
    ) {
48
        $this->setContent($value);
49
    }
50
51
52
    /**
53
     * Collect the value of the Format-property
54
     *
55
     * @return \SimpleSAML\SAML2\Type\SAMLAnyURIValue|null
56
     */
57
    public function getFormat(): ?SAMLAnyURIValue
58
    {
59
        return $this->Format;
60
    }
61
62
63
    /**
64
     * Collect the value of the SPProvidedID-property
65
     *
66
     * @return \SimpleSAML\SAML2\Type\SAMLStringValue|null
67
     */
68
    public function getSPProvidedID(): ?SAMLStringValue
69
    {
70
        return $this->SPProvidedID;
71
    }
72
73
74
    /**
75
     * Convert XML into an NameID
76
     *
77
     * @param \DOMElement $xml The XML element we should load
78
     * @return static
79
     *
80
     * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException
81
     *   if the qualified name of the supplied element is wrong
82
     */
83
    public static function fromXML(DOMElement $xml): static
84
    {
85
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
86
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
87
88
        return new static(
89
            SAMLStringValue::fromString($xml->textContent),
90
            self::getOptionalAttribute($xml, 'NameQualifier', SAMLStringValue::class, null),
91
            self::getOptionalAttribute($xml, 'SPNameQualifier', SAMLStringValue::class, null),
92
            self::getOptionalAttribute($xml, 'Format', SAMLAnyURIValue::class, null),
93
            self::getOptionalAttribute($xml, 'SPProvidedID', SAMLStringValue::class, null),
94
        );
95
    }
96
97
98
    /**
99
     * Convert this NameIDType to XML.
100
     *
101
     * @param \DOMElement $parent The element we are converting to XML.
102
     * @return \DOMElement The XML element after adding the data corresponding to this NameIDType.
103
     */
104
    public function toXML(?DOMElement $parent = null): DOMElement
105
    {
106
        $e = $this->instantiateParentElement($parent);
107
        $e->textContent = strval($this->getContent());
108
109
        if ($this->getNameQualifier() !== null) {
110
            $e->setAttribute('NameQualifier', $this->getNameQualifier()->getValue());
111
        }
112
113
        if ($this->getSPNameQualifier() !== null) {
114
            $e->setAttribute('SPNameQualifier', $this->getSPNameQualifier()->getValue());
115
        }
116
117
        if ($this->getFormat() !== null) {
118
            $e->setAttribute('Format', $this->getFormat()->getValue());
119
        }
120
121
        if ($this->getSPProvidedID() !== null) {
122
            $e->setAttribute('SPProvidedID', $this->getSPProvidedID()->getValue());
123
        }
124
125
        return $e;
126
    }
127
}
128