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

LocalSimpleType   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Importance

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