Passed
Push — master ( 8413b2...00f101 )
by Tim
02:19
created

LocalAttribute::fromXML()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 18
nc 1
nop 1
dl 0
loc 23
rs 9.6666
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\XMLSchema\XML;
6
7
use DOMElement;
8
use SimpleSAML\XML\Assert\Assert;
9
use SimpleSAML\XMLSchema\Exception\{InvalidDOMElementException, TooManyElementsException};
10
use SimpleSAML\XMLSchema\Type\{IDValue, NCNameValue, QNameValue, StringValue};
11
use SimpleSAML\XMLSchema\Type\Schema\{FormChoiceValue, UseValue};
12
13
use function array_pop;
14
15
/**
16
 * Class representing the attribute-element.
17
 *
18
 * @package simplesamlphp/xml-common
19
 */
20
final class LocalAttribute extends AbstractAttribute
21
{
22
    /** @var string */
23
    public const LOCALNAME = 'attribute';
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
        $simpleType = LocalSimpleType::getChildrenOfClass($xml);
44
        Assert::maxCount($simpleType, 1, TooManyElementsException::class);
45
46
        return new static(
47
            self::getOptionalAttribute($xml, 'type', QNameValue::class, null),
48
            self::getOptionalAttribute($xml, 'name', NCNameValue::class, null),
49
            self::getOptionalAttribute($xml, 'ref', QNameValue::class, null),
50
            self::getOptionalAttribute($xml, 'use', UseValue::class, null),
51
            self::getOptionalAttribute($xml, 'default', StringValue::class, null),
52
            self::getOptionalAttribute($xml, 'fixed', StringValue::class, null),
53
            self::getOptionalAttribute($xml, 'form', FormChoiceValue::class, null),
54
            array_pop($simpleType),
55
            array_pop($annotation),
56
            self::getOptionalAttribute($xml, 'id', IDValue::class, null),
57
            self::getAttributesNSFromXML($xml),
58
        );
59
    }
60
}
61