AbstractSubjectType::fromXML()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 9
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 14
rs 9.9666
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\XMLSchema\Exception\{InvalidDOMElementException, MissingElementException, TooManyElementsException};
10
11
/**
12
 * SAML SubjectType abstract data type.
13
 *
14
 * @package simplesamlphp/saml11
15
 */
16
abstract class AbstractSubjectType extends AbstractSamlElement
17
{
18
    /**
19
     * Initialize a saml:SubjectType from scratch
20
     *
21
     * @param \SimpleSAML\SAML11\XML\saml\SubjectConfirmation|null $subjectConfirmation
22
     * @param \SimpleSAML\SAML11\XML\saml\NameIdentifier|null $nameIdentifier
23
     */
24
    final public function __construct(
25
        protected ?SubjectConfirmation $subjectConfirmation = null,
26
        protected ?NameIdentifier $nameIdentifier = null,
27
    ) {
28
        if ($nameIdentifier === null) {
29
            Assert::notNull($subjectConfirmation, MissingElementException::class);
30
        }
31
    }
32
33
34
    /**
35
     * Collect the value of the subjectConfirmation-property
36
     *
37
     * @return \SimpleSAML\SAML11\XML\saml\SubjectConfirmation|null
38
     */
39
    public function getSubjectConfirmation(): ?SubjectConfirmation
40
    {
41
        return $this->subjectConfirmation;
42
    }
43
44
45
    /**
46
     * Collect the value of the nameIdentifier-property
47
     *
48
     * @return \SimpleSAML\SAML11\XML\saml\NameIdentifier|null
49
     */
50
    public function getNameIdentifier(): ?NameIdentifier
51
    {
52
        return $this->nameIdentifier;
53
    }
54
55
56
    /**
57
     * Convert XML into an SubjectType
58
     *
59
     * @param \DOMElement $xml The XML element we should load
60
     * @return static
61
     *
62
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
63
     *   if the qualified name of the supplied element is wrong
64
     */
65
    public static function fromXML(DOMElement $xml): static
66
    {
67
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
68
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
69
70
        $subjectConfirmation = SubjectConfirmation::getChildrenOfClass($xml);
71
        Assert::maxCount($subjectConfirmation, 1, TooManyElementsException::class);
72
73
        $nameIdentifier = NameIdentifier::getChildrenOfClass($xml);
74
        Assert::maxCount($nameIdentifier, 1, TooManyElementsException::class);
75
76
        return new static(
77
            array_pop($subjectConfirmation),
78
            array_pop($nameIdentifier),
79
        );
80
    }
81
82
83
    /**
84
     * Convert this SubjectType to XML.
85
     *
86
     * @param \DOMElement $parent The element we are converting to XML.
87
     * @return \DOMElement The XML element after adding the data corresponding to this SubjectType.
88
     */
89
    public function toXML(?DOMElement $parent = null): DOMElement
90
    {
91
        $e = $this->instantiateParentElement($parent);
92
93
        $this->getNameIdentifier()?->toXML($e);
94
        $this->getSubjectConfirmation()?->toXML($e);
95
96
        return $e;
97
    }
98
}
99