Passed
Branch master (c7ca4c)
by Tim
16:17 queued 14:33
created

TopLevelAttribute   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Importance

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