Passed
Branch feature/php8.3 (4d3b0a)
by Tim
17:15
created

TopLevelSimpleType   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 1
eloc 19
dl 0
loc 43
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\MissingElementException;
13
use SimpleSAML\XMLSchema\Exception\TooManyElementsException;
14
use SimpleSAML\XMLSchema\Type\IDValue;
15
use SimpleSAML\XMLSchema\Type\NCNameValue;
16
use SimpleSAML\XMLSchema\Type\Schema\SimpleDerivationSetValue;
17
use SimpleSAML\XMLSchema\XML\Interface\RedefinableInterface;
18
19
use function array_merge;
20
use function array_pop;
21
22
/**
23
 * Class representing the abstract simpleType.
24
 *
25
 * @package simplesamlphp/xml-common
26
 */
27
final class TopLevelSimpleType extends AbstractTopLevelSimpleType implements
28
    RedefinableInterface,
29
    SchemaValidatableElementInterface
30
{
31
    use SchemaValidatableElementTrait;
32
33
34
    public const string LOCALNAME = 'simpleType';
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_STRING, expecting '=' on line 34 at column 24
Loading history...
35
36
37
    /**
38
     * Create an instance of this object from its XML representation.
39
     *
40
     * @param \DOMElement $xml
41
     * @return static
42
     *
43
     * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException
44
     *   if the qualified name of the supplied element is wrong
45
     */
46
    public static function fromXML(DOMElement $xml): static
47
    {
48
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
49
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
50
51
        $annotation = Annotation::getChildrenOfClass($xml);
52
        Assert::maxCount($annotation, 1, TooManyElementsException::class);
53
54
        $union = Union::getChildrenOfClass($xml);
55
        $xsList = XsList::getChildrenOfClass($xml);
56
        $restriction = Restriction::getChildrenOfClass($xml);
57
58
        $derivation = array_merge($union, $xsList, $restriction);
59
        Assert::minCount($derivation, 1, MissingElementException::class);
60
        Assert::maxCount($derivation, 1, TooManyElementsException::class);
61
62
        return new static(
63
            $derivation[0],
64
            self::getAttribute($xml, 'name', NCNameValue::class),
65
            self::getOptionalAttribute($xml, 'final', SimpleDerivationSetValue::class, null),
66
            array_pop($annotation),
67
            self::getOptionalAttribute($xml, 'id', IDValue::class, null),
68
            self::getAttributesNSFromXML($xml),
69
        );
70
    }
71
}
72