AbstractStatement::getXsiType()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\SAML11\XML\saml;
6
7
use DOMElement;
8
use SimpleSAML\SAML11\Assert\Assert;
9
use SimpleSAML\SAML11\Constants as C;
10
use SimpleSAML\SAML11\Utils;
11
use SimpleSAML\SAML11\XML\ExtensionPointInterface;
12
use SimpleSAML\SAML11\XML\ExtensionPointTrait;
13
use SimpleSAML\XML\Attribute as XMLAttribute;
14
use SimpleSAML\XML\Chunk;
15
use SimpleSAML\XML\SchemaValidatableElementInterface;
16
use SimpleSAML\XML\SchemaValidatableElementTrait;
17
use SimpleSAML\XMLSchema\Constants as C_XSI;
18
use SimpleSAML\XMLSchema\Exception\InvalidDOMElementException;
19
use SimpleSAML\XMLSchema\Exception\SchemaViolationException;
20
use SimpleSAML\XMLSchema\Type\QNameValue;
21
22
use function strval;
23
24
/**
25
 * Class implementing the <saml:Statement> extension point.
26
 *
27
 * @package simplesamlphp/saml11
28
 */
29
abstract class AbstractStatement extends AbstractStatementType implements
30
    ExtensionPointInterface,
31
    SchemaValidatableElementInterface
32
{
33
    use ExtensionPointTrait;
34
    use SchemaValidatableElementTrait;
35
36
37
    /** @var string */
38
    public const LOCALNAME = 'Statement';
39
40
41
    /**
42
     * Initialize a custom saml:Statement element.
43
     *
44
     * @param \SimpleSAML\XMLSchema\Type\QNameValue $type
45
     */
46
    protected function __construct(
47
        protected QNameValue $type,
48
    ) {
49
    }
50
51
52
    /**
53
     * @return \SimpleSAML\XMLSchema\Type\QNameValue
54
     */
55
    public function getXsiType(): QNameValue
56
    {
57
        return $this->type;
58
    }
59
60
61
    /**
62
     * Convert an XML element into a Statement.
63
     *
64
     * @param \DOMElement $xml The root XML element
65
     * @return static
66
     *
67
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
68
     *   if the qualified name of the supplied element is wrong
69
     */
70
    public static function fromXML(DOMElement $xml): static
71
    {
72
        Assert::same($xml->localName, 'Statement', InvalidDOMElementException::class);
73
        Assert::same($xml->namespaceURI, C::NS_SAML, InvalidDOMElementException::class);
74
        Assert::true(
75
            $xml->hasAttributeNS(C_XSI::NS_XSI, 'type'),
76
            'Missing required xsi:type in <saml:Statement> element.',
77
            SchemaViolationException::class,
78
        );
79
80
        $type = QNameValue::fromDocument($xml->getAttributeNS(C_XSI::NS_XSI, 'type'), $xml);
81
82
        // now check if we have a handler registered for it
83
        $handler = Utils::getContainer()->getExtensionHandler($type);
84
        if ($handler === null) {
85
            // we don't have a handler, proceed with unknown statement
86
            return new UnknownStatement(new Chunk($xml), $type);
87
        }
88
89
        Assert::subclassOf(
90
            $handler,
91
            AbstractStatement::class,
92
            'Elements implementing Statement must extend \SimpleSAML\SAML11\XML\saml\AbstractStatementType.',
93
        );
94
        return $handler::fromXML($xml);
95
    }
96
97
98
    /**
99
     * Convert this Statement to XML.
100
     *
101
     * @param \DOMElement $parent The element we are converting to XML.
102
     * @return \DOMElement The XML element after adding the data corresponding to this Statement.
103
     */
104
    public function toXML(?DOMElement $parent = null): DOMElement
105
    {
106
        $e = $this->instantiateParentElement($parent);
107
108
        if (!$e->lookupPrefix($this->getXsiType()->getNamespaceURI()->getValue())) {
109
            $e->setAttributeNS(
110
                'http://www.w3.org/2000/xmlns/',
111
                'xmlns:' . static::getXsiTypePrefix(),
112
                strval(static::getXsiTypeNamespaceURI()),
113
            );
114
        }
115
116
        $type = new XMLAttribute(C_XSI::NS_XSI, 'xsi', 'type', $this->getXsiType());
117
        $type->toXML($e);
118
119
        return $e;
120
    }
121
}
122