Completed
Push — master ( 4f6af7...d55336 )
by Thijs
14s queued 10s
created

SubjectConfirmation::toXML()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 12
nc 4
nop 1
dl 0
loc 20
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
namespace SAML2\XML\saml;
4
5
use SAML2\Constants;
6
use SAML2\Utils;
7
8
/**
9
 * Class representing SAML 2 SubjectConfirmation element.
10
 *
11
 * @package SimpleSAMLphp
12
 */
13
class SubjectConfirmation
14
{
15
    /**
16
     * The method we can use to verify this Subject.
17
     *
18
     * @var string
19
     */
20
    public $Method;
21
22
    /**
23
     * The NameID of the entity that can use this element to verify the Subject.
24
     *
25
     * @var \SAML2\XML\saml\NameID|null
26
     */
27
    public $NameID;
28
29
    /**
30
     * SubjectConfirmationData element with extra data for verification of the Subject.
31
     *
32
     * @var \SAML2\XML\saml\SubjectConfirmationData|null
33
     */
34
    public $SubjectConfirmationData;
35
36
    /**
37
     * Initialize (and parse? a SubjectConfirmation element.
38
     *
39
     * @param \DOMElement|null $xml The XML element we should load.
40
     * @throws \Exception
41
     */
42
    public function __construct(\DOMElement $xml = null)
43
    {
44
        if ($xml === null) {
45
            return;
46
        }
47
48
        if (!$xml->hasAttribute('Method')) {
49
            throw new \Exception('SubjectConfirmation element without Method attribute.');
50
        }
51
        $this->Method = $xml->getAttribute('Method');
52
53
        $nid = Utils::xpQuery($xml, './saml_assertion:NameID');
54
        if (count($nid) > 1) {
55
            throw new \Exception('More than one NameID in a SubjectConfirmation element.');
56
        } elseif (!empty($nid)) {
57
            $this->NameID = new NameID($nid[0]);
58
        }
59
60
        $scd = Utils::xpQuery($xml, './saml_assertion:SubjectConfirmationData');
61
        if (count($scd) > 1) {
62
            throw new \Exception('More than one SubjectConfirmationData child in a SubjectConfirmation element.');
63
        } elseif (!empty($scd)) {
64
            $this->SubjectConfirmationData = new SubjectConfirmationData($scd[0]);
65
        }
66
    }
67
68
    /**
69
     * Convert this element to XML.
70
     *
71
     * @param  \DOMElement $parent The parent element we should append this element to.
72
     * @return \DOMElement This element, as XML.
73
     */
74
    public function toXML(\DOMElement $parent)
75
    {
76
        assert(is_string($this->Method));
77
        assert(is_null($this->NameID) || $this->NameID instanceof NameID);
78
        assert(is_null($this->SubjectConfirmationData) || $this->SubjectConfirmationData instanceof SubjectConfirmationData);
79
80
        $e = $parent->ownerDocument->createElementNS(Constants::NS_SAML, 'saml:SubjectConfirmation');
81
        $parent->appendChild($e);
82
83
        $e->setAttribute('Method', $this->Method);
84
85
        if (isset($this->NameID)) {
86
            $this->NameID->toXML($e);
87
        }
88
        if (isset($this->SubjectConfirmationData)) {
89
            $this->SubjectConfirmationData->toXML($e);
90
        }
91
92
        return $e;
93
    }
94
}
95