Completed
Push — master ( f72cfb...4ef12f )
by Tim
21s queued 18s
created

SimpleExtension::fromXML()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 16
nc 1
nop 1
dl 0
loc 22
rs 9.7333
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\XMLSchema\XML\xs;
6
7
use DOMElement;
8
use SimpleSAML\XML\Assert\Assert;
9
use SimpleSAML\XMLSchema\Exception\{InvalidDOMElementException, TooManyElementsException};
10
use SimpleSAML\XMLSchema\Type\Builtin\{IDValue, QNameValue};
11
12
use function array_merge;
13
use function array_pop;
14
15
/**
16
 * Class representing the simple version of the xs:extension.
17
 *
18
 * @package simplesamlphp/xml-common
19
 */
20
final class SimpleExtension extends AbstractSimpleExtensionType
21
{
22
    /** @var string */
23
    public const LOCALNAME = 'extension';
24
25
26
    /**
27
     * Create an instance of this object from its XML representation.
28
     *
29
     * @param \DOMElement $xml
30
     * @return static
31
     *
32
     * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException
33
     *   if the qualified name of the supplied element is wrong
34
     */
35
    public static function fromXML(DOMElement $xml): static
36
    {
37
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
38
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
39
40
        $annotation = Annotation::getChildrenOfClass($xml);
41
        Assert::maxCount($annotation, 1, TooManyElementsException::class);
42
43
        $localAttribute = LocalAttribute::getChildrenOfClass($xml);
44
        $attributeGroup = ReferencedAttributeGroup::getChildrenOfClass($xml);
45
        $attributes = array_merge($localAttribute, $attributeGroup);
46
47
        $anyAttribute = AnyAttribute::getChildrenOfClass($xml);
48
        Assert::maxCount($anyAttribute, 1, TooManyElementsException::class);
49
50
        return new static(
51
            self::getAttribute($xml, 'base', QNameValue::class),
52
            $attributes,
53
            array_pop($anyAttribute),
54
            array_pop($annotation),
55
            self::getOptionalAttribute($xml, 'id', IDValue::class, null),
56
            self::getAttributesNSFromXML($xml),
57
        );
58
    }
59
}
60