AbstractSubjectType   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 18
c 1
b 0
f 0
dl 0
loc 81
rs 10
wmc 6

5 Methods

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