AbstractQuery::fromXML()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

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