Passed
Pull Request — master (#305)
by Tim
02:37
created

Condition::fromXML()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 14
nc 1
nop 1
dl 0
loc 21
rs 9.7998
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\SAML2\XML\saml;
6
7
use DOMElement;
8
use SimpleSAML\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\Exception\InvalidDOMElementException;
14
use SimpleSAML\XML\Exception\SchemaViolationException;
15
16
use function explode;
17
18
/**
19
 * SAML Condition data type.
20
 *
21
 * @package simplesamlphp/saml2
22
 */
23
abstract class Condition extends AbstractSamlElement implements ExtensionPointInterface
24
{
25
    use ExtensionPointTrait;
26
27
    /** @var string */
28
    public const LOCALNAME = 'Condition';
29
30
31
    /**
32
     * Convert an XML element into a Condition.
33
     *
34
     * @param \DOMElement $xml The root XML element
35
     * @return \SimpleSAML\SAML2\XML\saml\Condition The condition
36
     *
37
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException if the qualified name of the supplied element is wrong
38
     */
39
    public static function fromXML(DOMElement $xml): object
40
    {
41
        Assert::same($xml->localName, 'Condition', InvalidDOMElementException::class);
42
        Assert::same($xml->namespaceURI, C::NS_SAML, InvalidDOMElementException::class);
43
        Assert::true(
44
            $xml->hasAttributeNS(C::NS_XSI, 'type'),
45
            'Missing required xsi:type in <saml:Condition> element.',
46
            InvalidDOMElementException::class
47
        );
48
49
        $type = $xml->getAttributeNS(C::NS_XSI, 'type');
50
        Assert::validQName($type, SchemaViolationException::class);
51
52
        list($prefix, $element) = explode(':', $type, 2);
53
        $ns = $xml->lookupNamespaceUri($prefix);
54
        $handler = Utils::getContainer()->getElementHandler($ns, $element);
55
56
        Assert::notNull($handler, 'Unknown Condition type `' . $type . '`.');
57
        Assert::isAOf($handler, Condition::class);
58
59
        return $handler::fromXML($xml);
60
    }
61
62
63
    /**
64
     * Convert this Condition to XML.
65
     *
66
     * @param \DOMElement $parent The element we are converting to XML.
67
     * @return \DOMElement The XML element after adding the data corresponding to this Condition.
68
     */
69
    public function toXML(DOMElement $parent = null): DOMElement
70
    {
71
        $e = $this->instantiateParentElement($parent);
72
73
        $e->setAttribute('xmlns:' . static::NS_XSI_TYPE_PREFIX, static::NS_XSI_TYPE_NAMESPACE);
0 ignored issues
show
Bug introduced by
The constant SimpleSAML\SAML2\XML\sam...::NS_XSI_TYPE_NAMESPACE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Bug introduced by
The constant SimpleSAML\SAML2\XML\sam...ion::NS_XSI_TYPE_PREFIX was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
74
        $e->setAttributeNS(C::NS_XSI, 'xsi:type', static::getXsiType());
75
76
        return $e;
77
    }
78
}
79