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

TopLevelAttribute::fromXML()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 30
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

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