Completed
Push — master ( f72cfb...4ef12f )
by Tim
21s queued 18s
created

SimpleChoice::fromXML()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 35
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 23
nc 1
nop 1
dl 0
loc 35
rs 9.552
c 1
b 0
f 0
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\{InvalidDOMElementException, SchemaViolationException, TooManyElementsException};
10
use SimpleSAML\XMLSchema\Type\Builtin\{IDValue, NCNameValue, QNameValue};
11
use SimpleSAML\XMLSchema\Type\{MaxOccursValue, MinOccursValue};
12
13
use function array_merge;
14
use function array_pop;
15
16
/**
17
 * Class representing the choice-element.
18
 *
19
 * @package simplesamlphp/xml-common
20
 */
21
final class SimpleChoice extends AbstractSimpleExplicitGroup
22
{
23
    /** @var string */
24
    public const LOCALNAME = 'choice';
25
26
27
    /**
28
     * Create an instance of this object from its XML representation.
29
     *
30
     * @param \DOMElement $xml
31
     * @return static
32
     *
33
     * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException
34
     *   if the qualified name of the supplied element is wrong
35
     */
36
    public static function fromXML(DOMElement $xml): static
37
    {
38
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
39
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
40
41
        // Prohibited attributes
42
        $name = self::getOptionalAttribute($xml, 'name', NCNameValue::class, null);
43
        Assert::null($name, SchemaViolationException::class);
44
45
        $ref = self::getOptionalAttribute($xml, 'ref', QNameValue::class, null);
46
        Assert::null($ref, SchemaViolationException::class);
47
48
        $minOccurs = self::getOptionalAttribute($xml, 'minOccurs', MinOccursValue::class, null);
49
        Assert::null($minOccurs, SchemaViolationException::class);
50
51
        $maxOccurs = self::getOptionalAttribute($xml, 'maxOccurs', MaxOccursValue::class, null);
52
        Assert::null($maxOccurs, SchemaViolationException::class);
53
54
        // Start here
55
        $annotation = Annotation::getChildrenOfClass($xml);
56
        Assert::maxCount($annotation, 1, TooManyElementsException::class);
57
58
        $any = Any::getChildrenOfClass($xml);
59
        $choice = Choice::getChildrenOfClass($xml);
60
        $localElement = LocalElement::getChildrenOfClass($xml);
61
        $referencedGroup = ReferencedGroup::getChildrenOfClass($xml);
62
        $sequence = Sequence::getChildrenOfClass($xml);
63
64
        $particles = array_merge($any, $choice, $localElement, $referencedGroup, $sequence);
65
66
        return new static(
67
            nestedParticles: $particles,
68
            annotation: array_pop($annotation),
69
            id: self::getOptionalAttribute($xml, 'id', IDValue::class, null),
70
            namespacedAttributes: self::getAttributesNSFromXML($xml),
71
        );
72
    }
73
}
74