Passed
Pull Request — master (#61)
by Tim
02:17
created

LocalSimpleType   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 20
dl 0
loc 42
rs 10
c 1
b 0
f 0
wmc 1

1 Method

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