Passed
Push — master ( 25fd26...1cb796 )
by Jaime Pérez
02:17
created

Subject::toXML()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 6
nc 4
nop 1
dl 0
loc 13
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SAML2\XML\saml;
6
7
use DOMElement;
8
use SAML2\Compat\ContainerSingleton;
9
use SAML2\Constants;
10
use SAML2\Utils;
11
use SAML2\XML\IdentifierTrait;
12
use Webmozart\Assert\Assert;
13
14
/**
15
 * Class representing SAML 2 Subject element.
16
 *
17
 * @author Tim van Dijen, <[email protected]>
18
 * @package SimpleSAMLphp
19
 */
20
final class Subject extends AbstractSamlElement
21
{
22
    use IdentifierTrait;
23
24
    /**
25
     * SubjectConfirmation element with extra data for verification of the Subject.
26
     *
27
     * @var \SAML2\XML\saml\SubjectConfirmation[]
28
     */
29
    protected $SubjectConfirmation;
30
31
32
    /**
33
     * Initialize a Subject element.
34
     *
35
     * @param \SAML2\XML\saml\IdentifierInterface|null $identifier
36
     * @param \SAML2\XML\saml\SubjectConfirmation[] $SubjectConfirmation
37
     */
38
    public function __construct(
39
        ?IdentifierInterface $identifier,
40
        array $SubjectConfirmation = []
41
    ) {
42
        if (empty($SubjectConfirmation)) {
43
            Assert::notNull(
44
                $identifier,
45
                'A <saml:Subject> not containing <saml:SubjectConfirmation> should provide exactly one of '
46
                    . '<saml:BaseID>, <saml:NameID> or <saml:EncryptedID>'
47
            );
48
        }
49
50
        $this->setIdentifier($identifier);
51
        $this->setSubjectConfirmation($SubjectConfirmation);
52
    }
53
54
55
    /**
56
     * Collect the value of the SubjectConfirmation-property
57
     *
58
     * @return \SAML2\XML\saml\SubjectConfirmation[]
59
     */
60
    public function getSubjectConfirmation(): array
61
    {
62
        return $this->SubjectConfirmation;
63
    }
64
65
66
    /**
67
     * Set the value of the SubjectConfirmation-property
68
     *
69
     * @param \SAML2\XML\saml\SubjectConfirmation[] $subjectConfirmation
70
     * @return void
71
     */
72
    private function setSubjectConfirmation(array $subjectConfirmation): void
73
    {
74
        $this->SubjectConfirmation = $subjectConfirmation;
75
    }
76
77
78
    /**
79
     * Convert XML into a Subject
80
     *
81
     * @param \DOMElement $xml The XML element we should load
82
     * @return self
83
     * @throws \InvalidArgumentException if the qualified name of the supplied element is wrong
84
     */
85
    public static function fromXML(DOMElement $xml): object
86
    {
87
        Assert::same($xml->localName, 'Subject');
88
        Assert::same($xml->namespaceURI, Subject::NS);
89
90
        $identifier = self::getIdentifierFromXML($xml);
91
        $subjectConfirmation = SubjectConfirmation::getChildrenOfClass($xml);
92
93
        if (empty($subjectConfirmation)) {
94
            Assert::notNull(
95
                $identifier,
96
                'A <saml:Subject> not containing <saml:SubjectConfirmation> should provide' .
97
                ' exactly one of <saml:BaseID>, <saml:NameID> or <saml:EncryptedID>'
98
            );
99
        }
100
101
        return new self(
102
            $identifier,
103
            $subjectConfirmation
104
        );
105
    }
106
107
108
    /**
109
     * Convert this element to XML.
110
     *
111
     * @param  \DOMElement|null $parent The parent element we should append this element to.
112
     * @return \DOMElement This element, as XML.
113
     */
114
    public function toXML(DOMElement $parent = null): DOMElement
115
    {
116
        $e = $this->instantiateParentElement($parent);
117
118
        if ($this->identifier !== null) {
119
            $this->identifier->toXML($e);
120
        }
121
122
        foreach ($this->SubjectConfirmation as $sc) {
123
            $sc->toXML($e);
124
        }
125
126
        return $e;
127
    }
128
}
129