LocalSimpleType::fromXML()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 27
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 19
nc 1
nop 1
dl 0
loc 27
rs 9.6333
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;
10
use SimpleSAML\XMLSchema\Exception\MissingElementException;
11
use SimpleSAML\XMLSchema\Exception\SchemaViolationException;
12
use SimpleSAML\XMLSchema\Exception\TooManyElementsException;
13
use SimpleSAML\XMLSchema\Type\IDValue;
14
use SimpleSAML\XMLSchema\Type\NCNameValue;
15
use SimpleSAML\XMLSchema\Type\Schema\SimpleDerivationSetValue;
16
17
use function array_merge;
18
use function array_pop;
19
20
/**
21
 * Class representing the abstract simpleType.
22
 *
23
 * @package simplesamlphp/xml-common
24
 */
25
final class LocalSimpleType extends AbstractLocalSimpleType
26
{
27
    /** @var string */
28
    public const LOCALNAME = 'simpleType';
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
        $annotation = Annotation::getChildrenOfClass($xml);
46
        Assert::maxCount($annotation, 1, TooManyElementsException::class);
47
48
        $union = Union::getChildrenOfClass($xml);
49
        $xsList = XsList::getChildrenOfClass($xml);
50
        $restriction = Restriction::getChildrenOfClass($xml);
51
52
        $derivation = array_merge($union, $xsList, $restriction);
53
        Assert::minCount($derivation, 1, MissingElementException::class);
54
        Assert::maxCount($derivation, 1, TooManyElementsException::class);
55
56
        $name = self::getOptionalAttribute($xml, 'name', NCNameValue::class, null);
57
        Assert::null($name, SchemaViolationException::class);
58
59
        $final = self::getOptionalAttribute($xml, 'final', SimpleDerivationSetValue::class, null);
60
        Assert::null($final, SchemaViolationException::class);
61
62
        return new static(
63
            $derivation[0],
64
            array_pop($annotation),
65
            self::getOptionalAttribute($xml, 'id', IDValue::class, null),
66
            self::getAttributesNSFromXML($xml),
67
        );
68
    }
69
}
70