Passed
Push — master ( a3fda0...7c0590 )
by Tim
14:58 queued 13:28
created

AbstractStatement   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 30
dl 0
loc 88
rs 10
c 0
b 0
f 0
wmc 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\SAML2\XML\saml;
6
7
use DOMElement;
8
use SimpleSAML\SAML2\Assert\Assert;
9
use SimpleSAML\SAML2\Constants as C;
10
use SimpleSAML\SAML2\Utils;
11
use SimpleSAML\SAML2\XML\ExtensionPointInterface;
12
use SimpleSAML\SAML2\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
/**
23
 * Class implementing the <saml:Statement> extension point.
24
 *
25
 * @package simplesamlphp/saml2
26
 */
27
abstract class AbstractStatement extends AbstractStatementType implements
28
    ExtensionPointInterface,
29
    SchemaValidatableElementInterface
30
{
31
    use ExtensionPointTrait;
32
    use SchemaValidatableElementTrait;
33
34
35
    public const string LOCALNAME = 'Statement';
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_STRING, expecting '=' on line 35 at column 24
Loading history...
36
37
38
    /**
39
     * Initialize a custom saml:Statement element.
40
     *
41
     * @param \SimpleSAML\XMLSchema\Type\QNameValue $type
42
     */
43
    protected function __construct(
44
        protected QNameValue $type,
45
    ) {
46
    }
47
48
49
    /**
50
     * @return \SimpleSAML\XMLSchema\Type\QNameValue
51
     */
52
    public function getXsiType(): QNameValue
53
    {
54
        return $this->type;
55
    }
56
57
58
    /**
59
     * Convert an XML element into a Statement.
60
     *
61
     * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException
62
     *   if the qualified name of the supplied element is wrong
63
     */
64
    public static function fromXML(DOMElement $xml): static
65
    {
66
        Assert::same($xml->localName, 'Statement', InvalidDOMElementException::class);
67
        Assert::same($xml->namespaceURI, C::NS_SAML, InvalidDOMElementException::class);
68
        Assert::true(
69
            $xml->hasAttributeNS(C_XSI::NS_XSI, 'type'),
70
            'Missing required xsi:type in <saml:Statement> element.',
71
            SchemaViolationException::class,
72
        );
73
74
        $type = QNameValue::fromDocument($xml->getAttributeNS(C_XSI::NS_XSI, 'type'), $xml);
75
76
        // now check if we have a handler registered for it
77
        $handler = Utils::getContainer()->getExtensionHandler($type);
78
        if ($handler === null) {
79
            // we don't have a handler, proceed with unknown statement
80
            return new UnknownStatement(new Chunk($xml), $type);
81
        }
82
83
        Assert::subclassOf(
84
            $handler,
85
            AbstractStatement::class,
86
            'Elements implementing Statement must extend \SimpleSAML\SAML2\XML\saml\AbstractStatement.',
87
        );
88
        return $handler::fromXML($xml);
89
    }
90
91
92
    /**
93
     * Convert this Statement to XML.
94
     */
95
    public function toXML(?DOMElement $parent = null): DOMElement
96
    {
97
        $e = $this->instantiateParentElement($parent);
98
        $e->setAttributeNS(
99
            'http://www.w3.org/2000/xmlns/',
100
            'xmlns:' . static::getXsiTypePrefix(),
101
            static::getXsiTypeNamespaceURI()->getValue(),
102
        );
103
104
        $type = new XMLAttribute(C_XSI::NS_XSI, 'xsi', 'type', $this->getXsiType());
105
        $type->toXML($e);
106
107
        return $e;
108
    }
109
}
110