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

TopLevelAttribute::fromXML()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 30
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 21
nc 1
nop 1
dl 0
loc 30
rs 9.584
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\XML\{SchemaValidatableElementInterface, SchemaValidatableElementTrait};
10
use SimpleSAML\XMLSchema\Exception\{InvalidDOMElementException, SchemaViolationException, TooManyElementsException};
11
use SimpleSAML\XMLSchema\Type\Builtin\{IDValue, NCNameValue, QNameValue, StringValue};
12
use SimpleSAML\XMLSchema\Type\{FormChoiceValue, UseValue};
13
14
use function array_pop;
15
16
/**
17
 * Class representing the attribute-element.
18
 *
19
 * @package simplesamlphp/xml-common
20
 */
21
final class TopLevelAttribute extends AbstractTopLevelAttribute implements
22
    SchemaTopInterface,
23
    SchemaValidatableElementInterface
24
{
25
    use SchemaValidatableElementTrait;
1 ignored issue
show
introduced by
The trait SimpleSAML\XML\SchemaValidatableElementTrait requires some properties which are not provided by SimpleSAML\XMLSchema\XML\xs\TopLevelAttribute: $message, $line
Loading history...
26
27
    /** @var string */
28
    public const LOCALNAME = 'attribute';
29
30
31
    /**
32
     * Create an instance of this object from its XML representation.
33
     *
34
     * @param \DOMElement $xml
35
     * @return static
36
     *
37
     * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException
38
     *   if the qualified name of the supplied element is wrong
39
     */
40
    public static function fromXML(DOMElement $xml): static
41
    {
42
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
43
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
44
45
        // Prohibited attributes
46
        $ref = self::getOptionalAttribute($xml, 'ref', QNameValue::class, null);
47
        Assert::null($ref, SchemaViolationException::class);
48
49
        $form = self::getOptionalAttribute($xml, 'form', FormChoiceValue::class, null);
50
        Assert::null($form, SchemaViolationException::class);
51
52
        $use = self::getOptionalAttribute($xml, 'use', UseValue::class, null);
53
        Assert::null($use, SchemaViolationException::class);
54
55
        $annotation = Annotation::getChildrenOfClass($xml);
56
        Assert::maxCount($annotation, 1, TooManyElementsException::class);
57
58
        $simpleType = LocalSimpleType::getChildrenOfClass($xml);
59
        Assert::maxCount($simpleType, 1, TooManyElementsException::class);
60
61
        return new static(
62
            self::getAttribute($xml, 'name', NCNameValue::class),
63
            self::getOptionalAttribute($xml, 'type', QNameValue::class, null),
64
            self::getOptionalAttribute($xml, 'default', StringValue::class, null),
65
            self::getOptionalAttribute($xml, 'fixed', StringValue::class, null),
66
            array_pop($simpleType),
67
            array_pop($annotation),
68
            self::getOptionalAttribute($xml, 'id', IDValue::class, null),
69
            self::getAttributesNSFromXML($xml),
70
        );
71
    }
72
}
73