Passed
Pull Request — master (#6)
by Tim
13:32
created

AbstractNameIdentifierType::getValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\SAML11\XML\saml;
6
7
use DOMElement;
8
use SimpleSAML\Assert\Assert;
9
use SimpleSAML\SAML11\Type\{AnyURIValue, StringValue};
10
use SimpleSAML\XML\Exception\InvalidDOMElementException;
11
12
use function strval;
13
14
/**
15
 * SAML NameIdentifierType abstract data type.
16
 *
17
 * @package simplesamlphp/saml11
18
 */
19
abstract class AbstractNameIdentifierType extends AbstractSamlElement
20
{
21
    /**
22
     * Initialize a saml:NameIdentifierType from scratch
23
     *
24
     * @param \SimpleSAML\SAML11\Type\StringValue $value
25
     * @param \SimpleSAML\SAML11\Type\StringValue|null $NameQualifier
26
     * @param \SimpleSAML\SAML11\Type\AnyURIValue|null $Format
27
     */
28
    final public function __construct(
29
        protected StringValue $value,
30
        protected ?StringValue $NameQualifier = null,
31
        protected ?AnyURIValue $Format = null,
32
    ) {
33
    }
34
35
36
    /**
37
     * Collect the value of the value-property
38
     *
39
     * @return \SimpleSAML\SAML11\Type\StringValue
40
     */
41
    public function getValue(): StringValue
42
    {
43
        return $this->value;
44
    }
45
46
47
    /**
48
     * Collect the value of the Format-property
49
     *
50
     * @return \SimpleSAML\SAML11\Type\AnyURIValue|null
51
     */
52
    public function getFormat(): ?AnyURIValue
53
    {
54
        return $this->Format;
55
    }
56
57
58
    /**
59
     * Collect the value of the NameQualifier-property
60
     *
61
     * @return \SimpleSAML\SAML11\Type\StringValue|null
62
     */
63
    public function getNameQualifier(): ?StringValue
64
    {
65
        return $this->NameQualifier;
66
    }
67
68
69
    /**
70
     * Convert XML into an NameIdentifier
71
     *
72
     * @param \DOMElement $xml The XML element we should load
73
     * @return static
74
     *
75
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
76
     *   if the qualified name of the supplied element is wrong
77
     */
78
    public static function fromXML(DOMElement $xml): static
79
    {
80
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
81
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
82
83
        return new static(
84
            StringValue::fromString($xml->textContent),
85
            self::getOptionalAttribute($xml, 'NameQualifier', StringValue::class, null),
86
            self::getOptionalAttribute($xml, 'Format', AnyURIValue::class, null),
87
        );
88
    }
89
90
91
    /**
92
     * Convert this NameIdentifierType to XML.
93
     *
94
     * @param \DOMElement $parent The element we are converting to XML.
95
     * @return \DOMElement The XML element after adding the data corresponding to this NameIdentifierType.
96
     */
97
    public function toXML(?DOMElement $parent = null): DOMElement
98
    {
99
        $e = $this->instantiateParentElement($parent);
100
        $e->textContent = strval($this->getValue());
101
102
        if ($this->getNameQualifier() !== null) {
103
            $e->setAttribute('NameQualifier', strval($this->getNameQualifier()));
104
        }
105
106
        if ($this->getFormat() !== null) {
107
            $e->setAttribute('Format', strval($this->getFormat()));
108
        }
109
110
        return $e;
111
    }
112
}
113